我已经对此进行了相当多的调整,但我陷入了奇怪的输出流而没有结果。基本上,我试图在我们的扩展网络上找到具有特定密码和处理器的某些ssh设备。这是脚本:
#/bin/bash
for i in 54
do
for j in 1 13 14 15
do
out=$(expect -c "spawn /usr/bin/ssh [email protected].$i.$j cat /proc/cpuinfo | grep MyString
expect {
-re \".*Are.*.*yes.*no.*\" {
send \"yes\n\"
exp_continue
}
\"*?assword:*\" {
send \"mypasswd\"
send \"\n\"
exp_continue
}
}")
if [["$out" != ""]]
then
echo "10.2.$i.$j" >> rpiout.txt
fi
done
done
ssh命令本身可以正常工作。另外,期望脚本也可以正常工作。另外,如果我在“ if [[...]]”语句之前插入“ echo $ out”,则可以从SSH命令获得预期的输出。但是,尝试写入文件时,我将此输出输出到命令行,并且没有日志文件...:
./check.sh: line 19: [[spawn /usr/bin/ssh some_guy:@10.2.54.1 cat /proc/cpuinfo | grep MyString
some_guy:@10.2.54.1's password:
Permission denied, please try again.
some_guy:@10.2.54.1's password:
Permission denied, please try again.
some_guy:@10.2.54.1's password:
: No such file or directoryy,password).
./check.sh: line 19: [[spawn /usr/bin/ssh some_guy:@10.2.54.13 cat /proc/cpuinfo | grep MyString
some_guy:@10.2.54.13's password:
: No such file or directory
./check.sh: line 19: [[spawn /usr/bin/ssh some_guy:@10.2.54.14 cat /proc/cpuinfo | grep MyString
some_guy:@10.2.54.14's password:
: No such file or directory
: No such file or directoryawn /usr/bin/ssh some_guy:@10.2.54.15 cat /proc/cpuinfo | grep MyString
第一个要求输入密码3次的密码是正确的(因为它不是目标设备之一)。后两个是不存在的IP设备,但后两个应该返回肯定的结果。
请注意,在“错误”“ ./check.sh:第19行:[[spawn ...”中,第19行是以” if [[...”开头的行。
非常感谢让我摆脱困境的任何帮助!!
在bash[[
中,不仅是语法,而且是命令。像任何其他命令一样,它需要空格将其与参数分开。
不是
if [["$out" != ""]]
但
if [[ "$out" != "" ]]
或者
if [[ -n "$out" ]]
此外,由于预期相呼应,就像你在终端看到该命令的方式,这是不可能的输出将永远是空的。尝试这样的事情:
out=$( expect <<END
spawn -noecho /usr/bin/ssh [email protected].$i.$j sh -c {grep -q MyString /proc/cpuinfo || echo _NOT_FOUND_}
expect {
-re ".*Are.*.*yes.*no.*" {
send "yes\r"
exp_continue
}
"*?assword:*" {
send "mypasswd\r"
exp_continue
}
eof
}
END
)
if [[ $out == *_NOT_FOUND_* ]]; then
echo "MyString not found on host 10.2.$i.$j"
fi
_NOT_FOUND_
您在/ proc / cpuinfo中看不到的一些字符串在哪里
在-noecho
这里至关重要,除非您回显它,否则将$ _out中的“ _NOT_FOUND_”排除在外。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句