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;
}

}

  

原文地址:https://www.cnblogs.com/SunshineKimi/p/10540276.html

时间: 2024-08-24 14:38:45

Java利用 ganymed-ssh2-build.jar来上传文件到linux以及下载linux文件以及执行linux shell命令的相关文章

nexus搭建maven私服及私服jar包上传和下载

nexus搭建maven私服及私服jar包上传和下载 标签: nexus管理maven库snapshot 2017-06-28 13:02 844人阅读 评论(0) 收藏 举报 分类: Maven(1) 版权声明:本文为博主原创文章,未经博主允许不得转载. 一.nexus搭建maven私服及相关介绍 1.下载nexus-2.12.0-01-bundle.zip(版本随意) 2.以管理员身份运行cmd,cd进入解压文件的bin目录,执行nexus.bat install 若未以管理员身份运行则安装

jar包上传到服务器端的那些事

做为一个彻头彻尾的菜鸟,最近需要在服务器上跑代码,揪心的事可真不少.      在本地写完代码了,得打包上传到服务器.问题来了,那么多第三方的jar包怎么办?     要么打成 Runnable的jar包吧,每次打的包都好大,上传效率太低了.要是遇上那种bug还有很多的代码,那就够麻烦了.要么打成普通的jar包,不把第三方的包包含进来,然后在profile中(linux系统下)修改classpath的值,这样也是可以的,但是如果第三方的Jar包很多,需要修改的classpath就很多了,也不大方

java配置ueditor中解决“未找到上传文件”错误提示

ueditor是一个功能十分强大的在线文本编辑器,但是在ssh框架中,确切的说实在struts2中由于其拦截器需要对request,session对象进行重新封装,这个过程中会把request对象中保存的一些内容清空,所以会导致ueditor的上传功能获取不到需要上传的内容导致“未找到上传文件”的错误! 参考网上资料和自己实验,最终的解决思路是,重写struts2中的一个转换的类,然后配置struts2使用我们重写的这个类.由于我们的工程中可能会有其他的上传等功能,为了不影响其他功能的时候,还需

iOS利用AFNetworking(AFN) 实现图片上传

1.上传图片以二进制流的形式上传 1 #pragma mark - 文件上传  2 - (IBAction)uploadImage  3 {  4     /*  5      此段代码如果需要修改,可以调整的位置  6        7      1. 把upload.php改成网站开发人员告知的地址  8      2. 把file改成网站开发人员告知的字段名  9      */ 10     // 1. httpClient->url 11      12     // 2. 上传请求P

java做的比较完善的FTP上传下载文件服务器源码

Filename: ftp.java Author: leetsing(elove) Create date: 2004-08-30 Use: connect to FTP server,then upload and download file Modify date: 2004-09-05 add to upload file 2004-09-13 add to download file Copy right: Magisky Media Technology Co.,Ltd. *****

maven jar包上传到服务器

maven jar包上传到服务器时出现pom文件没有上传上去,致使该jar包再被使用的时候没有依赖,jar包调用出错 解决办法,将pom文件一起deploy上去 mvn deploy:deploy-file -DgroupId=com.hikvision -DartifactId=WebXmlServer -Dversion=1.0.0-SNAPSHOT -Dpackaging=jar -Dfile="E:\workfiles\WebXmlServer\target\WebXmlServer-1

ios系统 竖屏拍照 canvas处理后 图片旋转(利用exif.js解决ios手机上传竖拍照片旋转90度问题)

转:https://www.cnblogs.com/lovelgx/articles/8656615.html ---恢复内容开始--- 问题:html5+canvas进行移动端手机照片上传时,发现ios手机上传竖拍照片会逆时针旋转90度,横拍照片无此问题:Android手机没这个问题. 解决方法:利用exif.js解决ios手机上传竖拍照片旋转90度问题 因此解决这个问题的思路是:获取到照片拍摄的方向角,对非横拍的ios照片进行角度旋转修正. 利用exif.js读取照片的拍摄信息,详见 htt

windows下用idea编写wordcount单词计数项目并打jar包上传到hadoop执行

windows下用idea编写wordcount单词计数项目并打jar包上传到hadoop执行 参考 https://blog.csdn.net/weixin_42370346/article/details/88688693 1.最后一步运行jar包时报错 原因:未指出主类名 解决方法:hadoop jar /home/wxx/WordCount.jar com.hadoop.project.WordcountMain /user/hadoop/input/file.txt /user/had

通过命令行将第三方jar包上传到Nexus3

1. Nexus2 才支持通过网页上传文件,Nexus3不支持,只能通过命令行 2. 在 Maven 默认 settings.xml 添加如下配置: 1 <server> 2 <id>gs-releases</id> 3 <username>user</username> 4 <password>password</password> 5 </server> 3. 执行命令: 1 mvn deploy:depl

AJAX文件上传实践与分析,带HTML5文件上传API。

对于HTML5已经支持AJAX文件上传了,但如果需要兼容的话还是得用一点小技巧的,HTML5等等介绍,先来看看以前我们是怎么写的. 网上可能会有一些叫AJAX文件上传插件,但在AJAX2.0之前是不可能实现的,因为浏览器的原因,AJAX根本获取不了文件信息,当然这里并不是说就不能文件上传了,只是说在AJAX2.0之前所谓的AJAX文件上传都是假冒的,核心更本没有用AJAX,而是利用iframe实现的,下面我们来看看如何利用iframe实现页面无刷新上传文件. iframe无刷新上传文件版. ht