2012-11-03 63 views
1

我有ssh登錄腳本,而不期望:慶典setsid&讀凍結

function do_auth_connect(){ 
    if [ -n "$SSH_ASKPASS_TMPFILE" ]; then 
     cat "$SSH_ASKPASS_TMPFILE" 
     exit 0 
    elif [ $# -lt 1 ]; then 
     echo "Usage: echo password | $0 <ssh command line options>" >&2 
     exit 1 
    fi 

    sighandler() { 
     rm "$TMP_PWD" 
    } 

    TMP_PWD=$(mktemp) 
    chmod 600 "$TMP_PWD" 
    trap 'sighandler' SIGHUP SIGINT SIGQUIT SIGABRT SIGKILL SIGALRM SIGTERM 

    export SSH_ASKPASS=$0 
    export SSH_ASKPASS_TMPFILE=$TMP_PWD 

    [ "$DISPLAY" ] || export DISPLAY=dummydisplay:0 
    read password 
    echo $password >> "$TMP_PWD" 

    # use setsid to detach from tty 
    #exec setsid "[email protected]" 
    setsid "[email protected]" 

    rm "$TMP_PWD" 
} 

如果我使用像命令:

echo "my_password" | do_auth_connect ssh [email protected] "uname -a" 

一切工作的罰款。但如果我使用:

read password 
echo "$password" | do_auth_connect ssh [email protected] "uname -a" 

腳本凍結。

「set -x」表示腳本進入循環,並等待另一個「讀取」參數。

我該如何解決這個問題?感謝

--- --- UPD(對於@Barmar)

設置-x輸出時確定(與SCP例如):

+ echo 'user_password' 
+ do_auth_connect scp -P 444 [email protected]:/home/user/dbg.log /home/user/dbg.log 
+ '[' -n '' ']' 
+ '[' 5 -lt 1 ']' 
++ mktemp 
+ TMP_PWD=/tmp/tmp.X8TKTIchq6 
+ chmod 600 /tmp/tmp.X8TKTIchq6 
+ trap sighandler SIGHUP SIGINT SIGQUIT SIGABRT SIGKILL SIGALRM SIGTERM 
+ export SSH_ASKPASS=./1.sh 
+ SSH_ASKPASS=./1.sh 
+ export SSH_ASKPASS_TMPFILE=/tmp/tmp.X8TKTIchq6 
+ SSH_ASKPASS_TMPFILE=/tmp/tmp.X8TKTIchq6 
+ '[' :0.0 ']' 
+ read password 
+ echo 'user_password' 
+ setsid scp -P 444 [email protected]:/home/user/dbg.log /home/user/dbg.log 
+ echo 'user_password' 
+ do_auth_connect scp -P 444 [email protected]:/home/user/dbg.log /home/user/dbg.log 
+ '[' -n /tmp/tmp.X8TKTIchq6 ']' 
+ cat /tmp/tmp.X8TKTIchq6 
+ exit 0 
+ rm /tmp/tmp.X8TKTIchq6 
+ exit 

並且當失敗:

+ read pass 
> user_password 
+ echo 'user_password' 
+ do_auth_connect scp -P 444 [email protected]:/home/user/dbg.log /home/user/dbg.log 
+ '[' -n '' ']' 
+ '[' 5 -lt 1 ']' 
++ mktemp 
+ TMP_PWD=/tmp/tmp.7usdmtHgqt 
+ chmod 600 /tmp/tmp.7usdmtHgqt 
+ trap sighandler SIGHUP SIGINT SIGQUIT SIGABRT SIGKILL SIGALRM SIGTERM 
+ export SSH_ASKPASS=./1.sh 
+ SSH_ASKPASS=./1.sh 
+ export SSH_ASKPASS_TMPFILE=/tmp/tmp.7usdmtHgqt 
+ SSH_ASKPASS_TMPFILE=/tmp/tmp.7usdmtHgqt 
+ '[' :0.0 ']' 
+ read password 
+ echo 'user_password' 
+ setsid scp -P 444 [email protected]:/home/user/dbg.log /home/user/dbg.log 
+ read -p '> ' pass 

這就是全部,^ C不起作用:)

--- UPD2 ---

我發現任何 「讀」 打破了腳本 例如:

read null 
echo "user_password" | do_auth_connect scp -P 444 [email protected]:/home/user/dbg.log ~/dbg.log 

有一些麻煩與凍結

+0

我想不出任何東西 - 你可以將'set -x'的輸出添加到問題中嗎? – Barmar

+1

爲什麼不使用公鑰認證和鑰匙串應用而不是手動輸入密碼? – Barmar

+0

@Barmar done,thx –

回答

0
SSH_ASKPASS   If ssh needs a passphrase, it will read the passphrase from 
         the current terminal if it was run from a terminal. If ssh 
         does not have a terminal associated with it but DISPLAY and 
         SSH_ASKPASS are set, it will execute the program specified by 
         SSH_ASKPASS and open an X11 window to read the passphrase. 
         This is particularly useful when calling ssh from a .xsession 
         or related script. (Note that on some machines it may be 
         necessary to redirect the input from /dev/null to make this 
         work.) 

關聯:

(請注意,在某些機器上,可能需要重定向來自/ dev/null的輸入以使其工作。)

這非常含糊,嘗試將輸入重定向到/ dev/null,發出ssh命令

-1

由於對mark a comment as an answer的功能請求仍然被拒絕,因此我在此複製上述解決方案。

當您執行SSH_ASKPASS = $ 0時,它表示「爲了獲取密碼,請再次運行此腳本」。如果腳本在開始時讀取通過,它將再次執行。您需要確定密碼是否已經保存在臨時文件中。 - Barmar

+3

而不是不斷地添加這些類型的答案,爲什麼不向用戶(在這種情況下是Barmar)發表評論,並要求他們發佈答案。 – Taryn

+0

1.如果用戶決定不發佈他的解答作爲答案,爲什麼他應該改變主意? 2.問他會涉及到記得直到有一段時間,然後重新訪問此頁並檢查他是否發佈了答案;這樣的程序比添加這種類型的答案要困難得多,如果他不這樣做,我們什麼也得不到。 – Armali

+1

@Armali,如果您有時間將註釋轉換爲答案,您至少應該騰出更多時間來美化答案,格式化代碼,添加解釋,刪除簽名等。噢,並刪除對被拒絕功能的無用參考。連接一些評論並不能解答問題。 –

0

來到這尋找類似的情況:ssh調用一個包含setsid調用的遠程腳本,並且觀察ssh永不終止。

ssh remotehost start-daemon.sh 

其中start-daemon。sh

#!/bin/bash 
... 
setsid $daemon $args & 
exit 0 

ssh會掛起,必須給^ C停止。此修復程序(如暗示在https://serverfault.com/questions/364689評論)是重定向setsid的stdout:

setsid $daemon $args > /dev/null & 

然後SSH停止如預期。在我的測試中,stderr不需要相同的處理。