How to drop your IP address into a share…

I have an Opensuse 12.1 workstation that I use as a script “toolbox”,  out in the regular cubicle space where I work.  Because it sits smack-dab in the middle of a DHCP-based environment — and the corporate policy is to block all out-going SMTP/email — I can’t just email myself the IP Address like I might in other locations.  However, I do need to know when it reboots (power outages, etc.) and potentially grabs a new IP address.

In this case, the one thing that I *do* have access to is a Windows server share (this is a corporation, after all), so why not drop the IP address there…?  To do this, I’ve written a quick little script to mount the share at boot, and dump the address to a text file on the share….

…But, I need to take an aside here.

While I would normally just add a scripted call to the “/etc/rc2.d/after.local“, something about systemd and the latest Opensuse 12.1 has caused this to stop working correctly.  The good news is that jdmcdaniel3 has put together some quick steps to fix that for you, and you can find it all here on the Opensuse forum.  For the rest of this post, I’ll assume you’ve followed his steps and fixed it on 12.1, or you are using an earlier version where the after.local just works.

First, create the script that will run at boot (after a delay), mount the share, and plop in a file with the address:

vi /home/jpavlov/IpAddress.bash

#!/bin/bash
# Better wait for network...
sleep 60
# What time is it!
RightNow=`date +%Y%m%d%H%M`
# What file to write results?
OutFile="$HOSTNAME-IpAddress.wri"
# Check the status of the mount first...
MountResult=`mount|grep "cifs"`
if [ "XX$MountResult" == "XX" ]; then
  mkdir /media/cifs
  # Mount the corporate share, mine is DFS-based...  set it to your path
  mount -t cifs -o username=YourDomain/YourUsername,password=YourPassword //YourDomain/DfsParent/DfsPath1/DfsPath2/ /media/cifs
fi
# Switch to my work-group user sub-folder, again, set to taste and escape spaces
cd /media/cifs/User\ Folders/User\ -\ Jeremy/linuxtoolbox/
# Finally, what we are waiting for
echo "Results from $0 on $RightNow" > $OutFile
/sbin/ifconfig eth0|grep "inet addr"|sed s/"inet addr:"//g|awk '{ print $1 }' >> $OutFile
# And I may as well drop an info message in case it doesn't work
echo "$RightNow ifconfig export sccript done!" >> /var/log/messages

…And protect that file (and the password) from prying eyes with:

chmod 700 /home/jpavlov/IpAddress.bash

Now, we need to add it to the after.local so it will launch at boot:

echo "/home/jpavlov/IpAddress.bash" >> /etc/rc.d/after.local

…And that’s about it.  After a reboot, you should find something like HOSTNAME-IpAddress.wri in your folder, with the IP address from your workstation at last boot.  Of course, you probably won’t do this is you have other folks who have root access to this machine, or they’d see your password in the script.  Or you could create a drop-box type share that permits the deposit of the file, etc…

Now go play!
😉

 

2 Comments

  1. Philip

    Hello Jeremy,

    Just curious: why not write down the MAC address of the PC, and then use ‘arp -a’ on your workstation to locate the IP?

    -Philip.

  2. Jeremy Pavlov

    @Philip
    Good suggestion, especially if I were always local to the machine (and I’d have to do a ping sweep of the subnet to ensure the address got in the arp table). However, I really didn’t mention this, but I mostly did this for those times where I’m remote (VIA VPN, etc.), or so that the other folks I work with can find it too (of course, they have rights to the share too).
    Thanks!
    -Jeremy

Leave a Comment

Your email address will not be published.