expect in your shell script

“come on, you need 2 scripts(shell and expect) to get the following done?”

first post:
second post:

not necessarily, i searched around and found a solution.

excerpts from: http://tinyurl.com/hnul8

================

#!/bin/sh
# \
exec expect -f “$0″ ${1+”$@”}
set password [lindex $argv 1]
spawn passwd [lindex $argv 0]
sleep 1
expect “assword:”
send “$password\r”
expect “assword:”
send “$password\r”
expect eof

================

Referring to the script above:

=========================
below is the shell script to change passwords across multiple servers
=========================

#!/usr/bin/bash
for i in `cat servers read the full info here.txt`; do
exec expect -f “$0″ ${1+”@”}
#i was using the line below when i was testing the script
#spawn rm .ssh/known_hosts

set server {$i}
set timeout -1
spawn ssh $server
#uncomment as needed
#expect “Are you sure you want to continue connecting (yes/no)? ”
#send — “yes\r”
expect “password:”
send — “oldpass\r”
set timeout 2
expect “\$”
set timeout 2
sleep 1
#sub the one below if the other doesn’t work/help
#spawn passwd
send — “passwd\r”
sleep 1
send — “pass4You\r”
expect “assword:”
sleep 1
send — “newpass\r”
sleep 1
expect “assword:”
send — “newpass\r”
send — “exit\r”
expect eof
if [ $? -eq 0 ]; then
echo “$i PASSWORD WAS MARVELOUSLY CHANGED”>> RESULTS
else
echo “$i FAIL” >>RESULTS
fi
done

========================
below is the one to check password across multiple servers
========================

#!/usr/bin/bash
for i in `cat servers.txt`; do

exec expect -f “$0″ ${1+”$@”}
set server [lindex $arv 0]
set timeout -1

spawn ssh -o NumberofPasswordPrompts=1 $server >/dev/null
expect “Are you sure you want to continue connecting (yes/no)? ”
send — “yes\r”
expect “password:”
send — “currentpass\r”
set timeout 2
expect eof

if [ $? -eq 0]; then
echo “$i PASSWORD is CURRENT” >>CHECK-RESULTS
else
echo “$i FAIL” >>CHECK-RESULTS
fi
done

NOTE TO SELF:
servers.txt – line separated list of servers

This entry was posted in Uncategorized and tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

Security Code: