带有expect模块的Shell脚本通过控制台备份路由器的运行配置

时间:2014-02-08 19:10:01

标签: shell expect

我是Linux爱好者,最近尝试使用shell脚本。

#!/usr/bin/expect-f
cd /home/vuser/Net_backup
now=$(date +"%m_%d_%y")
touch  /home/vuser/Net_backup/ASR$now.txt
sleep 5s
set Password "prizer"
set password "banger"
set Trial   "32"
set bye   "exit"
set quitconsole "q"
set exitcons "logout"
spawn telnet xx.xx.xx.xx
expect "password:"
send "$password\n"
expect "admin>"
send "$Trial\n"
sleep 5s
expect "ASR1001>"
send "enable\n"
expect "Password:"
send "$Password\n"
expect "ASR1001#"
send "terminal length 0\n"
expect "ASR1001#"
send "show run\n" | tee /home/vuser/Net_backup/ASRnow.txt
expect "ASR1001#"
send "$bye\n"
sleep 5s
xdotool key ctrl+]
sleep 5s
send "$quitconsole\n"
expect "admin >"
send "$exitcons\n"
sleep 5s
exit

我打算编写一个shell脚本来执行以下功能。

(1)在特定文件夹中创建文件 (2)将系统日期附加到文件名中,格式为“”%m_%d_%y“” (3)登录控制台服务器 (4)访问控制台服务器上的特定端口以登录路由器 (5)转到路由器上的超级用户提示符 (6)将终端长度设置为0,否定需要回车输入 (7)在路由器上运行命令“show run”并将此输出捕获到创建的文件中     第2步) (8)退出路由器命令提示符 (9)从控制台服务器退出(这需要三个步骤首先发送击键(ctrl +])然后
   命令q或退出,最后注销回到linux提示符) (10)退出剧本。

我已经写了上面给出的脚本..

然而,我的脚本卡在控制台服务器上并且没有向前移动,在我从控制台服务器手动注销后脚本继续运行但是没有达到预期的输出

感谢你的帮助。

谢谢 阿维纳什

4 个答案:

答案 0 :(得分:1)

在开发expect脚本时,您需要启用调试输出。始终将其添加到顶部附近:exp_internal 1

这是错误的:#!/usr/bin/expect-f - 在选项之前需要一个空格。

期待不是贝壳。这些行需要更改:

now=$(date +"%m_%d_%y")
touch  /home/vuser/Net_backup/ASR$now.txt
sleep 5s
send "show run\n" | tee /home/vuser/Net_backup/ASRnow.txt

使用\r代替\n为您的发送命令“点击输入”。

如果您的期望模式是正确的,则无需睡觉。

这是一个快速未经测试的重写

#!/usr/bin/expect -f
cd /home/vuser/Net_backup
set now [clock format [clock seconds] "%m_%d_%y"]
exec touch ASR$now.txt

set Password "prizer"
set password "banger"
set Trial   "32"
set bye   "exit"
set quitconsole "q"
set exitcons "logout"

spawn telnet 192.168.95.96
expect "password:"
send "$password\r"
expect "admin>"
send "$Trial\r"
expect "ASR1001>"
send "enable\r"
expect "Password:"
send "$Password\r"
expect "ASR1001#"
send "terminal length 0\r"
expect "ASR1001#"
send "show run\r"
expect -re "(.*)ASR1001#"
set fh [open ASR$now.txt w]
puts $fh $expect_out(1,string)
close $fh

send "$bye\r"
sleep 5
send "^]"     ;# here, in your editor, enter a literal ctrl-]
sleep 5
send "$quitconsole\r"
expect "admin >"
send "$exitcons\r"
expect eof

答案 1 :(得分:0)

