reading ip adress from script

cool. i'll try ans see what fits best into my plan.

the plan actually is to automatically post my ip-address to a website after establishing a dsl connection.

like this i hope to avoid situations where i wish i knew my ip number when i cannot get my hands on my machine otherwise.
 
Me(static IP, FreeBSD) and my friend(dynamic IP, adsl, MacOS X) uses the following strategy for uploading his IP to my (name)server. So that we always can reach his mac via it's name.
You would probably only need the client side script. As you can se we update the DNS record for his machine on my name server.

(Addresses altered for privacy reasons)

On the OS X client this is cron'ed every 5 minutes
---------------------------------------------------
#!/bin/sh
#
set path = /sbin /usr/bin
#
OIP=`cat /var/log/tyraip`
NIP=`/sbin/ifconfig en0 | /usr/bin/grep 'inet 2' | sed 's/inet//' | sed 's/netmask.*//'`

if EXPR=$(expr $NIP : $OIP); then
echo `date +'%y-%m-%d% %H:%M:%S'` -- ${NIP}
exit 0
fi

echo `date +'%y-%m-%d% %H:%M:%S'` ++ ${NIP}
echo ${NIP} > /var/log/tyraip

curl -T /var/log/tyraip ftp://name:pw@server.com/tyraip

exit 0
---------------------------------------------------



On the FreeBSD server this is cron'ed every 5 minutes
---------------------------------------------------
#!/bin/sh
#
PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin

IPFILE="/home/server.com/users/visitor/tyraip"
#
if ! test -f $IPFILE; then
echo `date +'%y-%m-%d% %H:%M:%S'` -- no change
exit 0
fi

TYRAIP=`cat $IPFILE`

if test -n "$TYRAIP"; then
cp /etc/namedb/tyra.host2.se.proto /etc/namedb/tyra.host2.se
echo "@ IN A "$TYRAIP >> /etc/namedb/tyra.host2.se
kill -HUP `ps -x | grep 'named' | grep -v 'grep' | awk '{print $1}'`
rm -f $IPFILE
echo `date +'%y-%m-%d% %H:%M:%S'` new ip ${TYRAIP}

fi

exit 0


--------------------------------------------

I hope this can be of any help to you.
 
thanx for your suggestions, but they don't seem to work for me:
1. /var/db/SystemConfiguration/preferences.xml

doesn't seem to contain my ip-address. i tried the following:
cat /var/db/SystemConfiguration/preferences.xml | perl -e 'while(<>){if(m/\d+\.\d+\.\d+\.\d+/){print;}}'

i have tested the perl part. it does work on files that do contain ip- adresses.

so i'm pretty sure that there is no ip-address in the file preferences.xml

2. ifconfig en0
doesn't return my ip-address either. it returns the following interesting info:
en0: flags=8863<UP,BROADCAST,b6,RUNNING,SIMPLEX,MULTICAST> mtu 1500
ether 00:50:e4:79:e7:21
media: autoselect (10baseT/UTP <half-duplex>) status: active
supported media: none autoselect 10baseT/UTP <half-duplex> 10baseT/UTP <full-duplex> 100baseTX <half-duplex> 100baseTX <full-duplex>

so the nice shell script won't work either.

any ideas?
 
while(<>){if(m/\d+\.\d+\.\d+\.\d+/){print;}}
This regex will match things like
127.0.0.1,
255.255.255.0, etc.
It will also match
123457.854431.587981354687.87871

Suggested rewrite:
while (<>) {print $_ if /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/;}
 
<shrug> JAPH. :)

Can't help but suggest fixes to Perl code when I see it. I know that if there are no IP addresses in the file it won't matter.
 
The script I posted is used on a OS X box with 2 ethernet adaptors (used as a gateway), you will have to alter it to fit your configuration.
 
thanx,
i figured out another way to read my ip address. even though it's not very elegant it works fine:
cat /var/log/system.log | perl -e 'while(<>){if(m/(local IP address\s(\d+\.\d+\.\d+\.\d+))/){$ip=$2;}}print "$ip;' > /Users/user/file
curl -T /Users/user/file ftp://user:passwd@mysite.net/ipadress

