标准 Expect 有效,但 xargs 无效
我目前正在尝试在使用 snmpwalk 时改进我的脚本,并希望合并使用 xargs,因为这将帮助我同时从多个设备收集信息。采取一些小步骤,我使用以下代码创建了一个简单的期望文件:
#!/usr/bin/expect -f
set hostname [lindex $argv 0]
spawn snmpwalk $hostname
interact
从那里我创建了一个简单的 bash 脚本,用这里的代码测试它的功能:
#!/bin/bash
cfg=$(cat ./ips)
for c in $cfg
do
expect ./EXPECT/01.exp $c
done
这样做会产生正确的信息,但需要时间,因为它通过每个 IP,一个一次。
然后,我将上面的代码注释掉,并在此处创建了 xargs 版本:
#!/bin/bash
xargs -I ADDRESS -P4 expect ./EXPECT/01.exp ADDRESS < ./ips
运行 bash 脚本后,除了来自期望文件的命令回显之外,不会产生任何内容,没有其他操作。然后我查看了它,发现你可以添加一个“-noecho”开关,但这并没有解决任何问题。
然后我将生成命令更改为 exec 但发生了同样的问题,除了命令的回显之外没有显示任何内容。
有兴趣看看是什么问题可能导致此问题。
I am currently trying to improve my script when using snmpwalk and wanted to incorporate using xargs as this would help me gather information from multiple devices at the same time. Taking small steps I created a simple expect file with the following code:
#!/usr/bin/expect -f
set hostname [lindex $argv 0]
spawn snmpwalk $hostname
interact
From there I created a simple bash script to test the functionality of it with the code here:
#!/bin/bash
cfg=$(cat ./ips)
for c in $cfg
do
expect ./EXPECT/01.exp $c
done
Doing this produces the proper information but takes time as it goes through each IP, one at a time.
I then commented the above code out and created the xargs version here:
#!/bin/bash
xargs -I ADDRESS -P4 expect ./EXPECT/01.exp ADDRESS < ./ips
Once I run the bash script, nothing is produced other then the command from the expect file echoing with no other action. I then looked into it and saw that you can add a "-noecho" switch to it but this didn't fix anything.
I then changed the spawn command to exec but the same issue happens and nothing is shown other then an echo of the command.
Would be interested to see what issue could be causing this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
所以答案相当简单,而且有点尴尬。 Expect 文件需要某种类型的 Expect 语句才能正确运行。我只是简单地输入以下期望语法,无论它是否会看到这一点,它都工作得很好:
从这里开始,脚本按预期工作,并且开始一次运行 4 个 IP。
So the answer was rather simple and a bit embarrassing. The expect file needs some type of expect statement before it will run correctly. I simply put in the following expect syntax regardless if it would see this or not and it worked fine:
From here the script worked as expected and it began to run 4 IP's at a time.