bash 中的 SSH while 循环。不会分配伪终端,因为标准输入不是终端

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

我正在尝试将文件循环到 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"

在某些服务器上我收到以下错误:

不会分配伪终端,因为标准输入不是终端。 stty:标准输入:设备的不适当 ioctl

我已经尝试了多种方法来解决这个问题。我使用了 -t、-tt、-T 选项,但是当使用这些选项时,同样的错误仍然存​​在,或者终端变得无响应。

有人对此有解决方案吗?

您没有 运行宁 find 在远程主机上;您正在尝试 运行 在远程主机上登录 shell,只有在退出之后才会 find 运行。此外,远程 shell 失败,因为它的标准输入由于 -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 包含单引号的可能性...)