【问题标题】:Calling ssh in a loop is destroying the loop variable context (ksh93 on AIX 7.1)在循环中调用 ssh 会破坏循环变量上下文(AIX 7.1 上的 ksh93)
【发布时间】:2018-06-18 21:57:45
【问题描述】:

我发布这个以防其他人遇到这个问题。我会向 IBM 立案,如果他们有更新,我会报告。

我在使用文件时在 AIX 7.1(但不是 6.1)上遇到问题 驱动循环(例如:cat myfile.txt | while read myvar)。调用 ssh from inside 导致循环在第一次迭代时提前退出。

在我的真实示例中,我没有使用“ls”来填充文件,但在这里这样做更容易演示。

我有以下代码:

#!/usr/bin/ksh93
ssh LESAUN01 -l root "ls -1 c*" > file.list
echo "------------------------------------------"
cat file.list
echo "------------------------------------------"
echo ""
echo "Loop 1"
echo ""
cat file.list | while read file1
do
   echo "    File: $file1"
done
echo ""
echo "Loop 2"
echo ""
cat file.list | while read file2
do
   echo "    File: $file2"
   ssh LESAUN01 -l root "ls -l $file2"
done
echo ""
echo "Done"
exit

当我从 AIX 6.1 lpar 运行它时,我得到了这些预期的结果

-----------------------------------------
client.txt
customer_handover.log
------------------------------------------

Loop 1

    File: client.txt
    File: customer_handover.log

Loop 2

    File: client.txt
-rw-r--r-- 1 root root 91323 Feb 12  2015 client.txt
    File: customer_handover.log
-rw------- 1 root root 27533 Aug 31 18:04 customer_handover.log

Done

在 7.1 上运行时,我得到以下结果:

------------------------------------------
client.txt
customer_handover.log
------------------------------------------

Loop 1

    File: client.txt
    File: customer_handover.log

Loop 2

    File: client.txt
-rw-r--r-- 1 root root 91323 Feb 12  2015 client.txt

Done

我的解决方案是从数组而不是文件(在将文件加载到数组之后)提供循环,这可以按预期工作。

这适用于 6.1 和 7.1

#!/usr/bin/ksh93

ssh LESAUN01 -l root "ls -1 c*" > file.list
echo "------------------------------------------"
cat file.list
echo "------------------------------------------"
echo ""
echo "Loop"
echo ""
i=0
set -A file_array
cat file.list | while read line
do
    file_array[ $i ]="$line"
    (( i++ ))
done
for x in "${!file_array[@]}"
do
   echo "    File: ${file_array[$x]}"
   ssh LESAUN01 -l root "ls -l ${file_array[$x]}"
done
echo ""
echo "Done"
exit

这给出了预期的结果。

------------------------------------------
client.txt
customer_handover.log
------------------------------------------

Loop

    File: client.txt
-rw-r--r-- 1 root root 91323 Feb 12  2015 client.txt
    File: customer_handover.log
-rw------- 1 root root 27533 Aug 31 18:04 customer_handover.log

Done

【问题讨论】:

  • 首先,您可以将选项移到主机名之前:´ssh -l root LEASUN01 ...´ 或简单地´ssh root@LEASUN01 ...´

标签: ksh aix


【解决方案1】:
cat file.list | while read file2
do
   echo "    File: $file2"
   ssh LESAUN01 -l root "ls -l $file2"
done

cat file.list 的输出是循环内每个命令的标准输入。 ssh 从其标准输入中读取,以便将流中继到远程进程,因此它正在使用 cat 输出。

一个简单的解决方法是重定向 ssh 的标准输入:

cat file.list | while read file2
do
   echo "    File: $file2"
   ssh LESAUN01 -l root "ls -l $file2" < /dev/null
done

【讨论】:

  • "ssh -n" 如果 stdin 也执行重定向,其中任何一个都使它像我预期的那样工作。那么,在 6.1 中不消耗输入,但在 7.1 中确实如此,这是否意味着他们在 7.1 中修复了 6.1 中的错误? :-)
  • 我认为 ssh 程序的行为在这方面没有改变。我的猜测是,旧版本的 ksh 可能正在缓冲 read 命令的输入。第一次调用 read 可能已经消耗了来自 cat 的全部输出以填充缓冲区。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-19
  • 2016-02-05
相关资源
最近更新 更多