2013-09-30 80 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