如何在expect脚本中使用带有2列的CSV作为输入来设置主机名

如何在expect脚本中使用带有2列的CSV作为输入来设置主机名,csv,expect,Csv,Expect,我有一个CSV,有两列,格式为“IP地址;主机名”。 我用这张纸条打开一个主机文件,登录到交换机/路由器,并将配置备份到tftp服务器 现在我需要调整脚本以打开csv,以便在交换机上设置主机名。我唯一需要知道的是如何打开CSV并从每行中获取两个变量:IP地址和主机名。其余的都没问题 #! /bin/expect set timeout 20 set hostnamefile [lindex $argv 0] set tftpip [lindex $argv 1]

我有一个CSV,有两列,格式为“IP地址;主机名”。 我用这张纸条打开一个主机文件,登录到交换机/路由器,并将配置备份到tftp服务器

现在我需要调整脚本以打开csv,以便在交换机上设置主机名。我唯一需要知道的是如何打开CSV并从每行中获取两个变量:IP地址和主机名。其余的都没问题

    #! /bin/expect

    set timeout 20
    set hostnamefile [lindex $argv 0]
    set tftpip [lindex $argv 1]
    set user [lindex $argv 2]
    set password [lindex $argv 3]
    set prompt "*#"

    set send_slow {10 .001}
    log_user 0

    if {[llength $argv] == 0} {
      send_user "Usage: scriptname hostnamefile \'TFTPserver IP\' username         \'userpassword\'\n"
      exit 1
    }

    ;# -- main activity

    proc dostuff { currenthost {tftpserver 1} } {

       ;# do something with currenthost

       send_user "\n#####\n# $currenthost\n#####\n"

       send -- "copy running-config tftp:\r"
       expect "Address or name of remote host []?"
       send "$tftpserver\r"
       expect "Destination filename*"
       send "\r"
       expect {
           "*bytes copied*" {
           send_user "TFTP copy succeeded\n"
           }
           "*Error*"{
           send_user "TFTP copy failed\n"
           }
       }
       send "\r"
       return
    }

    ;# -- start of task

    set fd [open ./$hostnamefile r]
    set hosts [read -nonewline $fd]
    close $fd


    foreach host [split $hosts "\n" ] {

        spawn /usr/bin/ssh $user@$host

        while (1) {
            expect {

            "no)? " {
               send -- "yes\r"
            }

            "*assword*:" {
                send -- "$password\r"
            }

            "*>" {
              send "enable\r"
            }

            "$prompt" {
               dostuff $host $tftpip
               break
            }
          }
       }

       expect "$prompt"
       send -- "exit\r"

    }

    expect eof

我设法围绕实现该技巧的expect脚本构建了一个bash脚本。如果有人有更好的办法,请告诉我

#!/bin/bash
input=./list.csv
oldifs=$ifs
ifs=,
[ ! -f $input ] && { echo "$input file not found"; exit 99; }

if [ $# -ne 2 ]
then
    echo "Usage: scriptname username password"
    exit 1
fi

username=$1
password=$2

while read line
do
eval $(echo "$line" | awk -F';' '{print "ip="$1";hostname="$2}')
#echo $ip
#echo $hostname

/usr/bin/expect - << EndMark
set prompt "*#"
set cprompt "*(config)#"

spawn /usr/bin/ssh $username@$ip

while (1) {
   expect {
       "no)? " {
          send -- "yes\r"
       }
       "*assword*:" {
          send -- "$password\r"
       }
       "*>" {
          send "enable\r"
       }
       "$prompt" {
          break
       }
   }
}
sleep 2

send -- "conf t\r"
expect "$cprompt"
send -- "hostname $hostname\r"
expect "$cprompt"
send -- "end\r"
expect "$prompt"
send -- "copy running-config startup-config\r"
expect "Destination filename*"
send -- "\r"
expect "$prompt"
send -- "exit\r"

send_user "\n#####\n# hostname $hostname\n# set on $ip\n#####\n"
send -- "exit\r"

expect eof

EndMark

done < $input
ifs=$oldifs
#/bin/bash
输入=./list.csv
oldifs=$ifs
如果=,
[!-f$input]&&&{echo“$input文件未找到”;退出99;}
如果[$#-ne 2]
然后
echo“用法:scriptname用户名密码”
出口1
fi
用户名=$1
密码=$2
读行时
做
eval$(echo“$line”| awk-F'”{print”ip=“$1”;hostname=“$2}”)
#echo$ip
#echo$hostname

/usr/bin/expect-一年后。。。但它仍然可能有用

#! /bin/expect

set f [open "list.csv"] #associate your csv to a var
set lines [split [read $f] "\n"] #build a list named "lines": each list item is a line

# create a while loop until "x" reach the length of your list -1
set x 0
while {$x<[expr [llength $lines] -1]} {   
    set line [lindex $lines $x]           
    regexp (.*);(.*) $line match_regex ip host 
    puts $match_regex
    puts $ip
    puts $host
    incr x
}


# set line... # var "line" will be at each cycle an item of your list
# regex...    # split var "line" into 3 vars: 
# "match_regex" will match all the string
# "ip" will match regex included in first "(.*)" 
# "host" regex in the second "(.*)"
#/bin/expect
设置f[打开“list.csv”]#将您的csv与var关联
设置行[拆分[读取$f]“\n”]#创建一个名为“行”的列表:每个列表项都是一行
#创建一个while循环,直到“x”达到列表的长度-1
集x0

当{$xis
hostnamefile
您询问的2列CSV文件时?是的。现在我有一列带有IP地址。但是我想添加第二列带有主机名,这样我可以在交换机/路由器上设置主机名。稍后添加更多列,这样我还可以为每个主机设置snmp设置。