2013-09-30 17 views
6

Tôi mới mong đợi và viết kịch bản nói chung. Tôi đang cố gắng thực hiện một vài kịch bản để làm cho cuộc sống của tôi dễ dàng hơn một chút khi kéo cấu hình thiết bị mạng. Tôi đã tạo một kịch bản mong đợi cơ bản cho SSH tới một thiết bị và lưu cấu hình.Bash/Expect Script cho SSH

Tôi muốn mở rộng khi điều này và cho phép tập lệnh kết nối với một số địa chỉ IP thay vì chỉ một như tôi có ngay bây giờ. Tôi có một tệp có tên là list.txt với một vài địa chỉ IP khác nhau với mỗi IP trên một dòng riêng biệt.

Tôi cần làm gì để tập lệnh mong đợi kết nối với từng địa chỉ IP này và thực hiện phần còn lại của các tác vụ trong tập lệnh?

Đây là Expect kịch bản tôi có cho đến nay:

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

Tôi có cần phải tích hợp này với một kịch bản Bash? Nếu vậy, có thể đọc một dòng của tệp list.txt, sử dụng nó làm biến IP/máy chủ và sau đó đọc tiếp theo và lặp lại không?

+1

Bạn có thể định cấu hình thiết bị của mình để sử dụng khóa ssh không? Nếu có, bạn không cần mong đợi chút nào. –

Trả lời

2

tôi sẽ làm được điều này (chưa được kiểm tra):

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

Ghi chú:

  • Tôi đã gỡ bỏ tất cả bình luận của bạn cho ngắn gọn
  • sử dụng để mô phỏng \r nhấn Enter khi bạn send lệnh.
  • tôi cho rằng bạn chỉ muốn ghi lại các "show config chạy" đầu ra
  • sử dụng expect eof sau khi bạn gửi "exit"
1

Đây là một phiên bản Perl cho vấn đề này:

Cài đặt hướng dẫn: cpan Mong đợi

Tập lệnh này hoạt động hoàn hảo cho nhu cầu của tôi.

Param 1: liên quan chuỗi (ví dụ: [email protected]) Param 2: mật khẩu văn bản rõ ràng Param 3: lệnh để thực hiện

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

Một khả năng là phải vượt qua các địa chỉ IP như một tham số trong bạn mong đợi kịch bản:

set host_ip [lindex $argv 0] 

và sau đó thực hiện một kịch bản shell, gọi mong đợi của bạn kịch bản bên trong một vòng lặp while:

ips_file="list.txt" 

while read line 
do 
    your_expect_script line 
done < $ips_file 
0

hoặc Sử dụng đặt ip [được stdin] thành địa chỉ ip từ đầu vào của người dùng.

cho example-

đặt "Nhập địa chỉ ip của bạn \ n" set ip [được stdin]

sử dụng này trong spawn, Chúng ta có thể làm tương tự cho địa chỉ ip nhiều sử dụng tiếp vòng spawn ssh $ ip -l quản trị

+0

Chào mừng bạn đến với Stackoverflow. Bạn có thể mở rộng câu trả lời của mình một chút để giải thích cách giải quyết vấn đề. – Daenarys

Các vấn đề liên quan