2015-12-10 148 views
3

我試圖將SSH自動化到一臺機器中,然後運行一些命令。但是,我正在逐漸的SSH部分卡:自動化SSH和運行命令

for h in ${hosts[*]}; do 
     ssh -i foo.pem [email protected]$h 
     echo "here" 
     sudo bash 
     cd /data/kafka/tmp/kafka-logs/ 
     exit 
     exit 
done 

當我運行該腳本,我可以SSH方式,但自動化在此消息停止:

******************************************************************************** 
This is a private computer system containing information that is proprietary 
and confidential to the owner of the system. Only individuals or entities 
authorized by the owner of the system are allowed to access or use the system. 
Any unauthorized access or use of the system or information is strictly 
prohibited. 

All violators will be prosecuted to the fullest extent permitted by law. 
******************************************************************************** 
Last login: Thu Dec 10 10:19:23 2015 from 10.81.120.55 
-bash: ulimit: open files: cannot modify limit: Operation not permitted 

當我按下control + d,我的echo "here"命令執行,然後我的腳本退出。不執行其餘的命令。

我看周圍,我試過,但我得到這個語法錯誤:

./kafka_prefill_count.sh: line 38: warning: here-document at line 26 delimited by end-of-file (wanted `EOF') 
./kafka_prefill_count.sh: line 39: syntax error: unexpected end of file 

腳本:

for h in ${hosts[*]}; do 
     ssh -i foo.pem [email protected]$h << EOF 
       echo "here" 
       sudo bash 
       cd /data/kafka/tmp/kafka-logs/ 
       ls | grep "$dir_name" 
       exit 
       exit 
       bash -l 

     EOF 
done 
+0

試着用'sudo'? –

+0

您可以使用ssh命令'ssh -i foo.pem bc2-user @ $ h '運行命令。編寫連接設備上運行的腳本可能比一系列ssh命令更容易。 –

+0

@MattTuttle我試圖運行相同的命令集到很多機器,所以我必須將該腳本複製到每臺機器=/ – Liondancer

回答

3

您當前的腳本是不是真的,你會如何使用ssh遠程執行命令。

它應該看起來更像這個

shh -i foo.pem [email protected] 'echo "here" ; hostname' 

(hostname命令就是例子來證明其他機器上的運行

Good resource

剛纔看到您的編輯:

EOF需要一直向左。

ssh -i foo.pem [email protected]$h << EOF 
      echo "here" 
      sudo bash 
      cd /data/kafka/tmp/kafka-logs/ 
      ls | grep "$dir_name" 
      exit 
      exit 
      bash -l 

EOF 
+1

當然,在你的例子中,hostname會在本地運行,而不是遠程運行,因爲';'會終止ssh命令沒有引用 –

+0

我很快就會接受答案。但是,如果我不想顯示SSH消息,你會知道該怎麼辦嗎?例如:'這是一個私人計算機系統.....' – Liondancer

+0

這個答案不能解決你對如何使用'sudo'和遠程shell的多種明顯的誤解。您應該接受@Cyrus的回答。 – tripleee

3

兩個here documents,一個是ssh和一個對於bash試試這個:

ssh -i foo.pem [email protected]$h << EOF1 
    echo "remote" 
    sudo bash << EOF2 
    cd /data/kafka/tmp/kafka-logs/ 
    ls | grep "$dir_name" 
EOF2 
EOF1