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

在本篇文章中,我将描述如何利用JSch实现执行远程SSH2主机的SHELL命令,不说了,直接上代码和详细的代码说明:


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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值