💾 Archived View for gemini.mingmengtou.org › publicipscript-source.gmi captured on 2023-12-28 at 15:40:46. Gemini links have been rewritten to link to archived content

View Raw

More Information

⬅️ Previous capture (2021-12-04)

-=-=-=-=-=-=-

neil in gemini space

public ip script source - 2021-03-16

i am always keen to learn so i am open to suggestions for improvement. one of the points for me writing this script was to keep the process simple, understandable, well commented, and for me to learn. i will likely smile politely and then ignore suggestions to get this done in one line of [your favourite lang] and regex's:-)

#!/bin/bash
# getpublicip.sh
#
# what this does
#
# get a public ip using a source randomly (sorta) selected
#    from a text file of public ip urls: publicipsources.txt
#	but change that filename if you want
#	we are playing nice and not bothering any source too much
#    check a log for our last ip
#	if it has changed
#		log the date time source url and ip: publicip.log
#			change that filename if you want
# 	publicip log uses space as a delimiter
#	change DEFAULT_PUBLIC_IP_SOURCE to whatever you want
#	use absolute pathnames if you are going to cron this
#
# this script will expect a file with public ip sources but
#	will make it's own if necessary and not break
# 
# useage
# 
#	if you extracted this from the tar.gz then this will
# 		be in a directory: publicipscript
#	make the script executable and run it
#	or run it from bash or sh
#	or make the script executable and run it from cron
#	
# changelog
#
# 20210302: first working version
# 20210304:
#	- changed var names closer to bash convention
#	- added file exists check for sources and a default source
# 20210306:
# 	- added line to remove whitespace from curl response
#	- added check for empty or non numeric first char
#		curl response and set to "fail..."
#	- added even more comments for clarity re curl 
# 20210312:
#	- fixed bug for detecting first ip digit: it can be 0-9
# 20210313:
#	- changed date format to +%Y-%m-%d-%H:%M
#	- moved some comments to the top of the file
#	- added usage
# 2021-03-16:
#	- changed date format again to be isoish: -u +%FT%TZ
# 
#####################################################################
#####################################################################
# Copyright [2021] [neil c timms]
# 
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#    
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.#
#####################################################################
#####################################################################
# how not to write a bash script or get this done efficiently:-)
# 
# var for publicip.log, sources, and defaults
PUBLIC_IP_LOG=publicip.log
PUBLIC_IP_SOURCES=publicipsources.txt
last_ip="none"
DEFAULT_PUBLIC_IP_SOURCE="http://ifconfig.me"
#
# check that our publicip.log exists
if [ -f "$PUBLIC_IP_LOG" ]; then
# save our last ip from publicip.log
	last_ip=$(tail -1 $PUBLIC_IP_LOG | awk '{print $3}')
fi
# if it doesn't exist then we already have a last_ip
#	and will write PUBLIC_IP_LOG anyway so no need to make it now
#
# check that the publicipsources.txt file exists else make it
# 	else make our sources file using our default source
# get a random public ip source url for publicipsources.txt
if [ -f "$PUBLIC_IP_SOURCES" ]; then
	ip_source=$(shuf -n 1 $PUBLIC_IP_SOURCES)
else
	# we will make our PUBLIC_IP_SOURCES file
	#	and set an ip_source
	echo $DEFAULT_PUBLIC_IP_SOURCE >> $PUBLIC_IP_SOURCES
	ip_source=$DEFAULT_PUBLIC_IP_SOURCE
fi
#
# curl the public ip_source to set public_ip using curl option -s, silent
# 	if curl fails to get a valid ip then public_ip can be empty
#		so we could check if empty with [ -z etc...
#			for info -n will check if not empty
#		but what if we get a webpage back and it starts with <? so
#		if public_ip doesn't start with 0-2 as an ip should
#			set it to "fail.etc" this also works if empty
public_ip=$(curl -s $ip_source)
# strip off any whitespace that might be there, or not
public_ip="${public_ip//[[:space:]]/}"
if [[ ! $public_ip =~ ^[0-9] ]]
then
	public_ip="fail.to.get.ip"
fi
#
# if the ip changed then log the new ip else do nothing
if [ "$last_ip" != "$public_ip" ]
then
# log the date time and public ip to publicipsources.txt
# 	we use >> to make publicip log if missing
	echo "$(date -u +%FT%TZ) $ip_source $public_ip" >> $PUBLIC_IP_LOG
# for debugging uncomment the next two lines for logging all gets
# else
	# echo "$(date -u +%FT%TZ) $ip_source $public_ip" >> $PUBLIC_IP_LOG
fi
# exit - that's all folks!

content licensed CC-BY-SA 4.0 unless stated.

creative commons licence information.