2013-09-30 29 views
6

私は一般的に期待してスクリプトを書くのが新しいです。私は、ネットワーク機器の設定を引き出すときに、私の人生を少し楽にするためにいくつかのスクリプトを作ろうとしています。私は、デバイスへのSSHへの基本的な期待スクリプトを作成し、設定を保存することができました。SSHのためのBash/Expectスクリプト

これを拡張して、私が今すぐ持っているように、1つではなく複数のIPアドレスに接続できるようにします。私は別の行に各IPといくつかの異なるIPアドレスとlist.txtという名前のファイルがあります。

expectスクリプトをこれらのIPアドレスのそれぞれに接続させ、スクリプト内の残りのタスクも実行するには、私は何が必要でしょうか?ここで

は、私がこれまで持っていたスクリプトを期待です:

#!/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ファイルの1行を読み込み、それを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 

ノート:

  • 私は打撃のときsendのコマンドを入力してシミュレートするために、簡潔にするためにすべてのあなたのコメント
  • 使用\rを削除しました。

    命令をインストールします:私はあなただけあなたが「出口」

1

を送信した後、これは、この問題のためのPerlのバージョンである出力

  • 使用expect eof「を実行しているショーの設定」を記録したいと仮定
  • cpan期待する

    このスクリプトは、私のニーズに完全に対応しています。

    パラメータ1:コネクション文字列(例:[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] 
    

    、その後、whileループ内で、あなたの期待するスクリプトを呼び出し、シェルスクリプトを作る:

    ips_file="list.txt" 
    
    while read line 
    do 
        your_expect_script line 
    done < $ips_file 
    
    あなたの期待スクリプトで
    0

    またはユーザ入力からip addressにset ip [gets stdin]を使用します。例 -

    ため

    が 設定したIP産卵中

    使用これを[GET STDIN] "あなたのIPアドレス\ nは入力" 置き、我々はLOOP- 卵を使用して、複数のIPアドレスに対して同じことを行うことができますssh $ ip -l admin

    +0

    ようこそStackoverflowへ。問題をどのように解決するのか説明するために、あなたの答えを少し拡大してください。 – Daenarys

    関連する問題