2013-09-30 79 views
6

我对新的期望和一般脚本很陌生。我正在尝试制作一些脚本,以便在拉动网络设备配置时让我的生活变得更轻松。我设法创建了一个基本的期望脚本来SSH到设备并保存配置。用于SSH的Bash/Expect脚本

我想对此进行扩展,并允许脚本连接到多个IP地址,而不是像我现在这样拥有一个IP地址。我有一个名为list.txt的文件,在单独的一行中有几个不同的IP地址与每个IP。

我需要做什么才能让期望脚本连接到这些IP地址中的每一个,并执行脚本中的其余任务?

这里是Expect脚本我到目前为止:

#!/usr/bin/expect -f 
#Tells interpreter where the expect program is located. This may need adjusting according to 
#your specific environment. Type ' which expect ' (without quotes) at a command prompt 
#to find where it is located on your system and adjust the following line accordingly. 
# 
# 
#Use the built in telnet program to connect to an IP and port number 
spawn ssh 192.168.1.4 -l admin 
# 
#The first thing we should see is a User Name prompt 
#expect "login as:" 
# 
#Send a valid username to the device 
#send "admin" 
# 
#The next thing we should see is a Password prompt 
expect "Password:" 
# 
#Send a vaild password to the device 
send "password\n" 
# 
#If the device automatically assigns us to a priviledged level after successful logon, 
#then we should be at an enable prompt 
expect "Last login:" 
# 
#Tell the device to turn off paging 
# 
#After each command issued at the enable prompt, we expect the enable prompt again to tell us the 
#command has executed and is ready for another command 
expect "[email protected]" 
# 
#Turn off the paging 
send "set cli pager off\n" 
# 
#Show us the running configuration on the screen 
send "show config running\n" 
# 
# Set the date. 
set date [timestamp -format %C%y%m%d] 
# 
#Test output sent to file with a timestamp on end 
#-noappend will create a new file if one already exists 
log_file -noappend /home/test.cfg$date 
# 
expect "[email protected]" 
# 
#Exit out of the network device 
send "exit\n" 
# 
#The interact command is part of the expect script, which tells the script to hand off control to the user. 
#This will allow you to continue to stay in the device for issuing future commands, instead of just closing 
#the session after finishing running all the commands.`enter code here` 
interact 

我需要这样一个bash脚本集成?如果是这样,是否可以读取list.txt文件的一行,将其用作IP /主机变量,然后读取下一个并重复?

+1

你可以配置你的设备使用ssh密钥吗?如果是的话,你根本不需要期待。 –

回答

2

我会做到这一点(未经测试):

#!/usr/bin/expect -f 

set logfile "/home/text.cfg[clock format [clock seconds] -format %Y%m%d]" 
close [open $logfile w]   ;# truncate the logfile if it exists 

set ip_file "list.txt" 
set fid [open $ip_file r] 

while {[gets $fid ip] != -1} { 

    spawn ssh $ip -l admin 
    expect "Password:" 
    send "password\r" 

    expect "[email protected]" 
    send "set cli pager off\r" 

    log_file $logfile 
    send "show config running\r" 

    expect "[email protected]" 
    log_file 

    send "exit\r" 
    expect eof 

} 
close $fid 

注:

  • 我删除了简洁的所有评论
  • 使用\r模拟按回车键,当你send命令。
  • 我以为你只是想记录“显示配置运行”输出
  • 使用expect eof后发送“退出”
1

这是一个Perl版本针对此问题:

安装指令: cpan期望

此脚本完全适合我的需求。

参数1:Connexion的字符串(如:[email protected]) 参数2:明文口令 参数3:命令执行

#!/usr/bin/perl 
use strict; 

use Expect; 

my $timeout=1; 

my $command="ssh ".$ARGV[0]." ".$ARGV[2]; 

#print " => $command\n"; 

my $exp = Expect->spawn($command) or die "Cannot spawn $command: $!\n"; 
$exp->raw_pty(1); 

LOGIN: 
$exp->expect($timeout, 
     [ 'ogin: $' => sub { 
        $exp->send("luser\n");   
        exp_continue; } 
     ], 
    [ 'yes\/no\)\?\s*$' => sub { 
       $exp->send("yes\n"); 
       goto LOGIN; 
       } 
    ], 
     [ 'assword:\s*$' => sub { 
         $exp->send($ARGV[1]."\n"); 
      #print "password send : ", $ARGV[1]; 
         exp_continue; } 
     ], 
     '-re', qr'[#>:] $' 
); 
$exp->soft_close(); 
0

一种可能性是通过IP地址作为参数在您的期望脚本:

set host_ip [lindex $argv 0] 

,然后作出一个shell脚本,调用你的期望while循环中的脚本:

ips_file="list.txt" 

while read line 
do 
    your_expect_script line 
done < $ips_file 
0

或使用set ip [从标准输入]获取用户输入的IP地址。

的示例 -

把“输入您的IP地址\ n” 集IP使用环 - 产卵[获取标准输入]

使用这种在产卵,我们可以为多个IP地址做同样的ssh $ ip -l admin

+0

欢迎来到Stackoverflow。你介意扩大你的答案一点点来解释它是如何解决这个问题的。 – Daenarys