Linux - Server This forum is for the discussion of Linux Software used in a server related context. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
01-14-2011, 04:06 AM
|
#1
|
Member
Registered: Mar 2009
Location: Belgium
Distribution: Ubuntu server 12.04 LTS / Raspbian Wheezy
Posts: 64
Rep:
|
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?
|
|
|
01-14-2011, 10:05 AM
|
#2
|
Senior Member
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
|
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.
|
01-17-2011, 01:39 AM
|
#3
|
Member
Registered: Mar 2009
Location: Belgium
Distribution: Ubuntu server 12.04 LTS / Raspbian Wheezy
Posts: 64
Original Poster
Rep:
|
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
|
|
|
01-17-2011, 02:20 AM
|
#4
|
Senior Member
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
|
Quote:
Originally Posted by rytec
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
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.
|
01-17-2011, 04:30 AM
|
#5
|
Member
Registered: Mar 2009
Location: Belgium
Distribution: Ubuntu server 12.04 LTS / Raspbian Wheezy
Posts: 64
Original Poster
Rep:
|
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...
|
|
|
01-17-2011, 04:42 AM
|
#6
|
Member
Registered: Mar 2009
Location: Belgium
Distribution: Ubuntu server 12.04 LTS / Raspbian Wheezy
Posts: 64
Original Poster
Rep:
|
Quote:
Originally Posted by rytec
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.
|
|
|
01-17-2011, 04:56 AM
|
#7
|
Member
Registered: Mar 2009
Location: Belgium
Distribution: Ubuntu server 12.04 LTS / Raspbian Wheezy
Posts: 64
Original Poster
Rep:
|
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
|
|
|
01-17-2011, 05:56 AM
|
#8
|
Member
Registered: Mar 2009
Location: Belgium
Distribution: Ubuntu server 12.04 LTS / Raspbian Wheezy
Posts: 64
Original Poster
Rep:
|
Quote:
Originally Posted by rytec
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.
|
|
|
01-17-2011, 09:41 PM
|
#9
|
Senior Member
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
|
Quote:
Originally Posted by rytec
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.
|
01-18-2011, 02:29 AM
|
#10
|
Member
Registered: Mar 2009
Location: Belgium
Distribution: Ubuntu server 12.04 LTS / Raspbian Wheezy
Posts: 64
Original Poster
Rep:
|
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.
|
|
|
All times are GMT -5. The time now is 02:09 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|