1

My target is to just validate the login is successful for list of servers in CSV file. For SSH Password, Userid and IP address, I am passing these values via CSV.

And want to capture the result of SSH Connection output after each successful SSH login.

Wrote a shell script for that but, I am seeing successful login output for only the first data set from the CSV. For rest of the servers SSH command is failing.

Script:

#!/bin/sh
INPUT=Test_data.csv
OLDIFS=$IFS
IFS=","
[ ! -f $INPUT ] && { echo "$INPUT file not found"; exit 99; }
while read ip user pass
do
sshpass -p $pass ssh -n -t -t -o "StrictHostKeyChecking no" $user@$ip >> output.txt
wait
exit
done < $INPUT
IFS=$OLDIFS

Output:

Welcome to CYBER SECURITY
Cisco Codec Release ce 9.12.3 140cd8212ba 2020-04-17
SW Release Date: 2020-04-17
*r Login successful
OK
^[[?1034h10.xx.xx.xx,admin,432584
Command not recognized.
10.xx.xx.xx,admin,432584
Command not recognized.
10.xx.xx.xx,admin,432584
Command not recognized.
10.xx.xx.xx,admin,432584
Command not recognized.
1

In short: SSH shell scripting does not work the way you're expecting and that's causing your script to break.

What's happening is obvious: The 'first' SSH connection is established, and from that point on everything that's coming from stdin is sent to the SSH connection -- and the Cisco has no idea what to do with a 'command' that's a CSV line.

From the ssh manpage:

SYNOPSIS
     ssh [-46AaCfGgKkMNnqsTtVvXxYy] [-B bind_interface] [-b bind_address] [-c cipher_spec] [-D [bind_address:]port]
         [-E log_file] [-e escape_char] [-F configfile] [-I pkcs11] [-i identity_file] [-J destination] [-L address]
         [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port] [-Q query_option] [-R address]
         [-S ctl_path] [-W host:port] [-w local_tun[:remote_tun]] destination [command]

That [command] bit is key here -- if you want to automate something on the other end of the link, you're going to have to specify it on the same command line or it's not part of the ssh command. wait and exit in your script never get called.

Try modifying the line to

sshpass -p $pass ssh -n -t -t -o "StrictHostKeyChecking no" $user@$ip 'wait; exit' >> output.txt

and see if that works, although frankly I'd recommend setting them up in an Ansible inventory instead; it might save you a hell of a lot of headaches because then you can just run ansible -m ping ciscos and get a lovely summary.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.