Skip to content

Instantly share code, notes, and snippets.

@michiel
Created August 16, 2017 22:21
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save michiel/c477face7c2b289c982c470b24d39184 to your computer and use it in GitHub Desktop.
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
#!/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