Repository 32bit  Forum
Repository 64bit  Wiki

Script TCL - Aggiornamento IP servizio cjb.net

Da Slacky.eu.
Versione delle 10:49, 12 set 2006, autore: L1q1d (Discussione | contributi)

(diff) ← Versione meno recente | Versione attuale (diff) | Versione più recente → (diff)

Descrizione

Script TCL che permette di aggiornare il proprio IP da linea di comando per il servizio di dns dinamico di cjb.net. è possibile specificare se deve utilizzare l'ip di una determinata interfaccia di rete o se deve trovarlo da solo (usando whatismyip.com). Ovviamente richiede il pacchetto tcl.

Script

# CJB.tcl
#
# CJB.net IP updater by SukkoPera <enjoy.the.silence@iol.it>
# Version 0.5 (22/05/2005)
#
# Released under the GPL


### GENERAL SETTINGS ###

# CJB.net user data
set username ""
set password ""

# Path to ifconfig
set ifconfig "/sbin/ifconfig"

# Interface bearing the IP to set at CJB.net, if set to "" the program
# will try to automatically fetch the IP from www.whatismyip.com
set device "ppp0"


### DAEMON MODE SETTINGS ###

# Seconds to wait between consequent IP updates
set sleeptime 60

# File where to log, must have write access. Set to "" to turn off logging
set logfile "/var/log/cjb.log"


### DO NOT MODIFY THE FOLLOWING VALUES ###

set server "www.cjb.net"
set port "80"
set uri "http://www.cjb.net/cgi-bin/dynip.cgi"

set daemon 0
set lastip 0
set pretend 0

set version "0.5 (22/05/2005)"

###########################################################################

proc help {} {
	global username password ifconfig device sleeptime logfile

	puts \
"Available options:

-d	Daemon mode (Please launch as \"[info script] -d &\")
-p	Do not set IP at CJB.net, only show the detected IP

Configurable parameters:
- Username...........: $username
- Password...........: $password
- Path to ifconfig...: $ifconfig
- Network interface..: [expr {$device != "" ? $device : "None, will use whatismyip.com"}]
- Sleep time.........: $sleeptime seconds
- Log file...........: $logfile

You can change them editing the beginning of the script
"

	return
}


proc daemonize {} {
	global daemon

	puts "Running in daemon mode: closing standard input and output"
#	close stdin
#	close stdout
#	close stderr

	set daemon 1
	msg "Running in daemon mode"

	return
}


proc msg {what} {
	global daemon logfile

	set timestamp [clock format [clock seconds]]
	set what "\[$timestamp\] $what"

	if {$daemon} {
		if {$logfile != ""} {
			set fd [open $logfile "a"]
			puts $fd $what
			close $fd
		}
	} else {
		puts $what
		flush stdout
	}

	return
}
		

proc get_ip_ifconfig {device} {
	global ifconfig

	msg "Determining IP of interface $device"
	#return [exec $ifconfig $device | grep "inet addr" | awk "\{print \$2\}" | cut -b 6-]
	set fp [open "|$ifconfig $device" "r"]
	set out [read $fp]
	if {![catch {close $fp}]} {
		foreach line [split $out "\n"] {
			if {[regsub -all " +inet addr:(\[0-9\]+)\\\.(\[0-9\]+)\\\.(\[0-9\]+)\\\.(\[0-9\]+) .*" $line {\1.\2.\3.\4} ip]} {
				break
			}
		}
	} else {
		msg "Could not determine IP of interface $device"
		set ip ""
	}

	return $ip
}


proc get_ip_myip {} {
	set myhost "www.whatismyip.com"
	set myport 80
	
	msg "Trying to get IP from $myhost:$myport"
	set sock [socket $myhost $myport]
	fconfigure $sock -buffering line
	puts $sock "GET / HTTP/1.0"
	puts $sock "Host: $myhost"
	puts $sock ""
	set out [read $sock]
	close $sock
	
	# Take out all quotes as they cause problems with TCL lists
	regsub -all "\"" $out "" out
	
	# Only keep the line containing the IP
	foreach line [split $out "\n"] {
		if {[string match "*Your IP Is*" $line]} {
			break
		}
	}
	
	# Filter out the IP from the line
	regsub -all ".* (\[0-9\]+)\\\.(\[0-9\]+)\\\.(\[0-9\]+)\\\.(\[0-9\]+) .*" $line {\1.\2.\3.\4} ip
	
	return $ip
}


proc update {ip} {
	global username password server port uri

	msg "Opening connection to $server:$port"
	flush stdout

	if {![catch {set cjb [socket $server $port]} err]} {
		fconfigure $cjb -buffering line

		# Create and send HTTP request
		set req "GET $uri?username=$username&password=$password&ip=$ip HTTP/1.0"
#		msg "Request: $req"
		puts $cjb $req
		# Very important!
		puts $cjb ""

		# Read the output page
		set out [read $cjb]

		# All done!
		close $cjb
	} else {
		set out ""
		msg "ERROR: $err"
	}
	
	return $out
}


proc parse_reply {reply} {
	set l [split $reply \n]
#	msg "--> [llength $l] elements"

	set out 0
	foreach line $l {
#		msg "--> $line"
		if {[string match "*has been updated to point to*" $line]} {
#			msg "FOUND: $line"
			set out 1
			msg "Update succeeded!"
			break
		} elseif {[string match "*The specified password is incorrect*" $line]} {
			msg "Update failed: wrong password!"
			break
		}
	}

	return $out
}


proc check_options {} {
	global argv username password pretend

	if {$username == "" || $password == ""} {
		puts "Please edit the script and set username and password!"
		exit 0
	}

	foreach option $argv {
		if {[string index $option 0] != "-"} {
			msg "- Invalid option \"$option\""
		} else {
			set op [string range "$option" 1 end]
			switch $op {
				"d" {
					daemonize
					break
				} "p" {
					set pretend 1
					break
				} "h" {
					help
					exit 0
				} default {
					msg "- Invalid option \"$option\""
					break
				}
			}
		}
	}

	return
}


proc doit {} {
	global device daemon sleeptime lastip pretend

	set done 0
	while {!$done} {
		if {$device != ""} {
			set ip [get_ip_ifconfig $device]
		}
		if {$device == "" || $ip == ""} {
			set ip [get_ip_myip]
		}
		if {!$pretend && $ip != $lastip} {
			msg "Updating CJB.net IP to $ip"
			set res [update $ip]
			set out [parse_reply $res]
			set lastip $ip
		} elseif {!$pretend} {
			msg "No IP change detected"
		} else {
			msg "Not updating CJB.net IP since -p was used"
			set out 0
		}

		if {!$pretend && $daemon} {
			# Daemon mode: sleep for some time and then run again
			# We never return (unless killed :-))
			set sleepms [expr $sleeptime * 1000]
			msg "Sleeping for $sleeptime seconds"
			after $sleepms
		} else {
			# Single run mode, get outta here!
			set done 1
		}
	}

	return $out
}


proc welcome {} {
	global version

	puts \
"##############################################################
CJB.tcl V $version
Copyright (C) 2005 SukkoPera <enjoy.the.silence@iol.it>

This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions; see the GNU GPL for details.
##############################################################

Use -h for help
"

	return;
}


proc main {} {
	welcome
	check_options
	exit [expr ![doit]]
}


main
  • Data: 03 Jun 2006
  • Autore: Useless
  • Versione: 0.5#!/usr/bin/tclsh
Strumenti personali
Namespace

Varianti