Wake on LAN script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Wake on LAN script
# 1  
Old 10-20-2006
Wake on LAN script

m old to Unix but new to scripting


I have a MacBook running osx that I want to use as an nfs client. The server will be a linux box with a wake on lan card. Here's the idea. Run a cron command on the mac every minute that checks if I am on my home wireless network (the linux box is wired to the same router as the macbook gets wireless from). If this is true then check if the NFS mount from the linux box is mounted. If not ping the linux box. If the Macbook gets no ping send the magic packet to linux box and wake it up then wait for the linux box to boot and then mount the necessary NFS mounts.

When the linux box is on, run a cron command every minute to see if it is exporting NFS to the said Macbook (or i could just ping the macbook) If not (or no ping) have the linux box shut down in to a wakeable on lan state.


It looks as if I can pull the info as to wether or not my macbook is on my home wireless network from reading a line in traceroute. Then running showmount and then reading from the output if the NFS mount on the linux box is up. if neither of these are true then run ping, if no response send the magic packet to the linux box and start it up.


My main questions:


Has anyone already done this so I don't have to write the script?

Which sort of scripting should I use bash? pearl? python?

Are there any good tutorials out there that would help a newbee scripter accomplish this?

does anyone know what command i could run to see if the linux box is sucessufully exporting to the macbook, or would it just be easier to ping the macbook? and then use the response to shut down the linux box.


Thanks from Sweden!,

Nathan
# 2  
Old 10-24-2006
Bug

Here is something that might help you or get started:

Code:
/bin/ping -c 1 <Linux IP>
echo $?
if [ $? -ne 0 ] ; then
echo "Ping unsuccessful"
echo "Do other things here"
# run wake on lan command
showmount -e  #to check for nfs
fi

Good luck!
# 3  
Old 10-24-2006
thanks!

I appreciate the help.

Could you tell me what is going on in this script so i can apply it to what im doing?

in other words whats going on below?

echo $?
if [ $? -ne 0 ] ; then
echo "Ping unsuccessful"

Last edited by anon0mus; 10-24-2006 at 12:29 PM..
# 4  
Old 10-24-2006
Quote:
Originally Posted by nitin
Here is something that might help you or get started:

Code:
/bin/ping -c 1 <Linux IP>
echo $?
if [ $? -ne 0 ] ; then
echo "Ping unsuccessful"
echo "Do other things here"
# run wake on lan command
showmount -e  #to check for nfs
fi

Good luck!
Don't display status code before testing it, your test will test the status code of the echo command.
Code:
/bin/ping -c 1 <Linux IP>
ping_sts=$?
echo $ping_sts
if [ $ping_sts -ne 0 ] ; then
   echo "Ping unsuccessful"
   echo "Do other things here"
   # run wake on lan command
   showmount -e  #to check for nfs
fi


Jean-Pierre.
# 5  
Old 10-24-2006
Jean-Pierre: Sorry about that. I was just giving him some pointers, you are right, it will show echo's exit code.

So here is the explanaiton:
The ping sends just one packet to your desired IP address. It then gets an exit code on completion (like any other Unix program). You can then put the code in a variable (look at Jean-Pierre's post), other wise shell will forget about it.
"echo $ping_sts" displays the code.
If code is not zero, then there was a problem. An exit of zero means that program or utility ran without any problems, which is usually good. I'm being very general here, you may want to read up more on exit codes.
So "if $ping_sts is not equal to zero" then wake up the linux box and check for nfs exports.
Run "showmount -e <Linux IP>" on Mac, and it'll display exported shares on Linux. I have Mac, but I don't know any CLI utlity for Wake on Lan, you can find it on some Mac site. I have a Wake on lan for Linux, and I usually use my Linux to wake up the Mac.

Note: You cannot wake up systems using wireless cards, it's a wireless protocol limit. But you can wake up a wired system from a wireless system.


-Nitin Smilie
# 6  
Old 10-24-2006
thanks Nitin & Jean-Pierre!

I'm still trying to get my head aroud the exit code so i understand it fully. Seems like I could have halved the amount of code if i used it. I'll try fiddling with it and get the script to work using exit codes instead of the half ass way I've got it running now.

but,



Here is what i ended up doing. This is my first script that is over about 3 lines long. I know there is a lot of room for improvement and that the writing to a file is a bit of a hack and, I'm sure full of security holes. Suggestions for improvement would be great. ( i did not include the NFS mount command as I have'nt got NFS working on the Apple yet)

HTML Code:
#
# Script that checkins if computer is connected to a specific wireless network (dd-wrt
)
#  and then checks to see if a specific ip adress is up(192.168.1.110) 
# if the ip adress is down the magic packet is broadcasted in order boot the #approprate machine.
#
NETWORKCOMP="dd-wrt"
        
/usr/sbin/traceroute umu.se | grep dd-wrt > /tmp/netchk
        grep "dd-wrt" /tmp/netchk | awk '{print $2}' > /tmp/netchk2  
        
        NETWORK=`cat /tmp/netchk2` # Set the network variable

if [ "$NETWORK" == "$NETWORKCOMP" ]; then
        echo "You have access!"
        
        # Remove the temp files
             rm /tmp/netchk
             rm /tmp/netchk2 


        GOODPING="icmp_seq=0"

        /sbin/ping -c 1 192.168.1.110 | grep icmp > /tmp/pingchk
        grep "icmp_seq=0" /tmp/pingchk | awk '{print $5}' > /tmp/pingchk2

        REALPING=`cat /tmp/pingchk2`
        
                if [ "$GOODPING" == "$REALPING" ]; then  
                        echo "host is alive, no need to wake up!"
 
                       # Remove the temp files
                         rm /tmp/pingchk
                         rm /tmp/pingchk2

                else
                        # Remove the temp files
                        rm /tmp/pingchk
                        rm /tmp/pingchk2 
        
                        echo "Host is dead, CLEAR!!"
                        
                        pythonw /sbin/wol.py
                fi

else
        echo "Network not found!"

        # Remove the temp files
             rm /tmp/netchk
             rm /tmp/netchk2 

fi

Nathan

Last edited by anon0mus; 10-24-2006 at 06:04 PM..
# 7  
Old 10-27-2006
I wonder if this problem is being overengineered. Why not just send the magic packet every time?
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Computer wake commands

I'm a OS X user (MacBook Pro, OS X Lion) and I need it to wake up on Mondays, Wednesdays, Thursdays and Saturdays at 9:00 AM on the rest of the days of the week at 7:00 I issue the following commands: sudo pmset repeat wake MWRS 09:00:00 for the former sudo pmset repeat wake TFU... (1 Reply)
Discussion started by: scrutinizerix
1 Replies

2. UNIX for Dummies Questions & Answers

Trouble Configuring Wake On Lan

As the title implies I'm having trouble setting up Wake-On-LAN with my Debian box. Here is the output from ethtool and my /etc/network/interfaces: # cat /etc/network/interfaces # /etc/network/interfaces - configuration file for ifup(8), ifdown(8) # The loopback interface auto lo iface lo... (2 Replies)
Discussion started by: Azrael
2 Replies

3. Shell Programming and Scripting

need perl script to get a comprehend statistic of network activity in a LAN

need perl script to get a comprehend statistic of network activity in a LAN. The purpose is to get each indivisual data usages statistics so that I can determine who is doing the network congestion in terms of bandwidth usages both in real time and retrospectively.. Thanks in... (4 Replies)
Discussion started by: rrd1986
4 Replies

4. Red Hat

Different hostnames with reboot while lan cable, no lan cable

I am facing strange problem regarding hostname on my Linux(2.6.18-164.el5xen x86_64 GNU/Linux), the hostname changes if reboot with lan cable and with NO lan cable Reboot with lan cable: The hostname is ubunut Unable to connect Oracle database using sqlplus some times database is not... (2 Replies)
Discussion started by: LinuxLearner
2 Replies

5. IP Networking

Local Lan, no-ip directed DNS forward, surf within lan

Hi, We have a website running on a local centos 5.4 surfer, static IP. The domain.com uses no-ip.com to take care of the DNS, it forwards all to my server. My router receives the port 80 call, routes it to my server and the world can see domain.com perfectly fine. However, we cannot see... (3 Replies)
Discussion started by: lawstudent
3 Replies

6. Linux

rhel 4 linux WOL wake on lan

Hi guys, Two boxes on the same .23 subnet 192.168.1.x The box I wanted to shutdown and restart remotely went down, but didn't come back up when I used ether-wake 00:11:22:etc:etc: The sleeper has two nics, but only one with cable. I configured both nics to wake on lan using the g option. The... (0 Replies)
Discussion started by: Bloke
0 Replies

7. Solaris

How to configure private LAN and coporate LAN on the same machine-Solaris10

Hi , I am trying to configure a private LAN and corporate LAN on the same machien on Solaris 10. How can I achieve this? Thanks (1 Reply)
Discussion started by: deedee
1 Replies

8. UNIX for Advanced & Expert Users

Wake on Lan script

Im old to Unix but new to scripting I have a MacBook running osx that I want to use as an nfs client. The server will be a linux box with a wake on lan card. Here's the idea. Run a cron command on the mac every minute that checks if I am on my home wireless network (the linux box is wired to... (0 Replies)
Discussion started by: anon0mus
0 Replies

9. Shell Programming and Scripting

Script to get IP addresses of LAN computers

I need a shell script for OS X, one that can find IP addresses of machines connected to my LAN, get the names of the computer associated with those addresses, then display them like so in a list: "Bob's L33T Boxx: #.#.#.#" Something like the network scanner in Apple Remote Desktop is what I'm... (1 Reply)
Discussion started by: sladuuch
1 Replies
Login or Register to Ask a Question