繁体   English   中英

bash将远程计算机的输出捕获到变量中

[英]bash capture output from remote machine into a variable

我正在尝试使用cli命令然后使用grep 717GE捕获多个远程设备的输出,并打印到多个主机的屏幕。 正如您在下面看到的,它从$ hosts捕获IP并将其传递给$ outip,而不是从命令行使用相同命令时显示的内容。 我认为该变量将捕获从给定命令返回的数据。 有人可以帮助我,帮助我了解我做错了什么吗? 我对学习真的很感兴趣,因此请不要发表过时的评论。 如果可能的话。

for host in ${hosts[@]}; do
  seven=( $(cli $hosts show gpon ont summary -n --max=3 --host ) )
  outip=( $(grep 717GE $seven) )
  echo $outip
done

输出:

+ for host in '${hosts[@]}'
+ seven=($(cli $hosts show gpon ont summary -n --max=3 --host ))
++ cli 10.100.112.2 show gpon ont summary -n --max=3 --host
+ outip=($(grep 717GE $seven))
++ grep 717GE 10.100.112.2 grep: 10.100.112.2: No such file or directory

除非要创建数组,否则不要使用var=( .. ) ,并使用bash here-string grep something <<< "$var" (相当于echo "$var" | grep something )来搜索匹配项行(否则,您说的是$var包含要搜索的文件名列表和一些为grep设置的选项):

for host in ${hosts[@]}; do
  # Assign as variable rather than array, and use $host instead of hosts
  seven=$(cli $host show gpon ont summary -n --max=3 --host )
  # Grep with "$seven" as text input and not as a list of filenames
  outip=$(grep 717GE <<< "$seven")
  echo "$outip"
done

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM