Exotic Ideas..
Angad's Blog
About this blog
I am Angad Singh. I have served as the Sun Campus Ambassador of JIIT University, Noida (India) from August 2007 to July 2008 and as a Campus Ambassador Tech Lead from July 2008 to July 2009. This was my sun blog. Here I jotted down all my random scribblings, reports on all activities I conducted as CA at my university, my little projects, hacks, geeky stuff and new technology I came across, all the way to things I learnt in my exciting journey with Sun..
About Me
View Angad Singh's profile on LinkedIn
Technorati Authority
View blog authority
Subscribe
Search

10 Recent Entries
Archives
« December 2009
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  
       
Today
Links
My..
Blogs that interest me
 

Today's Page Hits: 284

Best Viewed in..

Mozilla Firefox

Locations of visitors to this page
« Campus Ambassador... | Main | Posting syntax highl... »
Friday May 30, 2008
Solaris Network Administration Scripts
Here's a little script I was working on back in the college lab last month. I was facing a very trivial solaris administration problem. I've installed SXDE 1/08 on 120 systems in one of our biggest computer labs long back (more on how I did that later). Now I needed a way to do certain tasks on each of those systems like changing the boot order, changing the solaris GRUB splash image, setting the hostname for each system based on its current IP address, changing the wallpaper, setting permissions, creating user accounts, etc. This is basic post installation things that you do, but I needed to do it after a long time post their installation so I couldn't really use the smarter ways, like JumpStart installation scripts, Flash archive based installation, etc. I needed a way to "manage" or "administer" those 120 systems remotely over the network automatically.

So I went on with the little challenge to make a script (lan_exec.sh) which does the following:
  1. Loops through a range of IP addresses (those which are assigned to the systems in that lab)
  2. For each loop iteration, first it checks to see if the remote host is alive (ping)
  3. Then it checks if the SSH port (22) is open using nmap (to detect if the remote host is running Solaris)
  4. Once the checks are performed, another script is called with the IP address as an argument.

lan_exec.sh:

#!/bin/ksh

addr=172.16.74
si=128
ei=254
cmd=./exec.exp

i=${si}

while [ $i -le ${ei} ]
do

More:
	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:

  1. scp my modified grub menu.lst to the remote host's desktop using the public "jiit" account.
  2. Expect a set of responses from the scp command (first time it asks if we want to add this host's key, and further times it just says "Password:") and pass on the password accordingly.
  3. Do the same for any other files that have to be copied (in my case, a modified grub splash image)
  4. Run ssh to the remote system and input the password using expect
  5. Run su and input password using expect !
  6. Move the files copied to the Desktop in steps 2/3 to their respective places
  7. Do other tasks, needed to be done as root (in my case, I changed the hostname according to current IP, etc.)

exec.exp:

#!/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 {

More:
	"(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 :)

Posted at 05:53PM May 30, 2008 by Angad Singh in OpenSolaris  |  Comments[2]  |  del.icio.us digg slashdot technorati Stumble It! Share on Facebook furl reddit Share on Twitter    

Comments:

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 #

Post a Comment:
  • HTML Syntax: NOT allowed
Creative Commons License

This work by Angad Singh is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License.