2014-09-03 43 views
4

我嘗試這樣遠程腳本:慶典 - 運行從本地機器

#!bin/bash 
ssh [email protected] 'sudo /etc/init.d/script restart' 

但我得到這個錯誤:

sudo: no tty present and no askpass program specified 

我怎樣才能運行該腳本?現在,當我需要運行該腳本我做這些步驟:

ssh [email protected] 
sudo /etc/init.d/script restart 

但我不想手動登錄到遠程服務器中的所有時間,然後重新輸入命令。

我可以寫本地腳本,我可以運行,所以我只需要輸入密碼,它會運行遠程腳本並重新啓動過程?

+0

詢問你的系統管理員給你的特權在沒有密碼的情況下運行'sudo /etc/init.d/script'。如果你是系統管理員,請參閱'man visudo'和'man sudoers' – 2014-09-03 13:28:04

回答

4

您可以使用-t選項ssh命令附加僞終端ssh命令:

ssh -t -t [email protected] '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.

+1

感謝這個我正在尋找的東西。 – Andrius 2014-09-03 12:50:32

+0

不客氣,很高興它解決了。 – anubhava 2014-09-03 12:58:07

+0

今天我注意到奇怪的事情。當我從本地機器執行該命令時,我看到正常的消息:遠程服務器重新啓動並且一切正常,但實際上由於某種原因該服務器出現故障。如果我從遠程服務器正常重啓,它會正常重啓。爲什麼會發生這種情況?.. – Andrius 2014-09-05 08:22:31