JSch:纯JAVA实现远程执行SSH2主机的SHELL命令

上篇文章我编写了利用JSch实现SFTP的文件上传和下载 http://my.oschina.net/hetiangui/blog/137357,在本篇文章中,我将描述如何利用JSch实现执行远程SSH2主机的SHELL命令,不说了,直接上代码和详细的代码说明:


01 /**
02  * 利用JSch包实现远程主机SHELL命令执行
03  * @param ip 主机IP
04  * @param user 主机登陆用户名
05  * @param psw  主机登陆密码
06  * @param port 主机ssh2登陆端口,如果取默认值,传-1
07  * @param privateKey 密钥文件路径
08  * @param passphrase 密钥的密码
09  */
10 public static void sshShell(String ip, String user, String psw
11         ,int port ,String privateKey ,String passphrase) throws Exception{
12     Session session = null;
13     Channel channel = null;
14  
15      
16     JSch jsch = new JSch();
17  
18     //设置密钥和密码
19     if (privateKey != null && !"".equals(privateKey)) {
20         if (passphrase != null && "".equals(passphrase)) {
21             //设置带口令的密钥
22             jsch.addIdentity(privateKey, passphrase);
23         else {
24             //设置不带口令的密钥
25             jsch.addIdentity(privateKey);
26         }
27     }
28      
29     if(port <=0){
30         //连接服务器,采用默认端口
31         session = jsch.getSession(user, ip);
32     }else{
33         //采用指定的端口连接服务器
34         session = jsch.getSession(user, ip ,port);
35     }
36  
37     //如果服务器连接不上,则抛出异常
38     if (session == null) {
39         throw new Exception("session is null");
40     }
41      
42     //设置登陆主机的密码
43     session.setPassword(psw);//设置密码  
44     //设置第一次登陆的时候提示,可选值:(ask | yes | no)
45     session.setConfig("StrictHostKeyChecking""no");
46     //设置登陆超时时间  
47     session.connect(30000);
48          
49     try {
50         //创建sftp通信通道
51         channel = (Channel) session.openChannel("shell");
52         channel.connect(1000);
53  
54         //获取输入流和输出流
55         InputStream instream = channel.getInputStream();
56         OutputStream outstream = channel.getOutputStream();
57          
58         //发送需要执行的SHELL命令,需要用\n结尾,表示回车
59         String shellCommand = "ls \n";
60         outstream.write(shellCommand.getBytes());
61         outstream.flush();
62  
63  
64         //获取命令执行的结果
65         if (instream.available() > 0) {
66             byte[] data = new byte[instream.available()];
67             int nLen = instream.read(data);
68              
69             if (nLen < 0) {
70                 throw new Exception("network error.");
71             }
72              
73             //转换输出结果并打印出来
74             String temp = new String(data, 0, nLen,"iso8859-1");
75             System.out.println(temp);
76         }
77         outstream.close();
78         instream.close();
79     catch (Exception e) {
80         e.printStackTrace();
81     finally {
82         session.disconnect();
83         channel.disconnect();
84     }
85 }


利用JSch实现执行远程SSH2主机的SHELL命令,见我的博文:http://my.oschina.net/hetiangui/blog/137426

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值