Check if host is connected to LAN - python-3.x

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.

Related

SSH interception - Linux

Really hoping someone here can point me in the right direction,
Expected result: SSH successfully into a remote device.
Challenge/Back story:
We have devices out in remote places around the country,
These devices do not have a fixed public IP address
(Using GSM as its internet breakout)
These devices are able to SSH and break out.
My thought, with regards to maintaining these devices is to (if possible) use a server in the cloud as a middle man, have these devices create some sort of a reverse tunnel to our middleman server then have us as admins intercept it or something to that effect.
Again to summarize, Device cannot be ssh'd into directly, but can breakout.
Aim to be able to hit their terminal from the office.
have been looking at mitmssh but not coming right on that front.
Server A (no fixed address, cannot SSH into it directly but has breakout)
Server B (standard server which can be used as a middle man
Server C (Us admins)
Tried something along the lines of "ssh user#serverA -R serverB:12345:ServerA:22"
Which creates the tunnel, but struggling with grabbing hold of that SSH connection.
I think I regularly use something very similar. My target machine connects to the machine with a stable address with:
ssh midpoint -R 2022:localhost:22
my ~/.ssh/config file knows the real HostName. My config file on my work machine defines a ProxyCommand option to use this tunnelled TCP connection. like:
Host target
ProxyCommand ssh -q midpoint nc localhost 2022
the reason for using netcat was to get ssh-agent behaving.
I've just been searching around and it seems OpenSSH now has specific handling for this (-W command line option, and JumpHost in the config file). E.g. https://stackoverflow.com/a/29176698/1358308

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!

How to programmatically check for connection?

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).

Send information via Parallel Port

I have a BarCode-Scanner and I need to get some information about it,
such as Serial Number.
The problem is that it communicates with the host via parallel port
(like a keyboard). I need to know how to send commands to the parallel port.
I've tried:
$ echo -n 'hexadecimal command' > /dev/input/by-path/platform-i8042-serio-0-event-kbd
but that only get's trash!
does anybody have any idea how to send and read data from parallel port?
(it works just like a keyboard).
tks,

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