java 使用jsch 远程链接linux执行命令

一个简单的jsch链接linux并执行命令的utils。

         

import java.io.BufferedReader;  
  1. import java.io.IOException;  
  2. import java.io.InputStream;  
  3. import java.io.InputStreamReader;  
  4.   
  5. import com.jcraft.jsch.Channel;  
  6. import com.jcraft.jsch.ChannelExec;  
  7. import com.jcraft.jsch.JSch;  
  8. import com.jcraft.jsch.JSchException;  
  9. import com.jcraft.jsch.Session;  
  10.   
  11.   
  12.   
  13. public class ShellUtils {  
  14.     private static JSch jsch;  
  15.     private static Session session;  
  16.   
  17.       
  18.     /** 
  19.      * 连接到指定的IP 
  20.      *  
  21.      * @throws JSchException 
  22.      */  
  23.     public static void connect(String user, String passwd, String host) throws JSchException {  
  24.         jsch = new JSch();  
  25.         session = jsch.getSession(user, host, 22);  
  26.         session.setPassword(passwd);  
  27.           
  28.         java.util.Properties config = new java.util.Properties();  
  29.         config.put("StrictHostKeyChecking""no");  
  30.         session.setConfig(config);  
  31.           
  32.         session.connect();  
  33.     }  
  34.   
  35.     /** 
  36.      * 执行相关的命令 
  37.      * @throws JSchException  
  38.      */  
  39.     public static void execCmd(String command, String user, String passwd, String host) throws JSchException {  
  40.         connect(user, passwd, host);  
  41.           
  42.         BufferedReader reader = null;  
  43.         Channel channel = null;  
  44.   
  45.         try {  
  46.             while (command != null) {  
  47.                 channel = session.openChannel("exec");  
  48.                 ((ChannelExec) channel).setCommand(command);  
  49.                   
  50.                 channel.setInputStream(null);  
  51.                 ((ChannelExec) channel).setErrStream(System.err);  
  52.   
  53.                 channel.connect();  
  54.                 InputStream in = channel.getInputStream();  
  55.                 reader = new BufferedReader(new InputStreamReader(in));  
  56.                 String buf = null;  
  57.                 while ((buf = reader.readLine()) != null) {  
  58.                     System.out.println(buf);  
  59.                 }  
  60.             }  
  61.         } catch (IOException e) {  
  62.             e.printStackTrace();  
  63.         } catch (JSchException e) {  
  64.             e.printStackTrace();  
  65.         } finally {  
  66.             try {  
  67.                 reader.close();  
  68.             } catch (IOException e) {  
  69.                 e.printStackTrace();  
  70.             }  
  71.             channel.disconnect();  
  72.             session.disconnect();  
  73.         }  
  74.     }  
  75.      
  76. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值