How to programmatically check for connection? - linux

In Linux (Ubuntu), I want to programmatically check if there is Internet connection (or if eth0 is connected).
I'm doing this because I am writing a program that requires network connection on a system that is highly prone to lose connection.
So I was thinking maybe a script that I can run periodically to check.
Can you give me good suggestions?

Here is a quick script that will accomplish what you want:
EMAIL=youremail#something.com
ping -c 5 8.8.8.8 >> /dev/null
if [ $? -eq 0 ]
then
echo "Able to reach internet!" | mail $EMAIL
else
echo "Unable to reach internet!" | mail $EMAIL
fi
Obviosly you can change the mail to something else to do depending on what your goal is
EDIT: to explain, this pings googles dns server to ensure you are connected and sends you an email one way or the other. The email part on failure will only work of course if you have a local email server on your network.

/sbin/ifconfig would be an excellent "get adapter status" command to script.
cron would be an excellent way to execute the script.

I also suggest to ping or perhaps wget some distant server (preferably the one you want to connect to). The network could work well on the local campus, but not well on intercontinental connections (e.g. because some cables has been cut).

Related

Check if host is connected to LAN

I am writing a Python surveillance script for my home.
I have manually set a static LAN IP address to my phone by it's MAC address.
The Python script should check from time to time if my phone is still connected to the network. When I disconnect the script shall then continue and start the RTSP stream in my script.
The question
What is the simplest and best way to check if my phone is connected to the network or not?
I think the simplest way would be to ping your phone from time to time. Here's a bash example which I'm sure could be adapted to python one way or another:
ping -c2 192.168.1.200 >/dev/null && echo phone is connected || echo phone is offline
Just check the return code of the ping command.

Detecting that I am connected to a vpn/wifi and if not I automatically reconnect

so I made a script that logs into my schools vpn automatically by entering my username and password for me in my raspberry pi. Now I am trying to see if there is a way I can have a script running in the background that detects that I am connected to my vpn and also to my local wifi. If it detects there is no connection, then it will run my script of logging into the vpn automatically.
Below is the script I use to login to my vpn automatically my login.sh file:
#!/usr/bin/expect
spawn sudo openconnect vpn.ucr.edu/engineering
expect -r "\[sudo] .*\: " {
send "pw_for_my_linux\n"
}
expect "Username:" {
send "my_vpn_username\n"
}
expect "Password:" {
send "vpn_password\n"
}
interact
Perhaps you can use ethtool and check the connection status by looking at network interfaces.
ifconfig
(view network interface names, select network interface known to be associated with working vpn)
sudo ethtool eth0
Where 'eth0' is replaced by the name of the network interface that your vpn software is using
OUTPUT should look something like this if it's communicating (replace example net interface name eth0 with relevant one you can see in ifconfig):
Current message level: 0x00000007 (7)
drv probe link Link detected: yes
OUTPUT if down?:
Settings for eth0:
Link detected: no
In your bash code you can poll the 'link detected' status, parse out the answer to determine if you try reconnecting. Warning, haven't been able to test this on a vpn connection -- I have to use a special client other than openconnect, and for me I know the VPN is down if I run
ifconfig | grep "tun0"
If that returns something, VPN is up. If my network interface called tun0 is missing, VPN is not up.
Hope this gets you moving in the right direction!

Run script after network is connected

Placing files in /etc/network/if-up.d/ you can invoke a script whenever the interface is brought up. But that doesn't mean internet connectivity has been established. This happens to me quite frequently. For instance, the code below
#!/bin/sh
# Don't bother to do anything for lo.
if [ "$IFACE" = lo ]; then
exit 0
fi
# Only run from ifup.
if [ "$MODE" != start ]; then
exit 0
fi
# we only care about inet and inet6.
case $ADDRFAM in
inet|inet6|NetworkManager)
;;
*)
exit 0
;;
esac
/path/to/my/script
exit 0
Calls the script as soon as interface is brought up. But most often, I still do not have network connectivity, so my script fails because it tries to connect to the web.
What am I doing wrong?
By the time the interface is up, it doesn't means that you have a connectivity at all. If the interface is configured to get IP from DHCP for example, few seconds are needed for the IP configurations.
To check for Internet connectivity, you can query the IP of a known webpage with nslookup or dig. If you get a valid answer you can proceed. Otherwise, wait and repeat for a certain number of retries before aborting.
Now why to use the DNS system to check for Internet connectivity? I think that is the most easy and convenient way to check that all network aspects are properly configured (IP address, routes, DNS, etc).

Linux command line Syntax - Piping in commands after a ping request

Scenario is this.
I have logged into a web server as admin through the login page in browser.
it has displayed me with an input box that with a button next to it saying "ping"
it essentially will ping an ip address that you input.
I know for a fact the service running on the web server is running as root.
my question is this. can i put in an IP address and then pipe a command after it to elevate my privileges?
For Example...
ping 192.168.0.1 | usermod -a -G <groupname> username
ping 192.168.0.1 | whoami
the command shell running the ping is running under root so in theory it should allow me to elevate a users privileges on the system? please correct me if i'm wrong.
Don't have anything to test on so hence the question.
thanks in advance
This question cannot be answered with a certain "yes" or "no" without seeing the code or being able to explore the system.
It all really depends on the web app. If they're simply concatenating your input onto the end of ping and then execing that command, then yes, what you show is possible.
On the other hand, if they're properly sanitizing inputs (e.g. ensuring the input is only an IP address), or using some other implementation (e.g. a Ping library), then it may not be vulnerable.

Detecting a change of IP address in Linux

Does anyone know a way to detect a change of IP address in Linux. Say I have dhcpcd running, and it assigns a new IP address, is there a way I can get a notification when it changes? I can't use D-Bus, because this is an embedded ucLinux build that doesn't have it.
inotify on something in /proc/net/ ?
This is an old question, but I will answer for those who will arrive by Google (such as myself). After struggling for a while, I found out that you don't necessarily need to poll or hack a C solution for this. For my case, I wanted to update my home server's (dynamic dns) domain when the IP changes.
If you are running dhcpcd, you are in luck. dhcpcd will run hook scripts when anything happens. See man dhcpcd-run-hooks (online here). Basically you will want to modify or create your own dhcpcd.enter-hook or dhcpcd.exit-hook depending on what you want to do with the data provided by the event.
The command
ip monitor
will show you this kind of thing happening. It uses some the netlink API which is rather tricky and not documented well (at least for humans to understand). However, it is able to get notified by the kernel of various events, such as changes of assigned IPs, routing tables and link status (e.g. someone unplugged the network)
Since DHCP activity is sent to syslogd you could create a named pipe, direct syslog traffic to it and watch the stream for IP address updates. See 'man syslogd' and 'man syslog.conf'.
Edit: Another approach would be to use inotify to monitor the DHCP leases file for the interface. Under Ubuntu 9.10 that is in the /var/lib/dhcp3 directory.
What I thought of was running this script from cron every 10 or so minutes, depending on your link.
If I wrote this right, it only nsupdates when there is an IP change, so no undue load is creater on the zone's master server.
#!/bin/bash
OLD_IP=`cat ip.txt`
NEW_IP=`/sbin/ifconfig | awk -F "[: ]+'{ print $4}'` #adapted from something I got from the internets.
if [ $NEW_IP != OLD_IP ]; then
nsupdate <commands> #it seems like the keys need to be in the same directory from where nsupdate was called
fi
echo $NEW_IP > ip.txt
exit 0 #not sure if this is necessary
Not tested!
This is an older thread but in case someone finds it like I did, I wrote something that does network change detection/notification in Linux awhile back (mostly targeted at helping VPN users), and thanks to some pushy friends I put it up for others to use. It's a pet project now and I'm actively maintaining it, so feature requests and feedback are welcome.
http://code.google.com/p/ipcheck/source/browse/ipcheck.sh
I think you can use dbus to do this on modern Linux distributions. If your distribution uses NetworkManager, see this document for information about its dbus interface:
http://people.redhat.com/dcbw/NetworkManager/NetworkManager%20DBUS%20API.txt
If you have a router running DD-WRT and have the status page in use when going to the router, you can, with a script... wget the status page, cat for the ip address and write it to a file for comparison, have an email send when the latest wget ip address has changed from what is in the comparison file.
I'm running dd-wrt on a linksys wrt54g router and use this script:
It wgets the router status page from 192.168.3.1, uses cat on the page (index.html) and greps for the wan ip address, then writes it to a file (gotip.txt).
A comparison is made between the captured ip (gotip.txt) and the current working ip (workingip.txt). If the ip addresses are different, I get an email sent by send mail of the new ip, and the new working ip is written into the workingip.txt file.
Cron run this every 5 min or so and I have the cron output silenced to /dev/null
#!/bin/bash
getip=$(wget http://192.168.3.1/)
cat index.html | grep "wan_ipaddr" > gotip.txt
gotip=$(cat gotip.txt)
compare=$(cat workingip.txt)
if [[ "$compare" != "$gotip" ]]
then
EMAIL="youremail#foo.net"
EMAILMESSAGE="/home/pi/ipmessage.txt"
echo "ip address is now $gotip" >> $EMAILMESSAGE
/usr/sbin/sendmail -t "$EMAIL" < $EMAILMESSAGE
rm ipmessage.txt
cp gotip.txt workingip.txt
rm index.html
else
echo "done"
rm index.html
fi

Resources