我正在尝试创建一个循环,用于存储列表中每个主机的ssh命令的输出。
守则:
#!/bin/bash
while read i; do
ip=$(echo "$i" | awk -F "\"*,\"*" '{print $2}') #(file contains name,ip values)
if echo "$i" | grep -q "ARISTA"; then
results=$(sshpass -f/root/cred ssh user@$ip "show running-config")
echo "$results"
elif echo "$i" | grep -q "NEXUS"; then
results=$(sshpass -f/root/cred ssh user@$ip "show running-config")
echo "$results"
elif echo "$i" | grep -q "ARUBA"; then
results=$(sshpass -f/root/cred ssh user@$ip "show running-config")
echo "$results"
elif echo "$i" | grep -q "R1"; then
results=$(sshpass -f/root/cred ssh user@$ip "show configuration | display set")
echo "$results"
elif echo "$i" | grep -q "HP"; then
results=$(sshpass -f/root/cred ssh user@$ip "display current-configuration")
echo "$results"
else
echo "$i not found"
fi
done </root/hosts.txt我得到的输出是列表中的第一个主机的结果-- only。我怀疑问题是sshpass,因为当我尝试一个不同的语句时,我得到了准确的结果,比如:
#!/bin/bash
while read i; do
ip=$(echo "$i" | awk -F "\"*,\"*" '{print $2}') #(file contains name,ip values)
if echo "$i" | grep -q "ARISTA"; then
echo "$ip = Arista"
elif echo "$i" | grep -q "NEXUS"; then
echo "$ip = Nexus"
elif echo "$i" | grep -q "ARUBA"; then
echo "$ip = Aruba"
elif echo "$i" | grep -q "R1"; then
echo "$ip = R1"
elif echo "$i" | grep -q "HP"; then
echo "$ip = HP"
else
echo "$i not found"
fi
done </root/hosts.txt然而,在执行第一个sshpass命令之后,循环就中断了。
有什么想法吗?
发布于 2022-10-20 06:03:44
好吧那么。
这就是解决办法:
#!/bin/bash
while read i; do
ip=$(echo "$i" | awk -F "\"*,\"*" '{print $2}') #(file contains name,ip values)
if echo "$i" | grep -q "ARISTA"; then
results=$(sshpass -f/root/cred ssh -n user@$ip "show running-config")
echo "$results"
elif echo "$i" | grep -q "NEXUS"; then
results=$(sshpass -f/root/cred ssh -n user@$ip "show running-config")
echo "$results"
elif echo "$i" | grep -q "ARUBA"; then
results=$(sshpass -f/root/cred ssh -n user@$ip "show running-config")
echo "$results"
elif echo "$i" | grep -q "R1"; then
results=$(sshpass -f/root/cred ssh -n user@$ip "show configuration | display set")
echo "$results"
elif echo "$i" | grep -q "HP"; then
results=$(sshpass -f/root/cred ssh -n user@$ip "display current-configuration")
echo "$results"
else
echo "$i not found"
fi
done </root/hosts.txt是Jetchisel建议的,ssh正在吃STDIN。因此,解决方案是在ssh命令中添加标志
-n,该命令将输出传递给/dev/null.
。
非常感谢的伙计们,祝你们好运。
https://stackoverflow.com/questions/74127229
复制相似问题