簡體   English   中英

bash中的SSH while循環。 因為stdin不是終端,所以不會分配偽終端

[英]SSH while-loop in bash. Pseudo-terminal will not be allocated because stdin is not a terminal

我正在嘗試將文件ssh循環到服務器列表,並在這些服務器上為某些日志文件執行find命令。 我知道ssh將吞沒整個輸入文件。 所以我正在使用-n參數來ssh。 這工作正常,但在某些服務器上我遇到了新錯誤。

輸入文件的構建方式如下:servername:location:mtime:logfileexention

我使用的Bash中的代碼是:

sshCmd="ssh -n -o ConnectTimeout=5 -o Batchmode=yes -o StrictHostKeyChecking=no -o CheckHostIP=no -o PasswordAuthentication=no -q"

while IFS=: read -r f1 f2 f3 f4 ; do        
$sshCmd "$f1"
find "$f2" -type f -name "$f4" -mtime +"$f3"

在某些服務器上,我收到以下錯誤:

因為stdin不是終端,所以不會分配偽終端。 stty:標准輸入:設備的不合適的ioctl

我嘗試了多種選擇來解決此問題。 我使用了-t,-tt,-T選項,但是使用這些選項時,相同的錯誤仍然存​​在,或者終端無響應。

有人對此有解決方案嗎?

您沒有在遠程主機上運行find 您正在嘗試運行在遠程主機上登錄shell,並且只有退出之后會find運行。 此外,由於-n選項的緣故,遠程外殼程序由於其標准輸入從/dev/null重定向而失敗。

sshCmd="ssh -n -o ConnectTimeout=5 -o Batchmode=yes -o StrictHostKeyChecking=no -o CheckHostIP=no -o PasswordAuthentication=no -q"

while IFS=: read -r f1 f2 f3 f4 ; do   
  # Beware of values for f2, f3, and f4 containing double quotes themselves.     
  $sshCmd "$f1" "find \"$f2\" -type f -name \"$f4\" -mtime +\"$f3\""
done

無關,但sshCmd應該是一個函數 ,而不是要擴展的變量。

sshCmd () {
  ssh -n -o ConnectTimeout=5 -o Batchmode=yes -o StrictHostKeyChecking=no -o CheckHostIP=no -q "$@"
}

while IFS=: read -r f1 f2 f3 f4; do
   sshCmd "$f1" "find \"$f2\" -type f -name \"$f4\" -mtime +\"$f3\""
done

次要補充:避免一些反斜杠的悲傷:

while IFS=: read -r f1 f2 f3 f4; do
   sshCmd "$f1" "find '$f2' -type f -name '$f4' -mtime +'$f3'"
done

雙引號內的所有內容都將在命令處理之前進行擴展,因此值將被擴展並位於單引號內,以供遠程主機處理; 即,遠程主機將看到值,而不是變量名。

(關於f1,f2,f3,f4包含單引號的可能性適用標准免責聲明。)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM