从 Java 运行 Unix 命令的库 [英] Libraries to run unix commands from Java

查看:27
本文介绍了从 Java 运行 Unix 命令的库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找可用于从我的 Java 代码发送 Unix 命令的库.我已经尝试过 Expect4J 和 Gaynmed,但是找不到很好的文档.我的要求包括:

I am looking for libraries that I can use to send Unix commands from my Java code. I have already tried Expect4J and Gaynmed, but could not find good documentation with them. My requirements include:

  1. 运行多个命令.
  2. 捕获上一个命令的输出.
  3. 根据上一条命令的输出采取一些行动/运行不同的命令.

任何指针将不胜感激.

推荐答案

看你提到的库的名称,你似乎想使用 ssh 在远程服务器上运行命令.

查看http://code.google.com/p/sshxcute/

代码很简单

// Initialize a ConnBean object, parameter list is ip, username, password
ConnBean cb = new ConnBean("ip ", "username","password");
// Put the ConnBean instance as parameter for SSHExec static method getInstance(ConnBean) to retrieve a singleton SSHExec instance
ssh = SSHExec.getInstance(cb);          
// Connect to server
ssh.connect();
CustomTask sampleTask = new ExecCommand("echo 123");
ssh.exec(sampleTask);

获取输出也很容易.只需检查我提供的链接.

Getting the output is easy too. Just check the link I provided.

Result res = ssh.exec(task); 
if (res.isSuccess) {
    System.out.println("Return code: " + res.rc);
    System.out.println("sysout: " + res.sysout);
} else {
    System.out.println("Return code: " + res.rc);
    System.out.println("error message: " + res.error_msg);
}

这篇关于从 Java 运行 Unix 命令的库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