0

我对 bash 很陌生。我正在尝试实现一件非常简单的事情,但它看起来非常复杂......

我正在尝试从文件中读取变量(IP 地址、用户名、密码)并连接到防火墙以更改它们。

该文件大约有 100 行,我正在尝试逐一读取,从第 2 列选择防火墙的 IP 地址,第一个用户名并从第 3 和 4 列传递,第二个用户名从第 5 和第 6 列传递柱子。该文件如下所示:

Office1    IP   uname1 pass1       uname2  pass2
Office2    IP   uname1 pass1       uname2  pass2

所有的原始数据都由制表符分隔脚本看起来像这样:

   #!/bin/bash
   office=`awk '{print $1}' TEST.txt`
   user1=`awk '{print $3}' TEST.txt`
   user2=`awk '{print $5}' TEST.txt`
   password1=`awk '{print $4}' TEST.txt`
   password2=`awk '{print $6}' TEST.txt`
   /usr/bin/expect <<EOF 
   for p in `cat TEST.txt | awk '{print $2}'`
   do
   # echo $user1 ", " $password1", " $user2", "$password2"
   set timeout 20
   spawn telnet $p
   expect "login:"
   send "cipollone"
   send \r
   expect "password:"
   send "mypass"
   send \r
   expect ">"
   send "set admin user $user1 password $password1 privilege all"
   send \r
   expect ">"
   send "set admin user $user2 password $password2 privilege all"
   send \r
   expect ">"
   send "save"
   send \r
   expect ">"
   send "exit"
   send \r
   send \r
   send \r
   # echo $office $p "DONE"
   # echo \r
   # echo $office $p "DONE" >>  results.txt 
   # echo \r >> results.txt
   exit
   EOF
   done < TEST.txt
4

2 回答 2

4

您的脚本的一般结构应该是:

while read office ip user1 password1 user2 password2
do
  /usr/bin/expect <<EOF
  ...
EOF
done < TEST.txt
于 2013-06-27T09:31:54.160 回答
0

遵循@Barmar 的建议。此外:

  • for p in `cat TEST.txt | awk '{print $2}'`无效期望
  • 使用 here-docs 时,终止词 EOF 必须出现没有任何前导空格。
于 2013-06-27T10:26:05.123 回答