Staticddns

From wikinotes


This is not a program, but a system for regularly pushing my IP address to my website so that I can access my home servers from anywhere. This system isn't ideal, but it is passable, and free (at least so long as I continue to pay for my website).


website

First, setup an FTP server on your website. This isn't the ideal method of sending files, but it was the only automatable option I had.



cron script

update script

## Gets My External IP
#curl -G http://checkip.dyndns.com | awk -F" " '{print $6}' | awk  -F"<" '{print $1}' > /home/cron/ddns
dig +short myip.opendns.com @resolver1.opendns.com > /home/cron/ddns


## Ensures that curl didn't time out before uploading
## new IP to website
if [[ "`cat /home/cron/ddns`" != "" ]] ; then

#echo 'test'
ftp ftp://<user>:<password>@ftp.webhostname.com <<!
prompt off
binary
put "/home/cron/ddns" "/ddns"
bye
!

fi

crontab

SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin

#minute hour    mday    month   wday    command
*/30    *       *       *       *       /home/cron/uploadWAN.sh


git script

#!/usr/bin/env bash
# .git-rip

# PreRequisites:
#	- bash
#	- sed
#	- grep
#	- ls (or gnuls if BSD)


# git-rip -->> git-(r)efresh(ip)
# This script pulls my local IP from willpittman.com, and 
# replaces the address in any remote matching git@x.x.x.x
# script must be run from top of repo heirarchy

# arg path, or current path
if [[ "$1" == "" ]] ; then
	curDir=$( pwd )
else 
	curDir="$1"
fi



# navigate up tree until '/' or find '.git'
testDir=$curDir
function findRootGit() {
	foundGit=$( ls -a $1 | grep -Fx .git  )
	numDirs=$( grep -o '/' <<<"$1" | grep -c . )
	numDirs=$( expr $numDirs + 1 )						#cut range 0-9 does 0-8

	if [ "$foundGit" == ".git" ] ; then
		gitRoot=$1
		return 1

	elif [ "$numDirs" == "1" ] ; then
		echo
		echo "Error: could not find root of git project (contains .git)"
		echo "       Are you sure you are currently in a git project?"
		echo
		return 0

	else
		numDirs=$( expr $numDirs - 1 )
		testDir=$( echo $1 | cut -d'/' -f1-$numDirs )
		findRootGit $testDir

	fi
}
findRootGit $curDir




# if found git, change remotes' IPs
if [ "$gitRoot" != "" ] ; then
	leeboIP=$(curl -sG www.willpittman.com/ddns/ddns)

	if [[ "$leeboIP" != "" ]] ; then
		echo
		echo "Project Root: $gitRoot"
		echo 'Server IP: '$leeboIP
		echo
		
		## copy-on-write
		cp "$gitRoot/.git/config" /tmp/tmp-config
		sed -i "s_git@[0-9]*[.][0-9]*[.][0-9]*[.][0-9]*:8610_git@${leeboIP}:8610_" /tmp/tmp-config
		mv "$gitRoot/.git/config" "$gitRoot/.git/config_old"
		cp /tmp/tmp-config "$gitRoot/.git/config"


	else
		echo "Error obtaining lavos IP"
	fi
fi



Local web Redirection

For visiting websites, you can run a local-only webserver with PHP, and use PHP to collect the remote ip, and forward requests to your actual home address. I have forgotten how exactly I was doing this, but I believe I have a script or two in my PHP repository.