barre1986
Posts: 11
Joined: Tue Nov 01, 2016 5:29 pm

auto visit website as Dyndns

Tue Nov 01, 2016 5:50 pm

hi all

I have 2 raspberry pi at 2 different locations, the modems have dynamic ip addresses that change every 48 hours. At my home i have a dyndns account that i use for my nas , i can run a website from the nas with php script. I was thinking of some kind of script in python and cron (for every hour) that the raspberry pi's use to visit my php webpage so my webpage can store the ip's of the raspberry pi's so if i need to connect to them i know there ip adres. My php page can store it in a mysql database and maybe if the raspberry pi visit my page they can give some kind of variabele with it to say im Raspberry1 or Raspberry2
with simple in url var like http://mydynsaccount.com/page.php?name=raspberry1 . How do i make this , any tips for that?

User avatar
topguy
Posts: 6491
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: auto visit website as Dyndns

Thu Nov 03, 2016 1:43 pm

For the clients just use "wget" in a bash script and CRON to access the URL you want as often as you need.
If theory you could just extract the IP's from the access log of the web-server you are using.

There is nothing Raspberry specific in this request so you could have posted this to any PHP forum.

User avatar
DougieLawson
Posts: 39121
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: auto visit website as Dyndns

Fri Nov 04, 2016 1:10 pm

I use https://dnsomatic.com/ as that takes one update from my home network and posts it to all the other dynamic dns services.

Code: Select all

#!/bin/bash

OLDaddr=$(head -1 /home/pi/ipaddr.saved.txt)
IPaddr=$(printf '\42';dig +short myip.opendns.com \@resolver1.opendns.com | awk '{printf "%s",$0}';printf '\42')

if [ $IPaddr == '"0.0.0.0"' ]; then
  exit 20
fi

if [ $OLDaddr != $IPaddr ]; then
  NOW=$(date +"%d/%m/%Y, %T,")
  printf "$NOW New address found\r\n"
  echo -e $NOW$IPaddr >> /home/pi/ipaddr.log.txt
  echo $IPaddr | tee /home/pi/ipaddr.saved.txt
  printf "\r\n"
  curl -s -o - --user userid_goes_here:passwd_goes_here  https://updates.dnsomatic.com/nic/update
  printf "\r\n"
  /home/pi/pubIP.py | mail -r "pi@apollo.example.co.uk" -s "Apollo IP" my_gmail_id_goes_here@gmail.com
fi
This is my pubIP.py program

Code: Select all

#!/usr/bin/python3

from urllib.request import urlopen
import json
ip = urlopen('http://httpbin.org/ip').read()
ip = ip.decode('utf-8')
ip = json.loads(ip)

print(ip['origin'])
import socket
name, alias, addresslist = socket.gethostbyaddr(ip['origin'])
print (name)
I could use that rather than the "dig" line in the main bash script.

I run that from crontab every three minutes

Code: Select all

*/3 * * * * /home/pi/dnscheck.sh
I've also logged every IP address that has ever been assigned to my system since 8th Jan 2015.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

Return to “Python”