我的while read命令没有打开一个while循环。

huangapple go评论57阅读模式
英文:

My while read command isn't opening a while loop

问题

我正在尝试使用while read命令创建一个逗号分隔的名称、路由器和IP地址表。然后,它应该通过一系列的路由器使用router1的值进行SSH连接,输入一个命令并提取应返回的IP地址,然后将它们写入一个文件。据我了解,我可以使用read来实现这个目标。

grep -v '^#' /opt/pp1/projectprefixed/peeringinfo > /opt/pp1/projectprefixed/$DATE/peeringinfo.conf

ip_regex='([0-9]{1,3}\.){3}[0-9]{1,3}'

while IFS=, read NREN router1 peering
do
  if [ ! -f /opt/pp1/projectprefixed/$DATE/recFromNRENList/$router1 ] 
    then
    ssh -q -n -o StrictHostKeyChecking=no srv_oc_scripts@$router1 "show route recieve-protocol bgp" | grep -o "$ip_regex" > peeringsReceived
  fi
done < $router1

表格是这样的文本:

NAME1,mx1.ROUTER1,192.168.1.1
NAME2,mx1.ROUTER2,192.168.65.7

但是,它返回了一个"ambiguous redirect"错误。

英文:

I'm trying to use the while read command to create a comma separated table of names, routers, and IP addresses. It should then SSH through that series of routers with the router1 value, input a command, and extract the IP addresses it should return, then write them to a file. It is my understanding that I can do this with read.


 grep -v &#39;^#&#39; /opt/pp1/projectprefixed/peeringinfo &gt; /opt/pp1/projectprefixed/$DATE/peeringinfo.conf

ip_regex=&#39;([0-9]{1,3}\.){3}[0-9]{1,3}&#39;

while IFS=, read NREN router1 peering
do

  if [ ! -f /opt/pp1/projectprefixed/$DATE/recFromNRENList/$router1 ] 
    then
    ssh -q -n -o StrictHostKeyChecking=no srv_oc_scripts@$router1 &quot;show route recieve-protocol bgp&quot; | grep -o &quot;$ip_regex&quot; &gt; peeringsReceived
  fi

done &lt; $router1

The table is a text like that looks like this:

NAME1,mx1.ROUTER1,192.168.1.1
NAME2,mx1.ROUTER2,192.168.65.7

However, it returns an "ambiguous redirect" error.

答案1

得分: 1

以下是翻译好的内容:

有几个主要的逻辑错误:

  1. ssh 将会占用标准输入(stdin)。从不同的文件描述符读取

    while IFS= read -r NREN router1 peering <&3; do
      ...
    done 3< 4router1
    
  2. router1 似乎在 read 命令中初始化,但在循环之前用作输入文件,用作 ssh 远程主机,用作你测试存在性的文件名,以及用作循环命令的输出文件。我无法确定这个文件应该如何存在,以及应该包含什么内容。

  3. grep 也会占用标准输入(stdin):你可能忘记了为 grep 指定所需的输入。

我想要 ssh 进入路由器,运行指定的命令并提取匹配的模式,然后将其写入文件。它应该是一个 IP 地址。

作为一个尝试,也许你想要这样:

ip_regex='([0-9]{1,3}\.){3}[0-9]{1,3}'
output_dir="/opt/pp1/projectprefixed/$DATE/recFromNRENList"
router1=???

ip_addr=$(
    ssh -q -n -o StrictHostKeyChecking=no \
        "srv_oc_scripts@$router1" \
        "show route recieve-protocol bgp" \
    | grep -o "$ip_regex"
)
英文:

There are several major logic errors

  1. ssh will consume stdin. Read from a different file descriptor
    while IFS= read -r NREN router1 peering &lt;&amp;3; do
      ...
    done 3&lt; 4router1
    
  2. router1 appears to be initialized in the read command, but is used as an input file to the loop beforehand, and as the ssh remote host, and as a file name you test for existence, and as the output file for the loop commands. I can't tell how this file is supposed to exist, and what contents it's supposed to have
  3. grep will also consume stdin: You probably forgot to specify what input you want for grep

> I want to ssh into a router, run the specified command and pull only the matching pattern and write it to a file. It's meant to be an IP address

As a wile stab in the dark, perhaps you want

ip_regex=&#39;([0-9]{1,3}\.){3}[0-9]{1,3}&#39;
output_dir=&quot;/opt/pp1/projectprefixed/$DATE/recFromNRENList&quot;
router1=???

ip_addr=$(
    ssh -q -n -o StrictHostKeyChecking=no \
        &quot;srv_oc_scripts@$router1&quot; \
        &quot;show route recieve-protocol bgp&quot; \
    | grep -o &quot;$ip_regex&quot;
)


</details>



# 答案2
**得分**: 0

重定向在执行任何命令之前进行设置。
这意味着发送到 while 循环的输入重定向永远不会更新,并且始终读取 "router1" 的初始值作为输入。

在简化的代码形式中,我们在这里创建两个文件,然后在 while 循环中读取 `foo` 并尝试在运行中更改 while 循环的输入文件。

```bash
#!/usr/bin/env bash
echo $'bar\nbaz' > foo
echo $'qux' > bar
f=foo;i=0
while read -r f; do echo $f; f=bar; done < "$f"
grep . *

这不起作用,因为 while 循环的输入被分配给与 foo 相连接的文件指针,您无法更改它。

错误的原因是什么?最有可能与 https://stackoverflow.com/questions/2462385/getting-an-ambiguous-redirect-error 有关。

最有可能的情况是,在 while 循环之前忘记为 router1 赋值,或者局部于 while 循环的变量 router1 为空。这与链接问题中提到的错误一致。

英文:

Redirection is set up before any command execution is done.
This implies that the input redirect send to the while-loop is never updated and will always read the initial value of "router1" as input.

In a simplified form of code, here we create two files, read foo in the while-loop and attempt to change the input file of the while-loop mids run.

#!/usr/bin/env bash
echo $&#39;bar\nbaz&#39; &gt; foo
echo $&#39;qux&#39; &gt; bar
f=foo;i=0
while read -r f; do echo $f; f=bar; done &lt; &quot;$f&quot;
grep . *

This does not work because the while loop's input is assigned to the file-pointer connected to foo and you cannot change that.

Where does the error come from? Most likely related to https://stackoverflow.com/questions/2462385/getting-an-ambiguous-redirect-error

Most likely, you forgot to assign router1 before the while-loop or the variable router1, which is local to the while-loop, is empty. This is in agreement with the errors mentioned in the linked question.

huangapple
  • 本文由 发表于 2023年7月17日 18:25:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76703537.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定