2013-09-30 12 views
6

Jestem nowy, którego można się spodziewać i skryptów w ogóle. Próbuję wykonać kilka skryptów, aby nieco ułatwić sobie życie przy konfigurowaniu urządzeń sieciowych. Udało mi się stworzyć podstawowy skrypt SSH na urządzenie i zapisać konfigurację.Skrypt Bash/Expect dla SSH

Chcę się rozwinąć i zezwolić skryptowi na łączenie się z wieloma adresami IP, a nie tylko jednym takim, jaki mam teraz. Mam plik o nazwie list.txt z kilkoma różnymi adresami IP z każdym adresem IP w osobnej linii.

Co powinienem zrobić, aby skrypt oczekiwał połączenia z każdym z tych adresów IP i wykonać pozostałe zadania w skrypcie?

Oto Oczekiwać skrypt mam tak daleko:

#!/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 

Czy muszę zintegrować ze skryptu bash? Jeśli tak, czy można odczytać jedną linię z pliku list.txt, użyć go jako zmiennej IP/hosta, a następnie przeczytać następny i powtórzyć?

+1

Czy możesz skonfigurować urządzenia do używania kluczy ssh? Jeśli tak, nie musisz się w ogóle spodziewać. –

Odpowiedz

2

zrobiłbym to (niesprawdzone):

#!/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 

Uwagi:

  • usunąłem wszystkie swoje komentarze dla zwięzłości
  • użytku \r symulować uderzenia podać przy send poleceń.
  • Przypuszczałem chcesz tylko zalogować się "show config działa" Wyjście
  • korzystanie expect eof po wysłaniu "exit"
1

To jest wersja Perl do tej kwestii:

zainstalować instrukcję: cpan Expect

Ten skrypt działa idealnie na moje potrzeby.

Param 1: związek string (ex: [email protected]) Param 2: jasne, hasło tekst Param 3: polecenie do wykonania

#!/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

Możliwość jest przekazanie adresu IP jako parametr w twoich oczekiwać skrypt:

set host_ip [lindex $argv 0] 

a następnie wykonać skrypt, nazywając swój oczekiwać skrypt wewnątrz pętli while:

ips_file="list.txt" 

while read line 
do 
    your_expect_script line 
done < $ips_file 
0

lub Użyj zestawu ip [pobiera stdin] do adresu IP z danych wprowadzonych przez użytkownika.

na przykład-

stawia „Wpisz swój adres IP \ n” zestaw ip [dostać stdin]

wykorzystanie tego w tarło, możemy zrobić to samo dla adresu IP za pomocą pętli loop wielokrotnego ikry ssh $ ip -l admin

+0

Witamy w Stackoverflow. Czy mógłbyś rozszerzyć swoją odpowiedź, aby wyjaśnić, w jaki sposób rozwiązuje problem. – Daenarys