首页 > 解决方案 > bash 中的 SSH while 循环。不会分配伪终端,因为 stdin 不是终端

问题描述

我正在尝试将文件循环到 ssh 到服务器列表,并在这些服务器上为某些日志文件执行查找命令。我知道 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 选项,但是当使用这些选项时,相同的错误仍然存​​在,或者终端变得无响应。

有人对此有解决方案吗?

标签: bashwhile-loopptystty

解决方案


您没有find在远程主机上运行;您正在尝试在远程主机上运行登录 shell,并且只有在退出之后才会find运行。此外,远程 shell 失败,因为它的标准输入/dev/null由于该-n选项而被重定向。

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

推荐阅读