Jump to content

script needed


Recommended Posts

Can anyone help me create a script for me? It's pretty simple i think. All it needs to do it to do an ifconfig of my ppp0 > textfile, then email that file to my hotmail account, every day, this way i will be able to know the server's ip when i want to access it from over the internet, and won't get stuck when my isp decides to change my ip. I would really appreciate any ideas.

 

[moved from Software by spinynorman]

Link to comment
Share on other sites

Hmmmm...think this needs to be in "Terminal Shell Commands, Kernel and Programming" for a better response, but I'll give it a shot:

#!/bin/bash

ifconfig | grep -A 3 ppp0 | tee /home/curtis/ifconfig | mailto -s ipaddress curtisebear@hotmail.com
EXITVALUE=$?
if [ $EXITVALUE != 0 ]; then
   /usr/bin/logger -t mailip "ALERT exited abnormally with [$EXITVALUE]"
fi
exit 0

 

Name it mailip and put it in /etc/cron.daily, making it executable

 

Edit: You need metamail installed.

Edited by Steve Scrimpshire
Link to comment
Share on other sites

Just stepping in :)

The best expression to strip the ppp0 ip I know is this one:

/sbin/ifconfig | awk '/P-t-P/ {print substr($2,6)}'

 

Though this one is even clearer and faster (hence better):

/sbin/ifconfig ppp0 | awk '/addr/ {print substr($2,6)}'

 

 

Nothing new to add, just wanting to say hello :mr-green:

Edited by aru
Link to comment
Share on other sites

Just stepping in :)

The best expression to....

<<insert aru's boundless knowledge here>>

:mr-green:

 

 

yeahyeah :P

 

I'm confused about the output of the two. I'll use my wlan0 as an example because I have no ppp:

root@laptop.mdk /home/omar 1825 14-Feb-05

> /sbin/ifconfig wlan0 | awk '/inet/ {print substr($2,6)}'

192.168.1.102

 

root@laptop.mdk /home/omar 1825 14-Feb-05

> /sbin/ifconfig wlan0 | awk '/addr/ {print substr($2,6)}'

 

192.168.1.102

 

root@laptop.mdk /home/omar 1825 14-Feb-05

>

 

Why does the first one (supposedly slower) give me a new line after the ipaddr, but the second, faster, one gives me a new line before and after?

Edited by Steve Scrimpshire
Link to comment
Share on other sites

Why does the first one (supposedly slower) give me a new line after the ipaddr, but the second, faster, one gives me a new line before and after?

Well, the first one is slower (In my example not in yours) because it parses the full ifconfig output while the second one (in my example again) only parses the ppp0 output :P

 

The important thing about my second example is not that it's faster (the difference is minimal), but that is quite clearer because you can see w/o too much digging what the expression does, it gets ppp0 output and searchs for the "addr"ess inside. In the first example that wasn't clear at first sight (call me pedantic :juggle: )

 

back to your question, in your second command (not in mine :mr-green: ) the "/sbin/ifconfig wlan0" has several lines with the string "addr" inside, but only one fits the rest of the awk expression (which is quite lucky), so all the other lines that are matching "addr" are printed as null to stdout. That doesn't happen with ppp0 because its output only has a single line that matches "addr"

 

Here you'll see both your example, and the same command but printing instead all the lines that are matching the string "addr":

 

~$ /sbin/ifconfig eth0 | awk '/addr/ {print substr($2,6)}'

192.168.0.1

~$ /sbin/ifconfig eth0 | awk '/addr/ {print $0}' 
eth0      Link encap:Ethernet  HWaddr 00:02:44:5C:2E:E4  
         inet addr:192.168.0.1  Bcast:192.168.0.255  Mask:255.255.255.0
         Interrupt:10 Base address:0xac00 
~$

 

You'll better need to add ":" to your expression, as:

 

~$ /sbin/ifconfig eth0 | awk '/addr:/ {print substr($2,6)}'
192.168.0.1
~$

 

Glad to see you all again :D

Link to comment
Share on other sites

#!/bin/bash

ifconfig ppp0 | mailto -s ipaddress someone@someone.com
EXITVALUE=$?
if [ $EXITVALUE != 0 ]; then
   /usr/bin/logger -t mailmyiptostranger "ALERT exited abnormally with [$EXITVALUE]"
fi
exit 0

 

Name it mailmyiptostranger and put it in /etc/cron.daily, making it executable

This will not save it to a file first...just put it in the body of an email. The '-s ipaddress' part means 'subject'.

Edited by Steve Scrimpshire
Link to comment
Share on other sites

IMHO the best place to save that script is in /etc/ppp/ip-up.d, hence whenever you start/restart your internet connection you'll email the brand new IP.

 

--> man pppd

      /etc/ppp/ip-up

              A program or script which is executed when the link is available

              for  sending  and  receiving  IP packets (that is, IPCP has come

              up).  It is executed with the parameters

 

              interface-name      tty-device      speed      local-IP-address

              remote-IP-address ipparam

 

That ip-up script contains code to execute every script (run-parts) in /etc/ppp/ip-up.d

 

HTH

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...