Bash / Expect Script for SSH

I am new to waiting and writing scripts in general. I am trying to make several scenarios to make my life a little easier when you choose network device configurations. I managed to create a basic pending SSH script for the device and save the configuration.

I want to expand this and allow the script to connect to multiple IP addresses, not just one, as it is now. I have a file called list.txt with several different IP addresses with each IP address on a separate line.

What do I need to do so that the wait script connects to each of these IP addresses and does the rest of the tasks in the script?

Here is the Expect script I still have:

 #!/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 " admin@ " # #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 " admin@ " # #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 

Do I need to integrate this with a Bash script? If so, is it possible to read one line of the list.txt file, use it as an IP / host variable, and then read the next and repeat?

+6
source share
4 answers

I would do this (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 " admin@ " send "set cli pager off\r" log_file $logfile send "show config running\r" expect " admin@ " log_file send "exit\r" expect eof } close $fid 

Notes:

  • I deleted all your comments for brevity
  • use \r to simulate a keystroke when you execute send commands.
  • I assumed that you only want to log the output of "show config running"
  • use expect eof after sending "exit"
+2
source

This is the version of Perl for this problem:

Installation Instructions: cpan Expect

This script works great for my needs.

Parameter 1: communication string (for example: admin@10.34.123.10 ) Parameter 2: text password Parameter 3: command to execute

 #!/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(); 
+1
source

Ability to pass the IP address as a parameter in the expected script:

 set host_ip [lindex $argv 0] 

and then create a shell for the script, causing the script to wait inside the while loop:

 ips_file="list.txt" while read line do your_expect_script line done < $ips_file 
0
source

or use set ip [gets stdin] for the ip address from user input.

eg -

puts "Enter your IP address \ n" set ip [get stdin]

use this in spawn, we can do the same for multiple IPs using loop-spawn ssh $ ip -l admin

0
source

Source: https://habr.com/ru/post/954970/


All Articles