Created
August 16, 2017 22:21
-
-
Save michiel/c477face7c2b289c982c470b24d39184 to your computer and use it in GitHub Desktop.
expect + ssh + scp with password to collect data from a list of servers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
PASSWORD="password" | |
USERNAME="username" | |
SSHPORT=22 | |
for REMOTE_SERVER in `cat servers.txt`; do | |
OUTFILE="$REMOTE_SERVER-outfile" | |
echo "Remote is $REMOTE_SERVER .." | |
echo " - File output to out/$OUTFILE" | |
echo " - Connecting (SSH) to $REMOTE_SERVER .." | |
expect -c " | |
set timeout 1 | |
spawn ssh -oStrictHostKeyChecking=no -oCheckHostIP=no -p $SSHPORT $USERNAME@$REMOTE_SERVER | |
expect yes/no { send yes\r ; exp_continue } | |
expect password: { send $PASSWORD\r } | |
expect \$ { send 'ls -la /tmp > /tmp/testfile\r' } | |
sleep 1 | |
exit | |
" | |
echo "" | |
echo " - Connecting (SCP) to $REMOTE_SERVER .." | |
echo "" | |
expect -c " | |
set timeout 1 | |
spawn scp -P $SSHPORT $USERNAME@$REMOTE_SERVER:/tmp/testfile out/$OUTFILE | |
expect yes/no { send yes\r ; exp_continue } | |
expect password: { send $PASSWORD\r } | |
expect 100% | |
sleep 1 | |
exit | |
" | |
echo "" | |
if [ -f "out/$OUTFILE" ]; then | |
echo "Success" | |
else | |
echo "Failure" | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment