ssh升级后+sftp+java_java中使用JSCH包,SFTP及SSH2文件操作及远程命令执行(改进)...

我写过一篇java中使用JSCH包,SFTP及SSH2文件操作及远程命令执行,现在想来,觉得调用方式太过于绕,不符合我写程序的风格,所以进行了改进。

参数类,用于配置连接的参数,SshConfiguration.java

[java]

/**

*

*/

package com.versou.util.jsch;

/**

* @author hadoop

*

*/

public class SshConfiguration {

private String host;

private String username;

private String password;

private int port;

public String getHost() {

return host;

}

public void setHost(String host) {

this.host = host;

}

public String getUsername() {

return username;

}

public void setUsername(String username) {

this.username = username;

}

public String getPassword() {

return password;

}

public void setPassword(String password) {

this.password = password;

}

public int getPort() {

return port;

}

public void setPort(int port) {

this.port = port;

}

}

/**

*

*/

package com.versou.util.jsch;

/**

* @author hadoop

*

*/

public class SshConfiguration {

private String host;

private String username;

private String password;

private int port;

public String getHost() {

return host;

}

public void setHost(String host) {

this.host = host;

}

public String getUsername() {

return username;

}

public void setUsername(String username) {

this.username = username;

}

public String getPassword() {

return password;

}

public void setPassword(String password) {

this.password = password;

}

public int getPort() {

return port;

}

public void setPort(int port) {

this.port = port;

}

}

工具类,VersouSshUtil.java

[java]

/**

*

*/

package com.versou.util.jsch;

import java.io.BufferedReader;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.nio.charset.Charset;

import java.util.Properties;

import org.apache.log4j.Logger;

import com.jcraft.jsch.ChannelExec;

import com.jcraft.jsch.ChannelSftp;

import com.jcraft.jsch.JSch;

import com.jcraft.jsch.Session;

/**

* @author hadoop

*

*/

public class VersouSshUtil {

private ChannelSftp channelSftp;

private ChannelExec channelExec;

private Session session = null;

private int timeout = 60000;

private static final Logger LOG = Logger.getLogger(VersouSshUtil.class);

public VersouSshUtil(SshConfiguration conf) throws Exception

{

LOG.info("尝试连接到....host:" + conf.getHost() + ",username:" + conf.getUsername() + ",password:" + conf.getPassword() + ",port:" + conf.getPort());

JSch jsch = new JSch(); // 创建JSch对象

session = jsch.getSession(conf.getUsername(), conf.getHost(), conf.getPort()); // 根据用户名,主机ip,端口获取一个Session对象

session.setPassword(conf.getPassword()); // 设置密码

Properties config = new Properties();

config.put("StrictHostKeyChecking", "no");

session.setConfig(config); // 为Session对象设置properties

session.setTimeout(timeout); // 设置timeout时间

session.connect(); // 通过Session建立链接

}

public void download(String src, String dst) throws Exception

{

channelSftp = (ChannelSftp)session.openChannel("sftp");

channelSftp.connect();

channelSftp.get(src, dst, new FileProgressMonitor(), ChannelSftp.OVERWRITE);

channelSftp.quit();

}

public void upload(String src, String dst) throws Exception

{

channelSftp = (ChannelSftp)session.openChannel("sftp");

channelSftp.connect();

channelSftp.put(src, dst, new FileProgressMonitor(), ChannelSftp.OVERWRITE);

channelSftp.quit();

}

public void runCmd(String cmd, String charset) throws Exception

{

channelExec = (ChannelExec)session.openChannel("exec");

channelExec.setCommand(cmd);

channelExec.setInputStream(null);

channelExec.setErrStream(System.err);

channelExec.connect();

InputStream in = channelExec.getInputStream();

BufferedReader reader = new BufferedReader(new InputStreamReader(in, Charset.forName(charset)));

String buf = null;

while ((buf = reader.readLine()) != null)

{

System.out.println(buf);

}

reader.close();

channelExec.disconnect();

}

public void close()

{

session.disconnect();

}

}

/**

*

*/

package com.versou.util.jsch;

