LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 01-14-2011, 04:06 AM   #1
rytec
Member
 
Registered: Mar 2009
Location: Belgium
Distribution: Ubuntu server 12.04 LTS / Raspbian Wheezy
Posts: 64

Rep: Reputation: 7
How to create a script for ISP connection check


I would like to make a script which can check my ISP connection, I have a dynamic IP and it seems that lately it often gets changed.
So I thought of making a script which does a NSlookup and send this to my root address.
But it would be better if it only does this when it notice a disconnect otherwise I get loads of messages.
I use a watchguard firewall and behind this I have my Ubuntu server which is also the DNS and DHCP server for my internal LAN.
My watchguard makes a DSL connection with my bridged DSL modem.
Also the watchguard has a logging system and I send the syslog to my ubuntu server but it's too much work to look everyday into this whole logfile system.
Or would it be better to look into this logging files and search for a specific description and send only this to my root mail?
 
Old 01-14-2011, 10:05 AM   #2
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Well, ifconfig knows both the state and the IP address; why not use that?
If ifconfig is wrong, you won't have an internet connection anyway, so any hostname lookups would fail, too.

Here's an example skeleton script using bash,
Code:
#!/bin/bash

# The device used to connect to the internet.
NETDEV="eth0"

# Be robust, and use full path to ifconfig:
IFCONFIG="/sbin/ifconfig"

# This file contains the IP address others know.
IPFILE="/var/run/known.ip"

# Ask ipconfig for the IP address. If down, this will be empty.
CURRIP="`$IFCONFIG "$NETDEV" 2>/dev/null | sed -ne 's|^.*[\t ]inet addr:[\t ]*\([^\t ]*\).*$|\1|p'`"
if [ -z "$CURRIP" ]; then
    # No IPv4 address. Perhaps we're using IPv6?
    CURRIP="`$IFCONFIG "$NETDEV" 2>/dev/null | sed -ne 's|^.*[\t ]inet6 addr:[\t ]*\([^\t /]*\).*$|\1|p'`"
fi
if [ -z "$CURRIP" ]; then
    # Network is down..
    exit 0
fi

# Pick out the IP address from the file.
PREVIP=`sed -ne '/[^\t ]/ { s|^[\t ]*\([^\t ]*\).*$|\1|p ; q }' "$IPFILE" 2>/dev/null`
if [ "$CURRIP" == "$PREVIP" ]; then
    # No change ..
    exit 0
fi

# Okay, IP address was changed from $PREVIP to $CURRIP.
# Do whatever you wish to do ..
# Remember to exit if a failure occurs.

# Success! Update the known IP address locally, too.
echo "$CURRIP" > "$IPFILE"

# Done.
exit 0
You would then use e.g. cron to run this as often as you need, perhaps every five minutes.
You can even run that under a specific user, just remember to create the log file for that user with e.g.
Code:
touch /var/run/known.ip
chown user:group /var/run/known.ip
chmod ug=rw,o=r /var/run/known.ip
You could add a hook in the networking scripts to detect when your ethernet interface is brought up or its IP address changes, instead. That involves modifying the network interface scripts and configuration your distribution provides. It means you'd most likely need to fix them by hand if you ever upgrade -- and you might even have real issues if the default network configuration were updated, say due to a bug fix in the networking packages.

If run only every five minutes, or longer intervals, the CPU load of the above script is insignificant.
And the script is easy to maintain, and it does not interfere with the other network configuration.
Nominal Animal

Last edited by Nominal Animal; 03-21-2011 at 03:16 AM.
 
1 members found this post helpful.
Old 01-17-2011, 01:39 AM   #3
rytec
Member
 
Registered: Mar 2009
Location: Belgium
Distribution: Ubuntu server 12.04 LTS / Raspbian Wheezy
Posts: 64

Original Poster
Rep: Reputation: 7
Hello Nominal Animal,

Thank you very much for this great script and information.
But I doubt this will work in my situation because the server I run this script on is inside the LAN network and has a fixed IP.
My firewall is the unit which logs on the DSL modem and this firewall gets the WAN IP address.

So if I run NSlookup on my server I can see which dynamical WAN IP is connected to my domain name.

So I could use this wonderful script but instead of using ifconfig eth0 it should use the nslookup command somehow, yes somehow which means I do not exactly know how to write this in a script

Ryan
 
Old 01-17-2011, 02:20 AM   #4
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Quote:
Originally Posted by rytec View Post
But I doubt this will work in my situation because the server I run this script on is inside the LAN network and has a fixed IP.
Right, it won't do anything.
Quote:
Originally Posted by rytec View Post
My firewall is the unit which logs on the DSL modem and this firewall gets the WAN IP address.
Sure. Replace the fully.qualified.domain.name below with the full domain name of the IP address you wish to track.
Code:
#!/bin/bash

# The hostname to look for. Automagic.
NETHOST="fully.qualified.domain.name"

# This file contains the IP address others know.
IPFILE="/var/run/known.ip"

# Look up the hostname.
CURRIP="`nslookup "$NETHOST" 2>/dev/null | gawk '/Server:/ { getline ; next } /Address:/ { ip = $2 } END { print ip }'`"
if [ -z "$CURRIP" ]; then
    # Network is down..
    exit 0
fi

