我想通过ssh批量运行一个命令,并让调用机器确定该命令的退出状态和输出,但密码是事先知道的,而不是手工输入。< / p>
This似乎是我想要的,但它不起作用。
飞行前检查:
#> ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null testuser@localhost 'echo here' && echo ok || echo no
Warning: Permanently added '[localhost]:22' (ECDSA) to the list of known hosts.
testuser@localhost's password:
here
ok
良好的开端。现在用SSH_ASKPASS
捆绑它#!/bin/bash
u="testuser"
p="testpassword"
ask="/tmp/ask"
echo "echo $p" > $ask
chmod +x $ask
SSH_ASKPASS=$ask
DISPLAY=localhost:0.0
setsid ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null $u@localhost 'echo here' && echo ok || echo no
忽略安全问题,这只是一个POC。
当我运行时
#> /tmp/test.sh
Warning: Permanently added '[localhost]:22' (ECDSA) to the list of known hosts.
Permission denied, please try again.
Permission denied, please try again.
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
no
出了什么问题?
答案 0 :(得分:1)
您需要导出环境变量以申请ssh
命令:
export DISPLAY
export SSH_ASKPASS
使用ssh
运行-vvv
会告诉您SSH_ASKPASS
命令未被使用。
答案 1 :(得分:0)
完整的工作代码如下所示
#!/bin/bash
u="testuser"
p="testpassword"
ask="/tmp/ask"
echo "echo $p" > $ask
chmod +x $ask
SSH_ASKPASS=$ask
DISPLAY=localhost:0.0
export DISPLAY
export SSH_ASKPASS
setsid ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null $u@localhost 'echo here' && echo ok || echo no