#! /usr/bin/expect -f
set systemTime [clock seconds]
set now [clock format $systemTime -format  "%m_%d_%y"]
exec touch ASR$now.txt
set Password "prizer"
set password "banger"
set bye "exit"
set quitconsole "q"
set exitcons "logout"
spawn  ssh admin@192.168.95.96
expect "password:"
send "$password\r"
sleep 5
expect "admin>"
send "32\r"
sleep 5
send "\r"
expect "ASR1001>"
send "enable\r"
expect "Password:"
send "$Password\r"
expect "ASR1001#"
send "terminal length 0\r"
expect "ASR1001#"
send "show run \r"
set output [open "ASR$now.txt" "w"] <<<<<<<<<<<<<<< Fails here 
send "$bye\r"
sleep 5
send "ctrl-]"
sleep 5
send "$quitconsole\r"
expect "admin>"
send "$exitcons\r"
expect eof

答案 2 :(得分:0)

期望脚本与bash脚本完全不同。你不能在expect内部使用bash命令。

即,您不能在期望脚本中使用tee,cp,mv,rm等基本命令。

但您可以在本地计算机中配置tftp并在expect脚本中使用以下命令。

send "copy running-config tftp://$tftp/$config\n\n"
expect "$tftp"
send "\n"
  

脚本应该像,

#!/usr/bin/expect-f
cd /home/vuser/Net_backup
now=$(date +"%m_%d_%y")
touch  /home/vuser/Net_backup/ASR$now.txt
sleep 5s
set Password "prizer"
set password "banger"
set Trial   "32"
set bye   "exit"
set quitconsole "q"
set exitcons "logout"
spawn telnet xx.xx.xx.xx
expect "password:"
send "$password\n"
expect "admin>"
send "$Trial\n"
sleep 5s
expect "ASR1001>"
send "enable\n"
expect "Password:"
send "$Password\n"
expect "ASR1001#"
send "terminal length 0\n"
expect "ASR1001#"
send "copy running-config tftp://$tftp/$config\n\n" #edit this line wrt your local machine's configurations
expect "$tftp"
send "\n"
expect "ASR1001#"
send "$bye\n"
sleep 5s
xdotool key ctrl+]
sleep 5s
send "$quitconsole\n"
expect "admin >"
send "$exitcons\n"
sleep 5s
exit

答案 3 :(得分:0)

这是一个可行的解决方案。我已经使用连接到我的Debian 9 Stretch VM的一台Cisco设备对它进行了测试。

#!/usr/bin/expect -f
cd /var/www/html/Netbackup
set systemTime [clock seconds]
set now [clock format $systemTime -format  "%m_%d_%y"]
exec touch R1$now.txt
sleep 1
spawn telnet 192.168.10.100
sleep 1
expect "Username:"
send "cisco\n"
sleep 1
expect "Password:"
send "cisco\n"
sleep 1
expect "R1"
send "terminal length 0\n"
expect "R1#"
send "show run\n"
sleep 2
expect -re "(.*)R1#"
set fh [open /var/www/html/Netbackup/R1$now.txt w]
puts $fh $expect_out(1,string)
close $fh
sleep 1
xdotool key ctrl+]
sleep 1
send "$quitconsole\n"
sleep 1
send "$exitcons\n"
sleep 1
exit

下面是输出

root@lampserver:~/Desktop# ./Netbackup.sh
spawn telnet 192.168.10.100
Trying 192.168.10.100...
Connected to 192.168.10.100.
Escape character is '^]'.


User Access Verification

Username: cisco
Password:
R1#terminal length 0
R1#show run
Building configuration...

Current configuration : 2325 bytes
!
............
Output Omitted
............
R1#can't read "bye": no such variable
    while executing
"send "$bye\n""
    (file "./Netbackup.sh" line 24)
root@lampserver:~/Desktop#
root@lampserver:~/Desktop# cd
root@lampserver:~# cd /var/www/html/Netbackup
root@lampserver:/var/www/html/Netbackup# ls
R102_14_19.txt
root@lampserver:/var/www/html/Netbackup#
root@lampserver:/var/www/html/Netbackup# ls -al
-rw-r--r-- 1 root     root     2583 Feb 14 17:49 R102_14_19.txt
root@lampserver:/var/www/html/Netbackup#