# Pick out the IP address from the file.
PREVIP=`sed -ne '/[^\t ]/ { s|^[\t ]*\([^\t ]*\).*$|\1|p ; q }' "$IPFILE" 2>/dev/null`
if [ "$CURRIP" == "$PREVIP" ]; then
    # No change ..
    exit 0
fi

# Okay, IP address was changed from $PREVIP to $CURRIP.
# Do whatever you wish to do ..
# Remember to exit if a failure occurs.

# Success! Update the known IP address locally, too.
echo "$CURRIP" > "$IPFILE"

# Done.
exit 0
Hope this helps,
Nominal Animal

Last edited by Nominal Animal; 03-21-2011 at 06:31 AM.
 
1 members found this post helpful.
Old 01-17-2011, 04:30 AM   #5
rytec
Member
 
Registered: Mar 2009
Location: Belgium
Distribution: Ubuntu server 12.04 LTS / Raspbian Wheezy
Posts: 64

Original Poster
Rep: Reputation: 7
The program gawk is not installed yet.
Is this same kind as grep?

I have made a script with your text and I have made the known.ip file.
Started the script and it created the current IP address in the file, if I run it again nothing appears on the commandline such as No change...
 
Old 01-17-2011, 04:42 AM   #6
rytec
Member
 
Registered: Mar 2009
Location: Belgium
Distribution: Ubuntu server 12.04 LTS / Raspbian Wheezy
Posts: 64

Original Poster
Rep: Reputation: 7
Quote:
Originally Posted by rytec View Post
The program gawk is not installed yet.
Is this same kind as grep?

I have made a script with your text and I have made the known.ip file.
Started the script and it created the current IP address in the file, if I run it again nothing appears on the commandline such as No change...
I noticed the echo command was not in front of the text you made as example so I changed it with echo "No change.." and this works.

You have done me a huge favour ! now I will try to integrate this myself so it sends a mailmessage if the wan IP is changed.

many thanks.
 
Old 01-17-2011, 04:56 AM   #7
rytec
Member
 
Registered: Mar 2009
Location: Belgium
Distribution: Ubuntu server 12.04 LTS / Raspbian Wheezy
Posts: 64

Original Poster
Rep: Reputation: 7
I have changed the IP address in the known.ip file to be able to test the echo output if ip address is changed but nothing happens to the output # Success! Update the known IP address locally, too.
echo "$CURRIP" > "$IPFILE"
does not show anything
 
Old 01-17-2011, 05:56 AM   #8
rytec
Member
 
Registered: Mar 2009
Location: Belgium
Distribution: Ubuntu server 12.04 LTS / Raspbian Wheezy
Posts: 64

Original Poster
Rep: Reputation: 7
Quote:
Originally Posted by rytec View Post
I have changed the IP address in the known.ip file to be able to test the echo output if ip address is changed but nothing happens to the output # Success! Update the known IP address locally, too.
echo "$CURRIP" > "$IPFILE"
does not show anything
Also here I have found the solution and removed hash before okay and put:
echo "Okay, IP address was changed" from $PREVIP to $CURRIP.
 
Old 01-17-2011, 09:41 PM   #9
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Quote:
Originally Posted by rytec View Post
Also here I have found the solution and removed hash before okay and put:
echo "Okay, IP address was changed" from $PREVIP to $CURRIP.
Right! For Bash scripts, # starts a comment line. I was in a bit of a hurry, so I made the changes to my local version of the script, quickly tested it, and pasted it, without remembering I changed the comments to echoes earlier. Sorry about that!

If you set the script to run regularly with superuser rights, say by putting it into /etc/cron.hourly or running it directly from /etc/crontab, you can restart e.g. OpenSSH by adding service sshd restart to the script. (This makes sshd restart and listen on the new IP address.)

If the script solves your problem, prepending [SOLVED] to the title would be polite and helpful.

If you have problems with other services when your IP address changes, it might be better to start a new thread with topic something like "How to get X to work again after my IP address changes?" to get a wider audience. Although you may find that adapting the above service command should work for any service.
Nominal Animal

Last edited by Nominal Animal; 03-21-2011 at 06:29 AM.
 
1 members found this post helpful.
Old 01-18-2011, 02:29 AM   #10
rytec
Member
 
Registered: Mar 2009
Location: Belgium
Distribution: Ubuntu server 12.04 LTS / Raspbian Wheezy
Posts: 64

Original Poster
Rep: Reputation: 7
Goodmorning, this thread was super and solved my problem.
Now I can check the ISP connection on DNS problems and IP changes, yesterday evening we already found some difficulties and this script reported me directly first it could not resolve the hostname and then later it could again.
And this morning again message network down and 5mins later message IP address changed so this means the ISP connection went down and up in this time.

many many thanks again.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Script to check connection (and restart network if down) mac_phil Linux - Networking 15 06-21-2016 05:59 PM
Create own ISP CJPoll Linux - Networking 5 11-11-2006 08:07 PM
anyway to check if ISP is blocking ports caykroyd Linux - Server 2 08-30-2006 06:56 PM
check out whether the two have same ISP.... b0nd Linux - Networking 3 12-30-2005 12:41 AM
how to check for block ports by isp? Drogo Linux - Software 2 10-26-2003 05:15 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 02:09 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration