Java利用 ganymed-ssh2-build.jar来上传文件到linux以及下载linux文件以及执行linux shell命令
package api;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.InputStream;
import org.apache.log4j.*;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.SCPClient;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
public class SshTest {
public static void main(String[] args) {
SshTest.exeCmd("df -m","root","123456","192.168.229.128", 22);
}
private static Logger logger = Logger.getLogger(SshTest.class);
public static Connection getConnect(String user,String password ,String ip,int port ) {
Connection conn=new Connection(ip, port);
try {
conn.connect();
conn.authenticateWithPassword(user, password);
logger.error("ssh连接ok");
logger.info("hhhhhhhh");
}catch(Exception e) {
e.printStackTrace();
}
return conn;
}
public static String exeCmd(String cmd,String user,String password ,String ip,int port){
String line=null;
Connection connection=null;
Session session=null;
try {
connection=getConnect(user, password, ip, port);
session=connection.openSession();
session.execCommand(cmd);
InputStream in = new StreamGobbler(session.getStdout());
BufferedReader brs = new BufferedReader(new InputStreamReader(in));
line = brs.readLine();
// logger.info(line);
}catch(Exception e) {
e.printStackTrace();
}finally {
session.close();
connection.close();
}
return line;
}
// downLoadFile from Linux
public static boolean sftpDownload(String remoteFilePath,String localFilePath,String user,String password ,String ip,int port) {
boolean bool=false;
Connection connection=null;
try {
connection=getConnect(user, password, ip, port);
SCPClient scpClient = connection.createSCPClient();
scpClient.put(localFilePath, remoteFilePath);
bool=true;
}catch(IOException ioe) {
ioe.printStackTrace();
bool =false;
}finally {
connection.close();
}
return bool;
}
// uploadFile to Linux
public static boolean uoloadFile(String remoteFilePath,String localFilePath,String user,String password ,String ip,int port) {
boolean bool=false;
Connection connection=null;
try {
connection=getConnect(user, password, ip, port);
SCPClient scpClient = connection.createSCPClient();
scpClient.get(remoteFilePath, localFilePath);
bool=true;
}catch(IOException ioe) {
ioe.printStackTrace();
bool =false;
}finally {
connection.close();
}
return bool;
}
}
Java利用 ganymed-ssh2-build.jar来上传文件到linux以及下载linux文件以及执行linux shell命令的更多相关文章
- java导出excel并且压缩成zip上传到oss,并下载,使用字节流去存储,不用文件流保存文件到本地
最近项目上要求实现导出excel并根据条数做分割,然后将分割后的多个excel打包成压缩包上传到oss服务器上,然后提供下载方法,具体代码如下:这里只展示部分代码,获取数据的代码就不展示了 ByteA ...
- IDEA如何将写好的java类(UDF函数)打成jar包上传linux
一.编写一个UDF函数,实现将字符串大写转小写 import org.apache.hadoop.hive.ql.exec.UDF; import org.apache.hadoop.io.Text; ...
- 构建自己的jar包上传至Mvaen中央仓库和版本更新
构建自己的jar包上传至Mvaen中央仓库和版本更新 一直羡慕别人制造轮子,开源项目,供别人使用:我也想这样,可以自己才疏学浅,本次就将自己写小工具上传到Maven的中央仓库. 一步一步详细教程演示如 ...
- Apache Flink任意Jar包上传导致远程代码执行漏洞复现
0x00 简介 Apache Flink是由Apache软件基金会开发的开源流处理框架,其核心是用Java和Scala编写的分布式流数据流引擎.Flink以数据并行和流水线方式执行任意流数据程序,Fl ...
- kindeditor在Java项目中的应用以及图片上传配置
在官网下载Kindededitor的开发包 在项目中javaweb项目中导入kindeditor必须要使用的Jar包(用于文件上传,除非你的富文本编辑器不使用图片上传)jar包可以在官网的开发包中 ...
- nexus搭建maven私服及私服jar包上传和下载
nexus搭建maven私服及私服jar包上传和下载 标签: nexus管理maven库snapshot 2017-06-28 13:02 844人阅读 评论(0) 收藏 举报 分类: Maven(1 ...
- win7下利用ftp实现华为路由器的上传和下载
win7下利用ftp实现华为路由器的上传和下载 1. Win7下ftp的安装和配置 (1)开始->控制面板->程序->程序和功能->打开或关闭Windows功能 (2)在Wi ...
- Maven中安装本地Jar包到仓库中或将本地jar包上传
摘要 maven install 本地jar 命令格式 mvn install:install-file -DgroupId=<group_name> -DartifactId=<a ...
- 利用jquery+iframe做一个ajax上传效果
以下是自学it网--中级班上课笔记 网址:www.zixue.it html页面 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict ...
- 利用WCF与Android实现图片上传并传参
利用WCF与Android实现图片上传并传参 最近做一个项目后端使用WCF接收Android手机拍照并带其它参数保存到服务器里:刚好把最近学习的WCF利用上,本以为是个比较简单的功能应该很好实现,没想 ...
随机推荐
- CodeForces 606A(水)
这道题之前没注意到at least,审题不仔细啊,两个问题解法还是有些许区别的 有at least的 #include <iostream> #include <string> ...
- PHP之httpRequest
<?php /** * Respose A Http Request * * @param string $url * @param array $post * @param string $m ...
- 【HTML&CSS】基本的入门
在公司培训一段时间不久就去流浪了一段时间,现在回来重新捧起心爱的编程,特别亲切. 自学HTML&CSS,别人说了很多,这那这那的,无论简单还是困难,不亲自去俯下身子学习,怎么都学不会HTML和 ...
- 【MySQL数据库】一些bug的解决
往往碰到mysql配置好后,第二天就登不上,也运行不了服务. 在cmd中输入 net start mysql 报mysql无法启动系统错误1067等 解决方案: 以前搞到最后没办法,重装了,后来找到 ...
- input button 与 submit 的区别
在表单中,我们会经常提交数据,通常使用<input type="submit" value="提交"/>进行提交数据, 另一种方式是使用<bu ...
- Android 图片缩略图显示
//通过openRawResource获取一个inputStream对象 InputStream inputStream = getResources().openRawResource(R.draw ...
- MUI框架-04-切换页面头部文字重叠
MUI框架-04-切换页面头部文字重叠 从现在开始就只分享 MUI 框架开发时遇到的问题了 开发不懂请查看:官方文档 问题情况 切换页面 title 文字重叠,覆盖 截图: 解决办法 给 h1 标签加 ...
- IIS支持http协议的:put、delete等方法
HTTP常见方法:DELETE, GET, HEAD, OPTIONS, POST, PUT, PATCH 以下是从网上搜索的方法:其实经过测试,只配置最后那个web.config就可以实现这个功能: ...
- Linux常用命令(二)————压缩+解压
tar -c: 建立压缩档案-x:解压-t:查看内容-r:向压缩归档文件末尾追加文件-u:更新原压缩包中的文件 这五个是独立的命令,压缩解压都要用到其中一个,可以和别的命令连用但只能用其中一个.下面的 ...
- 【Leetcode】【Easy】Isomorphic Strings
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...