bash - 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 选项,但是当使用这些选项时,相同的错误仍然存在,或者终端变得无响应。
有人对此有解决方案吗?
解决方案
您没有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
推荐阅读
- python - 将 PHP 请求转换为 Python
- vba - VBA 的 selenium webdriver 中的过时元素引用异常
- .net - 在 VB.NET 中上传 FileNet 文件
- java - UI Automator java虚拟机Mac上的错误
- mongodb - 通过 mongo-go-driver 使用 mongodb 本机函数
- javascript - Angular 移动依赖服务
- python - 读取文本文件并在 python 中仅显示其中的一部分
- amazon-web-services - Terraform-AWS IAM 角色仅在尚未附加托管策略时将其附加到角色
- javascript - 如何将插入符号放置在其中没有文本的 span 元素中?
- ssl - Ingress Nginx 中的 TCP 公开服务与 ssl 一起使用吗?