首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当读取循环破坏Bash时,sshpass

当读取循环破坏Bash时,sshpass
EN

Stack Overflow用户
提问于 2022-10-19 14:33:05
回答 1查看 30关注 0票数 0

我正在尝试创建一个循环,用于存储列表中每个主机的ssh命令的输出。

守则:

代码语言:javascript
复制
#!/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,因为当我尝试一个不同的语句时,我得到了准确的结果,比如:

代码语言:javascript
复制
#!/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命令之后,循环就中断了。

有什么想法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-10-20 06:03:44

好吧那么。

这就是解决办法:

代码语言:javascript
复制
#!/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.

非常感谢的伙计们,祝你们好运。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74127229

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档