Tcl 如果一次登录失败,脚本将停止多个设备

Tcl 如果一次登录失败,脚本将停止多个设备,tcl,expect,Tcl,Expect,我正在尝试编写一个带有一些嵌入式Expect脚本的小bash脚本。我需要更改3500交换机的主机名。我有一个csv文件,其中包含我的ip地址、新主机名以及我的expect脚本。如果连接到交换机没有问题,脚本似乎运行得很好。如果我确实从交换机获得“超时”或“拒绝访问”,脚本将在退出时停止。我需要脚本转到下一个ip地址 我的自动登录确实使用了酸败的clogin 我对expect和bash是个新手,我在我最好的朋友“google”上搜索可能的答案,但找不到答案 脚本如下: 主机名.exp #!/usr

我正在尝试编写一个带有一些嵌入式Expect脚本的小bash脚本。我需要更改3500交换机的主机名。我有一个csv文件,其中包含我的ip地址、新主机名以及我的expect脚本。如果连接到交换机没有问题,脚本似乎运行得很好。如果我确实从交换机获得“超时”或“拒绝访问”,脚本将在退出时停止。我需要脚本转到下一个ip地址

我的自动登录确实使用了酸败的clogin

我对expect和bash是个新手,我在我最好的朋友“google”上搜索可能的答案,但找不到答案

脚本如下:

主机名.exp

#!/usr/bin/expect -f
# Set variables
set DATE [exec date +%F]
set timeout 10
# Log results
log_file -a hostnames-$DATE.log

# Let's go to configure mode

## Read the file
set fid [open ./hostnames.csv]
set content [read $fid]
close $fid

## Split into records on newlines
set records [split $content "\n"]

## Iterate over the records
foreach rec $records {

## Split into fields on comma
set fields [split $rec ","]

## Assign fields to variables and print some out...
lassign $fields\  hostname newname

puts "$hostname"
puts "$newname"

if {$hostname == ""} continue


# Announce which device we are working on and at what time
send_user "\n"
send_user ">>>>> Working on $hostname @ [exec date] <<<<<\r"
send_user "\n"

spawn clogin "$hostname\r"

expect {
    timeout  { send_user "\n Failed to get login prompt\n"; exit 1 }
    eof { send_user "\nSSH failure for hostname\n"; exit 1 }
    "*-> $"
    }

sleep 2


send "conf t\n"
expect "#"
send "hostname $newname\n"
expect "#"
send "exit\n"
expect "(config)#"
send "write mem\n"
expect "*#"
send "exit\n"
expect ":~\$" exit

# Announce which device we are working on and at what time
 send_user "\n"
 send_user ">>>>> Done working on $hostname @ [exec date] <<<<<\r"
 send_user "\n"

}
我将感谢任何帮助

多谢各位

嗨,迪内什

谢谢你的回复

我使用了您提供的第一个没有“句柄”部分的代码。如果登录失败,它现在将转到下一个ip地址,但是,它不会在良好的登录上运行我的命令。登录到172.16.1.2是唯一有效的连接。其他的是测试故障转移

$**expect hostnames.exp** 
10.10.1.1
newhostname1

>>>>> Working on 10.10.1.1 @ Wed Oct  1 11:59:09 SAST 2014 <<<<<
spawn clogin 10.10.1.1
10.10.1.1

in /home/*****/.cloginrc.0.1.1

SSH failure for hostname for 10.10.1.1
172.16.1.2
newhostname2

>>>>> Working on 172.16.1.2 @ Wed Oct  1 11:59:09 SAST 2014 <<<<<
spawn clogin 172.16.1.2
172.16.1.2
spawn telnet 172.16.1.2
Trying 172.16.1.2...
.onnected to 172.16.1.2
Escape character is '^]'.

User Access Verification

Username: *****
Password: 
SR_Test_SW#
SR_Test_SW#
Failed to get login prompt for 172.16.1.2
192.168.45.150
newhostname3

>>>>> Working on 192.168.45.150 @ Wed Oct  1 11:59:11 SAST 2014 <<<<<
spawn clogin 192.168.45.150
192.168.45.150
spawn telnet 192.168.45.150
Trying 192.168.45.150...

Failed to get login prompt for 192.168.45.150
$
$**主机名除外。exp**
10.10.1.1
新主机名1

>>>>>10.10.1.1@Wed Oct 1 11:59:09 SAST 2014>172.16.1.2@Wed Oct 1 11:59:09 SAST 2014>192.168.45.150@Wed Oct 1 11:59:11 SAST 2014只需在expect for
超时下使用
continue
eof
,而不是
退出1

expect {
    timeout  { 
              send_user "\n Failed to get login prompt for the switch $hostname\n"  
              continue 
             }
    eof { 
          send_user "\nSSH failure for hostname for the switch $hostname\n"
          continue 
        }
    "*-> $"
}
下面是一个示例说明

#!/usr/bin/expect
set timeout 3
send_user "we are waiting for the word 'hi' \n"

#Looping for 10 times
foreach x { 1 2 3 4 5 6 7 8 9 10} {
        puts "Is anybody there???"
        expect  {
                timeout {
                        puts "Nobody responding to me at trial $x"
                        puts "I'm waiting"
                        #Proceeding with the next element of the loop, using 'continue'
                        continue
                }
                #Dont bother about this 'nocase' keyword. ;)
                -nocase "hi" { puts "Hey friend!" }
        }
}
由于您甚至在
expect
之前就已经生成了“clogin”,因此我们在这方面是安全的,不会造成任何问题。如果它无法到达,我们将继续产卵新的“clogin”

但是,很好的做法是优雅地关闭生成的进程。这可以安排为

# After this command execution, the variable 'handle' will hold 
# the spawn handle reference for the 'clogin' process
set handle [ spawn clogin "$hostname\r" ] 

# Your expect statements here - Just modifying it for our needs

expect {
        timeout  { 
                  send_user "\n Failed to get login prompt for the switch $hostname\n"  
                  # Closing the process gracefully 
                  close $handle
                  continue 
                 }
        eof { 
              send_user "\nSSH failure for hostname for the switch $hostname\n"
              close $handle
              continue 
            }
        "*-> $"
}

我正在搜索类似于此脚本的内容,只是它会在三分之一的时间内执行。那么,您将如何获取列表,将其拆分为三个文件/部分,然后在三个不同的会话中执行更改呢。因此完成得更快。

我对第一个代码部分进行了更改,并将“*->$”部分替换为“*#”{}现在似乎一切都好了。谢谢。@Evan van Zyl,是的,请更新“我的答案”评论部分中的错误输出。请查看此链接..:
# After this command execution, the variable 'handle' will hold 
# the spawn handle reference for the 'clogin' process
set handle [ spawn clogin "$hostname\r" ] 

# Your expect statements here - Just modifying it for our needs

expect {
        timeout  { 
                  send_user "\n Failed to get login prompt for the switch $hostname\n"  
                  # Closing the process gracefully 
                  close $handle
                  continue 
                 }
        eof { 
              send_user "\nSSH failure for hostname for the switch $hostname\n"
              close $handle
              continue 
            }
        "*-> $"
}