我试过了:
#!bin/bash
ssh user@host.com 'sudo /etc/init.d/script restart'
但是我收到了这个错误:
sudo: no tty present and no askpass program specified
我该如何运行该脚本?现在,当我需要运行该脚本时,我会执行以下步骤:
ssh user@host.com
sudo /etc/init.d/script restart
但我不想一直手动登录到远程服务器,然后输入restart命令。
我可以编写我可以运行的本地脚本,所以我只需要输入密码,然后运行远程脚本并重启进程吗?
答案 0 :(得分:4)
您可以在-t
命令中使用ssh
选项使用ssh
命令附加伪tty :
ssh -t -t user@host.com 'sudo /etc/init.d/script restart'
根据man ssh
:
-t Force pseudo-tty allocation. This can be used to execute arbitrary screen-based programs on a remote machine, which can be very useful, e.g. when implementing menu services. Multiple -t options force tty allocation, even if ssh has no local tty.