我知道有AntBuilder
andJSch
等,但我想做这样的事情 - 没有任何依赖:
def sshArray = ["ssh root@11.11.11.11 -p 111 '/etc/init.d/tomcat7 stop'", ...]
def env = System.getenv().collect { k,v -> "$k=$v" }
sshArray.each {
println "Executing: " + it
def process = (it).execute(env, null)
def writer = new PrintWriter(new BufferedOutputStream(process.out))
writer.println("mypassword")
writer.close()
process.waitFor()
process.consumeProcessOutput(System.out, System.err)
}
但这很遗憾不起作用,因为我得到的输出是:
Executing: ssh root@11.11.11.11 -p 111 '/etc/init.d/tomcat7 stop'
Permission denied, please try again.
Permission denied, please try again.
Permission denied (publickey,password).
...
此外,如果我注释掉这些行,我会得到相同的输出:
def writer = new PrintWriter(new BufferedOutputStream(process.out))
writer.println("mypassword")
writer.close()
为什么我会收到 3 次“权限被拒绝”?如何为 ssh 进程提供密码?这可能吗?