栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java远程调用Shell脚本并获取输出信息

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java远程调用Shell脚本并获取输出信息

1、添加依赖


  ch.ethz.ganymed
  ganymed-ssh2
  262


  commons-io
  commons-io
  2.6

2、Api说明

首先构造一个连接器,传入一个需要登陆的ip地址;

Connection conn = new Connection(ipAddr);

模拟登陆目的服务器,传入用户名和密码;

boolean isAuthenticated = conn.authenticateWithPassword(userName, passWord);

它会返回一个布尔值,true 代表成功登陆目的服务器,否则登陆失败。

打开一个session,执行你需要的linux 脚本命令;

Session session = conn.openSession();
session.execCommand(“ifconfig”);

接收目标服务器上的控制台返回结果,读取br中的内容;

InputStream stdout = new StreamGobbler(session.getStdout());
BufferedReader br = new BufferedReader(new InputStreamReader(stdout));

得到脚本运行成功与否的标志 :0-成功 非0-失败

System.out.println(“ExitCode: ” + session.getExitStatus());

关闭session和connection

session.close();
conn.close();

Tips:

通过第二部认证成功后当前目录就位于/home/username/目录之下,你可以指定脚本文件所在的绝对路径,或者通过cd导航到脚本文件所在的目录,然后传递执行脚本所需要的参数,完成脚本调用执行。
执行脚本以后,可以获取脚本执行的结果文本,需要对这些文本进行正确编码后返回给客户端,避免乱码产生。
如果你需要执行多个linux控制台脚本,比如第一个脚本的返回结果是第二个脚本的入参,你必须打开多个Session,也就是多次调用 Session sess = conn.openSession();,使用完毕记得关闭就可以了。

3. 实例:工具类

public class SSHTool {
  private Connection conn;
  private String ipAddr;
  private Charset charset = StandardCharsets.UTF_8;
  private String userName;
  private String password;
  public SSHTool(String ipAddr, String userName, String password, Charset charset) {
    this.ipAddr = ipAddr;
    this.userName = userName;
    this.password = password;
    if (charset != null) {
      this.charset = charset;
    }
  }
  
  private boolean login() {
    conn = new Connection(ipAddr);
    try {
      // 连接
      conn.connect();
      // 认证
      return conn.authenticateWithPassword(userName, password);
    } catch (IOException e) {
      e.printStackTrace();
      return false;
    }
  }
  
  public StringBuilder exec(String cmds) throws IOException {
    InputStream in = null;
    StringBuilder result = new StringBuilder();
    try {
      if (this.login()) {
 // 打开一个会话
 Session session = conn.openSession();
 session.execCommand(cmds);
 in = session.getStdout();
 result = this.processStdout(in, this.charset);
 conn.close();
      }
    } finally {
      if (null != in) {
 in.close();
      }
    }
    return result;
  }
  
  public StringBuilder processStdout(InputStream in, Charset charset) throws FileNotFoundException {
    byte[] buf = new byte[1024];
    StringBuilder sb = new StringBuilder();
//    OutputStream os = new FileOutputStream("./data.txt");
    try {
      int length;
      while ((length = in.read(buf)) != -1) {
// os.write(buf, 0, c);
 sb.append(new String(buf, 0, length));
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
    return sb;
  }
  public static void main(String[] args) throws IOException {
    SSHTool tool = new SSHTool("192.168.100.40", "root", "123456", StandardCharsets.UTF_8);
    StringBuilder exec = tool.exec("bash /root/test12345.sh");
    System.out.println(exec);
  }
}

总结

以上所述是小编给大家介绍的Java远程调用Shell脚本并获取输出信息,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对考高分网网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/136957.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号