i'm connected to the net via adsl.
aha cool,
ifconfig -au does actually show my ip address, i'll use that instead of reading the system log. what does -au mean?

thanx again.

couzteau
 
ftp://user:passwd
That's funny.

One note about your Perl (I'm sorry, I can't help it). You have two sets of parens in your match, but only use $2. Why not just one set of parens and $1?
 
thanx for your explanations. i understand that
my active interface is called ppp0.

ifconfig ppp0 shows my ipaddress. may be this is because i switched from a ppp-analog-modem connection to adsl?

is the prferences.xml the place where the network control panel stores data? if so, i'm just guessing here, it stores only static data there. my ip-address is not static, that's why i started this thread to begin with. is yours static?

my first version of the script has a problem. i can't rely on the system.log containing my ip-address. when i got home last night it didn't. it only contained a few lines starting with:
Mar 24 03:15:09 localhost syslogd: restart

which is probably due automatic system cleaning. a bit o/t but how can i change the times system cleaning runs? my /var/cron/tabs is empty.
i also need to install my script as a cron job. never done this before.

here is the final version of the script:

ifconfig ppp0 | perl -e 'while(<>){if(m/inet (\d+\.\d+\.\d+\.\d+)/){$ip=$1;}}print "$ip"' > /Users/user/file
curl -T /Users/user/file ftp://user:pass@mysite.net/ipadress

thanx again and again, you are very helpful. boosting my productivity.

couzteau

ps: using colons in Quotes is a funny thing to do ;-)
 
here is the final version of the script for a machine running tcp/ip over ppp with a dynamic ip-address. it will create a file on a remote ftp-machine that doesn't mind that it's a script that does the talking, not a real user.

here it is:
#!/bin/sh

NIP=`/sbin/ifconfig ppp0 | /usr/bin/grep 'inet' | sed 's/ inet //' | sed 's/ -->.*//'`
echo "${NIP}" \
| curl -s -T - ftp://ftp_user:ftp_pass@my_domain.org/ipaddress

it's very important that NIP only contains the ip address, and no spaces or tabs. the line starts with a tab after inet is a whitespace! the 1st sed command gets rid of that.

note that when i was running this script with cron without naming the exact path to ifconfig and grep it didn't work. cron uses a different PATH.

moreover i liked gumse's approach to use grep and sed instead of perl, because it seemed inappropriate to fire up the mighty perl just to get rid of a few chars in a string.

what i didn't like in gumse's script (even though it might very well have been appropriate to solve his problem and i foun his version very helpful) was that it creates a temporary file everytime it runs. instead In my version curl reads from STDIN which is linked with the STDOUT of the echo command. Finally i wanted curl to do its job quietly. so used i used -s to make it shut up. otherwise cron might jam your mailbox because it sends an email with the output of the script everytime it runs it.


i hope you will find this useful.
 
here is the final version of the script for a machine running tcp/ip over ppp with a dynamic ip-address. it will create a file on a remote ftp-machine that doesn't mind that it's a script that does the talking, not a real user.

here it is:
#!/bin/sh

NIP=`/sbin/ifconfig ppp0 | /usr/bin/grep 'inet' | sed 's/ inet //' | sed 's/ -->.*//'`
echo "${NIP}" | curl -s -T - ftp://ftp_user:ftp_pass@my_domain.org/ipaddress

it's very important that NIP only contains the ip address, and no spaces or tabs. the line starts with a tab after inet is a whitespace! the 1st sed command gets rid of that.

note that when i was running this script with cron without naming the exact path to ifconfig and grep it didn't work. cron uses a different PATH.

moreover i liked gumse's approach to use grep and sed instead of perl, because it seemed inappropriate to fire up the mighty perl just to get rid of a few chars in a string.

what i didn't like in gumse's script (even though it might very well have been appropriate to solve his problem and i foun his version very helpful) was that it creates a temporary file everytime it runs. instead In my version curl reads from STDIN which is linked with the STDOUT of the echo command. Finally i wanted curl to do its job quietly. so used i used -s to make it shut up. otherwise cron might jam your mailbox because it sends an email with the output of the script everytime it runs it.


i hope you will find this useful.
 
Back
Top