Friday May 30, 2008
Today's Page Hits: 284
Friday May 30, 2008
#!/bin/ksh
addr=172.16.74
si=128
ei=254
cmd=./exec.exp
i=${si}
while [ $i -le ${ei} ]
do
ip=$addr.$i
#check to see if remote client is live (ping)
result=`ping $ip 1`
if [ "$result" = "$ip is alive" ]
then
echo "[$ip] is ALIVE.. \c"
#check to see if port 22 (scp) is open
port22=`nmap -sT -p22 $ip|grep open`
if [ "$port22" != "" ]
then
echo "SSH port OPEN"
#then execute operation to be performed on remote machine
${cmd} $ip
echo "Finished execution"
else
echo "SSH port CLOSED"
fi
else
echo "[$ip] is DOWN"
fi
i=`expr $i + 1`
done
The second script (exec.exp) is the big deal here. I wanted to be able to SSH into a remote system from a system who's public keys aren't stored on that system, and since it was a default untouched SXDE installation, root wasn't allowed to remote SSH login, so there was just no way I could SSH using root. I had to SSH using a previously created public account called "jiit". Now the problem was that I needed root permission on the shell to do some of the remote management tasks (like changing grub boot order, installation of apps, etc.). Since, there was no sudo installed, I had to su. Unfortunately, su doesn't accept passwords from standard input. There's just no way to do it (try it). I definitely wasn't going to enter the root password a 120 times, so I looked for a solution. A nifty little thing called "Expect" saved the day - a full blown scripting language for automating interactive command line applications! So I could actually emulate keystrokes in the remote shell to give su the root password (and much more!).
Here's what my expect script does:
#!/opt/csw/bin/expect
# Program to change boot order of a system
set ipaddr [lrange $argv 0 0]
puts "Copying boot file to remote system"
spawn /usr/bin/scp -r /boot/grub/menu.lst jiit@${ipaddr}:/export/home/jiit/Desktop/menu.lst
expect {
"(yes/no)? " {
send "yes\r"
expect "Password: "
send "jiit\r"
}
"Password: " {
send "jiit\r"
}
"password: " {
send "jiit\r"
}
}
expect "\$ "
spawn /usr/bin/scp -r /boot/grub/splash.xpm.gz jiit@${ipaddr}:/export/home/jiit/Desktop/splash.xpm.gz
expect "Password: "
send "jiit\r"
expect "\$ "
puts "Running SSH to remote system"
spawn ssh jiit@${ipaddr}
expect "Password:"
send "jiit\r"
expect "\$ "
send -- "\r"
expect "\$ "
send "su\r"
expect "Password: "
send "mypassisntthissimple\r"
expect "# "
send "rm /boot/grub/menu.lst\r"
expect "# "
send "rm /boot/grub/splash.xpm.gz\r"
expect "# "
send "mv /export/home/jiit/Desktop/menu.lst /boot/grub/menu.lst\r"
expect "# "
send "mv /export/home/jiit/Desktop/splash.xpm.gz /boot/grub/splash.xpm.gz\r"
expect "# "
puts "Setting hostname"
send "echo cl1-solaris-`echo \\`echo \"${ipaddr}\" | cut -c11-\\`` > /etc/nodename\r"
expect "# "
puts "Restarting System.."
send "reboot\r"
expect "# "
The above method (the 2 scripts) now allow me to remote manage all those 120 solaris machines without any hassles :)
Dude,
Excellent post. Though, I have never ever done such things. Still reading it, shows the skills you have and how you have made creative use of them.
Keep it up!
Posted by Varun on June 01, 2008 at 11:46 AM IST #
Can I recommend Puppet here (http://reductivelabs.com/trac/puppet/). We use it at Nominet and it's great for easily managing large numbers of systems.
Posted by Andy Holdaway on June 20, 2008 at 02:09 PM IST #