我有一个样例js脚本,它使我能够查看位于服务器myHost中的文件,因此它可以完美地工作:
var exec = require('ssh-exec')
var v_host = 'myHost'
exec('ls -lh', {
user: 'username',
host: v_host,
password: 'password'
}).pipe(process.stdout , function (err, data) {
if ( err ) { console.log(v_host); console.log(err); }
console.log(data)
})现在,当我试图从groovy执行system commands时,我想设置host。以下是我的示例groovy脚本,但它在本地系统上工作,实际上我看到的文件位于我的本地服务器上:
Process p = Runtime.getRuntime().exec("ls -l")
p.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
StringBuilder execSb = new StringBuilder();
String line = "";
while ((line = reader.readLine())!= null) {
execSb.append(line + "\n");
}我可以像js script一样将host赋给exec方法吗
发布于 2020-06-19 01:47:55
ssh-exec执行远程系统命令
其中一个变体:下载jschXX.jar库并将其放入groovy/lib目录中
然后,您应该能够使用以下groovy代码:
def ant = new AntBuilder()
ant.sshexec{
host: "somehost",
username: "dude",
password: "yo",
command: "touch somefile",
outputproperty: "ssh_stdout"
}
println ant.ssh_stdout当前无法尝试该代码。
ant.sshexec命令的参数重新引用:https://ant.apache.org/manual/Tasks/sshexec.html
https://stackoverflow.com/questions/62448551
复制相似问题