| 注册
请输入搜索内容

热门搜索

Java Linux MySQL PHP JavaScript Hibernate jQuery Nginx
wgd7
9年前发布

采用JSCH实现对linux远程操作,执行简单的命令

 JSch是Java Secure Channel的缩写。JSch是一个SSH2的纯Java实现。它允许你连接到一个SSH服务器,并且可以使用端口转发,X11转发,文件传输等,当然你也可以集成它的功能到你自己的应用程序。

官方地址为:http://www.jcraft.com/jsch/
GitHub 地址为:https://github.com/vngx/vngx-jsch
JSCH的特点:

1.基于DSA和RSA加密。

2.可以实现4中认证机制。分别是:

(1i): password

(2i): publickey(DSA,RSA)

(3i): keyboard-interactive

(4i): gss-api-with-mic

3.生成public/private key pair.

4.执行bash script 等脚本

5.可以通过HTTP/SOCK5 proxy

6.支持常见SSH1协议和SSH2协议

import java.io.BufferedReader;  import java.io.IOException;  import java.io.InputStream;  import java.io.InputStreamReader;  import java.nio.charset.Charset;  import com.jcraft.jsch.Channel;  import com.jcraft.jsch.ChannelExec;  import com.jcraft.jsch.JSch;  import com.jcraft.jsch.JSchException;  import com.jcraft.jsch.Session;    public class JSchDemo {      private String charset = "UTF-8"; // 设置编码格式      private String user; // 用户名      private String passwd; // 登录密码      private String host; // 主机IP      private JSch jsch;      private Session session;        /**       *        * @param user用户名       * @param passwd密码       * @param host主机IP       */      public JSchDemo(String user, String passwd, String host) {          this.user = user;          this.passwd = passwd;          this.host = host;      }        /**       * 连接到指定的IP       *        * @throws JSchException       */      public void connect() throws JSchException {          jsch = new JSch();          session = jsch.getSession(user, host, 22);          session.setPassword(passwd);          java.util.Properties config = new java.util.Properties();          config.put("StrictHostKeyChecking", "no");          session.setConfig(config);          session.connect();      }        /**       * 执行相关的命令       */      public void execCmd() {          BufferedReader br = new BufferedReader(new InputStreamReader(System.in));            String command = "";          BufferedReader reader = null;          Channel channel = null;            try {              while ((command = br.readLine()) != null) {                  channel = session.openChannel("exec");                  ((ChannelExec) channel).setCommand(command);                  channel.setInputStream(null);                  ((ChannelExec) channel).setErrStream(System.err);                    channel.connect();                  InputStream in = channel.getInputStream();                  reader = new BufferedReader(new InputStreamReader(in,                          Charset.forName(charset)));                  String buf = null;                  while ((buf = reader.readLine()) != null) {                      System.out.println(buf);                  }              }          } catch (IOException e) {              e.printStackTrace();          } catch (JSchException e) {              e.printStackTrace();          } finally {              try {                  reader.close();              } catch (IOException e) {                  e.printStackTrace();              }              channel.disconnect();              session.disconnect();          }      }        public static void main(String[] args) throws Exception {          String user = "root";          String passwd = "123456";          String host = "192.168.1.188";            JSchDemo demo = new JSchDemo(user, passwd, host);          demo.connect();          demo.execCmd();      }  }