Tag Archives: internet

Headless RaspPi: Tell Me Where You Are

I like several of the little SBC (Single Board Computer) systems that are available, despite having a love/hate relationship with my favourite – the ODROID C2. For the minute I’m stuck with just a Raspberry Pi 3 as a little SBC, so needs must when the devil, etc etc…

Anyway, when you control your own network, as I used to, it doesn’t really matter what IP DHCP decides to assign to each system. I could look it up on the router easily enough, or even fix an IP address based on MAC address.

OK, fine… but what if you don’t control your router?

Well, you could brute-force scan the entire known IP range of the network using NMAP… (nmap -sS xxx.yyy.zzz.0/24) but that seems a little extreme. It works in a pinch, though, and might be the only way of finding it that all important first time.

Once you know where it is, however, how can you keep it? On a network you don’t control, you can get jumped around the IP range depending on how the router is feeling and how many others are connected. Not very useful.

Here’s a little script modified from another one that didn’t work for me… (requires Python 2, not Python 3…)

Ah, and I found where the mods come from…!

This will send you an e-mail telling you your headless systems’ IP when the RPi boots.

Just add this to a file with vi bootupEmail.py, then chmod +x it:

import subprocess
import smtplib
import socket
from email.mime.text import MIMEText
import datetime
# Change to your own account information
to = 'me@example.com'
gmail_user = 'test@gmail.com'
gmail_password = 'yourpassword'
smtpserver = smtplib.SMTP('smtp.gmail.com', 587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo
smtpserver.login(gmail_user, gmail_password)
today = datetime.date.today()
# Very Linux Specific
arg='ip route list'
p=subprocess.Popen(arg,shell=True,stdout=subprocess.PIPE)
data = p.communicate()
split_data = data[0].split()
ipaddr = split_data[split_data.index('src')+1]
my_ip = 'Your ip is %s' %  ipaddr
msg = MIMEText(my_ip)
msg['Subject'] = 'IP For RaspberryPi on %s' % today.strftime('%b %d %Y')
msg['From'] = gmail_user
msg['To'] = to
smtpserver.sendmail(gmail_user, [to], msg.as_string())
smtpserver.quit()

You’ll also need to add python /home/user/bin/scripts/bootupEmail.py to your rc.local, just before the exit 0 at the end.

IMPORTANT: Obviously, since you just put your Gmail password in a plain text file (really, really bad security practice!) just set up a gmail account to only do this one thing. Or, if you want, you could set up a mailserver on your Pi.

That may become the topic of a future post. In fact, it probably will, because I can’t believe I just suggested putting a password in plaintext.

Linux Ping Script… (Finally…)

Anyone reading this may remember I was having intermittent – but regular and extremely annoying – network dropouts in my accommodation.

Well, I finally got twenty minutes (and a network switch so I can have more than one thing plugged in to the LAN at once) to get my act together and update my little Raspberry Pi 3 so it can sit and (low power) ping a server and let me know when the network drops out.

Just like the Windows version, this only writes to the log file when it can’t connect – so if your network never drops out, you’ll never even see the log file get created. You can test it by changing the sleep time and what it greps for (if you change sleep to 1 and grep unknown to grep PING, you can check it works…)

Just save it somewhere sensible, like /home/user/bin/scripts or similar, chmod +x it, and run it from the terminal. Force it to the background and you can log out and leave it ticking over.

#!/bin/bash
while true; do
     x=`ping -c1 www.google.com 2>&1 | grep unknown`
     if [ ! "$x" = "" ]; then
         date >> brokentimes-pingtest.txt
     fi
sleep 30
done

Dodgy Internet Connections: Ping Script

I’m having some fun at the minute with a dodgy internet connection. It really is driving me to distraction, I must admit. It’s quite frustrating.

It manifests as a connection that drops in and out almost at random. In fact, if I heavily load the connection, rather than doing what I expect and dropping out, it seems to stabilise. But I can’t really download hundreds of GB all the time, can I? Also, it does likewise drop out occasionally at high loads – that’s the worst issue anyone has to try to troubleshoot: an intermittent one.

Regardless, I’m trying to collect evidence for the flakiness (although tonight it doesn’t seem to be playing – I’ve had a largely painless experience online this evening) so need some way of proving that something is up.

Note that I’m in Windows 10 most evenings, due to Skype, Word and Visual Studio. Yup, spend all day at work on Linux, move to Windows of an evening. Insert moaning about no good Office suites on Linux here.

So, anyway, a quick Windows (non-Powershell) script to ping a site at regular intervals, and log if not successful. Right now I’m not interested in when it is working – only when it’s not.

@echo off
setlocal enabledelayedexpansion
set hostIP=[put an IP address here]
:loop
set pingline=1
for /f "delims=" %%A in ('ping -n 1 -w 250 -l 255 %hostIP%') do (
    if !pingline! equ 2 (
        set logline=!date! !time! "%%A"
        echo !logline! | find "TTL=">nul || echo !logline! >> pinglog.txt
        )
    set /a pingline+=1
    )
timeout 10
goto loop

All credit due, I found this here and modified slightly so I didn’t have to download something from Windows Server 2003. OK, so timeout is slightly less accurate than sleep, but for my purposes it is sufficient. It will run until you kill it. Closing the command line is sufficient.