import java.io.BufferedReader;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.nio.charset.Charset;

import java.util.Properties;

import org.apache.log4j.Logger;

import com.jcraft.jsch.ChannelExec;

import com.jcraft.jsch.ChannelSftp;

import com.jcraft.jsch.JSch;

import com.jcraft.jsch.Session;

/**

* @author hadoop

*

*/

public class VersouSshUtil {

private ChannelSftp channelSftp;

private ChannelExec channelExec;

private Session session = null;

private int timeout = 60000;

private static final Logger LOG = Logger.getLogger(VersouSshUtil.class);

public VersouSshUtil(SshConfiguration conf) throws Exception

{

LOG.info("尝试连接到....host:" + conf.getHost() + ",username:" + conf.getUsername() + ",password:" + conf.getPassword() + ",port:" + conf.getPort());

JSch jsch = new JSch(); // 创建JSch对象

session = jsch.getSession(conf.getUsername(), conf.getHost(), conf.getPort()); // 根据用户名,主机ip,端口获取一个Session对象

session.setPassword(conf.getPassword()); // 设置密码

Properties config = new Properties();

config.put("StrictHostKeyChecking", "no");

session.setConfig(config); // 为Session对象设置properties

session.setTimeout(timeout); // 设置timeout时间

session.connect(); // 通过Session建立链接

}

public void download(String src, String dst) throws Exception

{

channelSftp = (ChannelSftp)session.openChannel("sftp");

channelSftp.connect();

channelSftp.get(src, dst, new FileProgressMonitor(), ChannelSftp.OVERWRITE);

channelSftp.quit();

}

public void upload(String src, String dst) throws Exception

{

channelSftp = (ChannelSftp)session.openChannel("sftp");

channelSftp.connect();

channelSftp.put(src, dst, new FileProgressMonitor(), ChannelSftp.OVERWRITE);

channelSftp.quit();

}

public void runCmd(String cmd, String charset) throws Exception

{

channelExec = (ChannelExec)session.openChannel("exec");

channelExec.setCommand(cmd);

channelExec.setInputStream(null);

channelExec.setErrStream(System.err);

channelExec.connect();

InputStream in = channelExec.getInputStream();

BufferedReader reader = new BufferedReader(new InputStreamReader(in, Charset.forName(charset)));

String buf = null;

while ((buf = reader.readLine()) != null)

{

System.out.println(buf);

}

reader.close();

channelExec.disconnect();

}

public void close()

{

session.disconnect();

}

}

main函数,

[plain]

/**

*

*/

package com.versou.util.jsch;

/**

* @author hadoop

*

*/

public class AppMain {

/**

* @param args

*/

public static void main(String[] args) {

SshConfiguration conf = new SshConfiguration();

conf.setHost("192.168.1.17");

conf.setUsername("root");

conf.setPassword("123456");

conf.setPort(22);

try {

VersouSshUtil sshUitl = new VersouSshUtil(conf);

sshUitl.upload("testData/vvv.txt", "/home/vvv.txt");

sshUitl.download("/home/test.txt", "testData/test.txt");

sshUitl.runCmd("java -version", "UTF-8");

sshUitl.close();

} catch (Exception e) {

e.printStackTrace();

}

}

}

/**

*

*/

package com.versou.util.jsch;

/**

* @author hadoop

*

*/

public class AppMain {

/**

* @param args

*/

public static void main(String[] args) {

SshConfiguration conf = new SshConfiguration();

conf.setHost("192.168.1.17");

conf.setUsername("root");

conf.setPassword("123456");

conf.setPort(22);

try {

VersouSshUtil sshUitl = new VersouSshUtil(conf);

sshUitl.upload("testData/vvv.txt", "/home/vvv.txt");

sshUitl.download("/home/test.txt", "testData/test.txt");

sshUitl.runCmd("java -version", "UTF-8");

sshUitl.close();

} catch (Exception e) {

e.printStackTrace();

}

}

}

原来的文章所写代码主要摘自网上,用起来很便扭,这次进行了修改,看起来直观许多。

注意目标机器要装java哦,否则请用ls等代替java -version

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值