我的bash脚本问题

时间:2013-06-27 09:19:33

标签: bash passwords

我对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

2 个答案:

答案 0 :(得分:4)

脚本的一般结构应为:

while read office ip user1 password1 user2 password2
do
  /usr/bin/expect <<EOF
  ...
EOF
done < TEST.txt

答案 1 :(得分:0)

按照@ Barmar的建议。此外:

  • for p in `cat TEST.txt | awk '{print $2}'`无效期待
  • 使用here-docs时,终止字EOF必须没有任何前导空格。