Bash script help reading IPs from files. Using expect and ssh

250 views
Skip to first unread message

Luis Arizaga

unread,
Oct 26, 2017, 6:53:46 AM10/26/17
to Linux Users Group
Hi folks, I'm a newbie in bash scripting. I'm trying to perform ping tests using bash + expect + some files where I have the IPs of the devices I have to connect to and the IPs to ping. It's a dumb performance test in order to learn a little bit about bash and expect.
Here's the code, I'll explain it below:
#!/bin/bash
 # Collect the current user's ssh and enable passwords
 echo "Rentrez votre login TACACS RR3 s'il vous plaît"
 echo "Normalement real.xxx01"
 read -s -e -r login_tacacs
 echo "----------------------------------------------------"
 echo "Rentrer votre mot de passe TACACS RR3 s'il vous plaît"
 read -s -e -r password_tacacs
 echo "----------------------------------------------------"
 echo "Rentrer votre login perimetre garik (RR2)"
 echo "Normalement prenom.nom"
 read -s -e -r logingarik
 echo "----------------------------------------------------"
 echo "Rentrer votre mot de passe perimete garik"
 read -s -e -r passwordgarik
 echo "----------------------------------------------------"
 echo -ne '\n'
 log_file=test-$(date +%F).log
 touch "$log_file"
# Feed the expect script a device list & the collected passwords
 echo "Connecting to CVPN devices"
for device in $(cat cvpn-lo0.txt); do
for device_ping in $(cat cvpn-pe-ping.txt); do
./test-ospf.exp $device $login_tacacs $password_tacacs $device_ping >> $log_file;
done
done
 echo "----------------------------------------------------"
 echo "Connecting to LNS devices"
 echo -ne '\n'
for device in $(cat lns-lo0.txt); do
for device_ping in $(cat lns-pe-ping.txt); do
./test-ospf.exp $device $login_tacacs $password_tacacs $device_ping >> $log_file;
done
done
 echo "----------------------------------------------------"
 echo "Connecting to XR devices"
 echo -ne '\n'
for device in $(cat xr-lo0.txt); do
for device_ping in $(cat xr-pe-ping.txt); do
./test-ospf.exp $device $logingarik $passwordgarik $device_ping >> $log_file;
done
done
 echo "----------------------------------------------------"
 echo "Connecting to RH devices"
 echo -ne '\n'
for device in $(cat rh-lo0.txt); do
for device_ping in $(cat rh-pe-ping.txt); do
./test-ospf.exp $device $login_tacacs $password_tacacs $device_ping >> $log_file;
done
done

So first I ask for 2 credentials that I need in order to login to different devices. I create a log file and then my idea is to loop between two files: one for the ips of the devices that I have to go in with SSH (cvpn-lo0.txt and all lo0.txt files, there's basically and ip per line in those txt files) and another for the lo0 of the devices I have to ping from every specific source (there's an IP per line as well). The ping is done in expect from the script test-ospf.exp

Thing is that I keep having an error on line 23 (the first for iteration). I don't know what I'm doing wrong. I'm sending the expect script as well in case you want to take a look. Any help would be appreciated.





Expect Script
#!/usr/bin/expect -f


 exp_internal
1
# Set variables
 
set timeout 20
 
set hostname [lindex $argv 0]
 
set username $env(USER)
 
set password [lindex $argv 1]
 
set lo0_ping [lindex $argv 2]
 
# Announce which device we are working on and at what time
 send_user
"\n"
 send_user
">>>>>  Je travaille sur $hostname @ [exec date] :) <<<<<\n"
 send_user
"\n"
 
# Don't check keys
 spawn ssh
-o StrictHostKeyChecking=no $username\@$hostname
 
# Allow this script to handle ssh connection issues
 expect
{
 timeout
{ send_user "\nTimeout Exceeded - Check Host\n"; exit 1 }
 eof
{ send_user "\nSSH Connection To $hostname Failed\n"; exit 1 }
 
"*#" {}
 
"*assword:" {
 send
"$password\n"
 
}
 
}
 
# If we're not already in enable mode, get us there
 expect
{
 
default { send_user "\nEnable Mode Failed - Check Password\n"; exit 1 }
 
"*#" {}
 
}
 
# Enter your commands here. Examples listed below


  send
"ping $lo0_ping repeat 1000 source loopback0"
  expect
"*ms"
  quit
 
exit


Thanks in advance folks.

Cheers,

Luis


 

Jeremiah Bess

unread,
Oct 26, 2017, 3:27:41 PM10/26/17
to linuxus...@googlegroups.com
Hey Luis,

I played around a bit with that part of your code that is erroring out and I can't figure out what the problem is. I would suggest for testing purposes to break that command up. Something like this:

for device in $(cat cvpn-lo0.txt); do
echo $device
done

for device_ping in $(cat cvpn-pe-ping.txt); do
echo './test-ospf.exp $device $login_tacacs $password_tacacs $device_ping >> $log_file;'
done

This might help you zero in on what part it's having issues with.

--
--
You received this message because you are subscribed to the Linux Users Group.
To post a message, send email to linuxus...@googlegroups.com
To unsubscribe, send email to linuxusersgro...@googlegroups.com
For more options, visit our group at http://groups.google.com/group/linuxusersgroup
References can be found at: http://goo.gl/anqri
Please remember to abide by our list rules (http://tinyurl.com/LUG-Rules)
---
You received this message because you are subscribed to the Google Groups "Linux Users Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to linuxusersgro...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--

Jeremiah Bess

Luis Arizaga

unread,
Oct 27, 2017, 2:35:18 AM10/27/17
to Linux Users Group

Thanks for your suggestion Jeremiah. I think I'll do that. I'll break the code a little bit in order to analyse it better. I guess I'm trying to learn bash too fast haha :)

Have a nice day.

Regards,

Luis
Reply all
Reply to author
Forward
0 new messages