我没有在我的两台服务器a和b之间启用无密码ssh,所以我使用sshpass从a连接到服务器b。
我需要从a在服务器b的/etc/hosts中添加主机条目。但我登录到服务器b的用户是非root用户,但具有编辑root拥有的文件的sudo权限。
在使用sshpass时,如何通过shell脚本将主机条目从服务器a添加到服务器b的/etc/hosts中。
以下是尝试过的脚本:
#!/bin/bash
export SSHPASS="password"
SSHUSER=ciuser
WPC_IP=10.8.150.28
sshpass -e ssh -o UserKnownHostsFile=/dev/null -o 'StrictHostKeyChecking no' $SSHUSER@$WPC_IP "echo test >> /etc/hosts"输出:
bash test.sh
Warning: Permanently added '10.8.150.28' (RSA) to the list of known hosts.
bash: /etc/hosts: Permission denied谢谢。
发布于 2016-12-03 06:45:09
直接sudo doesn't work with redirects,因此您可以使用sudo tee -a将其追加到文件:
echo '1.2.3.4 test' | sudo tee -a /etc/hosts在您的命令中,这将是:
sshpass -e ssh -o UserKnownHostsFile=/dev/null -o 'StrictHostKeyChecking no' "$SSHUSER@$WPC_IP" "echo test | sudo tee -a /etc/hosts"请注意,这需要没有tty的无密码sudo访问,这不一定与您的sudo权限相同。
https://stackoverflow.com/questions/40940118
复制相似问题