2013-09-30 10 views
6

के लिए बैश/अपेक्षा स्क्रिप्ट मैं आम तौर पर उम्मीद और स्क्रिप्टिंग के लिए नया हूं। मैं नेटवर्क डिवाइस कॉन्फ़िगरेशन खींचते समय अपनी जिंदगी को थोड़ा आसान बनाने के लिए कुछ स्क्रिप्ट बनाने की कोशिश कर रहा हूं। मैं एक डिवाइस के लिए एसएसएच को मूल उम्मीद स्क्रिप्ट बनाने और कॉन्फ़िगरेशन को सहेजने में कामयाब रहा।एसएसएच

मैं इस पर विस्तार करना चाहता हूं और स्क्रिप्ट को सिर्फ एक जैसा है जैसे कि सिर्फ एक आईपी पते से कनेक्ट करने की अनुमति देता हूं। मेरे पास list.txt नामक एक फ़ाइल है जो प्रत्येक आईपी के साथ एक अलग लाइन पर कुछ अलग आईपी पते के साथ है।

अपेक्षित स्क्रिप्ट इन आईपी पते में से प्रत्येक से कनेक्ट होने और स्क्रिप्ट में शेष कार्यों को करने के लिए मुझे क्या करने की आवश्यकता होगी?

यहाँ स्क्रिप्ट मैं अब तक है उम्मीद है:

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

मैं एक बैश स्क्रिप्ट के साथ इस एकीकृत करने के लिए की जरूरत है? यदि हां, तो क्या list.txt फ़ाइल की एक पंक्ति को पढ़ना संभव है, इसे आईपी/होस्ट चर के रूप में उपयोग करें और फिर अगला पढ़ें और दोहराएं?

+1

क्या आप अपने डिवाइस को एसएसएच कुंजी का उपयोग करने के लिए कॉन्फ़िगर कर सकते हैं? यदि हां, तो आपको बिल्कुल उम्मीद की आवश्यकता नहीं है। –

उत्तर

2

मैं इस (untested) करना होगा:

#!/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 "चल शो config" आउटपुट लॉग इन करने की भेजने के बाद चाहते हैं 'प्रस्थान'
1

यह इस समस्या के लिए एक पर्ल संस्करण है:

अनुदेश स्थापित करें: सीपीएन उम्मीद

यह स्क्रिप्ट मेरी आवश्यकताओं के लिए पूरी तरह से काम करती है।

परम 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

एक संभावना एक पैरामीटर के रूप आईपी पते पारित करने के लिए है

set host_ip [lindex $argv 0] 

और फिर एक खोल स्क्रिप्ट बनाने के लिए, अपने एक समय पाश अंदर स्क्रिप्ट उम्मीद बुला: अपने में स्क्रिप्ट उम्मीद

ips_file="list.txt" 

while read line 
do 
    your_expect_script line 
done < $ips_file 
0

या उपयोगकर्ता इनपुट से आईपी पते पर सेट आईपी [stdin] का उपयोग करें।

उदाहरण

के लिए

डालता सेट आईपी अंडे loop- का उपयोग कर [stdin प्राप्त]

उपयोग इस अंडे में, हम कई आईपी पते के लिए भी ऐसा ही कर सकते हैं "n आपके आईपी पते \ दर्ज करें" एसएसएच $ आईपी -एल व्यवस्थापक

+0

स्टैक ओवरफ्लो में आपका स्वागत है। क्या आप इस समस्या को हल करने के तरीके को समझाने के लिए अपने उत्तर को थोड़ा सा विस्तारित करना चाहते हैं। – Daenarys