"The Linux Gazette...making Linux just a little more fun!"


(?) The Answer Guy (!)


By James T. Dennis, [email protected]
Starshine Technical Services, http://www.starshine.org/


(?) Dynamic IP Address Publishing Hack

From Ronald Kuetemeier on Sat, 05 Dec 1998

(?) Here is an expect script that you might find useful for your article. It keeps a connection to the internet up and running with a dynamically assigned ip address. It updates html file(s) with the assigned ip address and ftps it to a well known server on the internet. Ronald

> =====================================================================
 #!/usr/bin/expect -f

 #expect script to keep a www server connected to the internet over
 #dynamically assigned ip address
 #Ronald Kuetemeier 11/1/1998 [email protected]
 #Replace all xxxx with your values

 #initial ppp server address to see if we are already up
 #change this to your ftp server ip addr.
 set server xxx.xxx.xxx.xxx

 #use of ppp script to make sure ppp is down and can be restarted
 #change this to your local ppp up/down script
 proc logon {} {
  system xxxx stop
  close
  wait
  sleep 10
  system xxxx start
  close
  wait
  sleep 35
  ping 1
 }

 #get ip's from ifconfig
 proc getip {} {
  spawn ifconfig
  expect -re "P-t-P:\[0-9\]+.\[0-9\]+.\[0-9\]+.\[0-9]+"

   close
   wait
   setip $expect_out(buffer)
  }

 }

 #find local ip and remote server ip address from ifconfig
 proc setip {out} {
  global server
  set ips [string range $out [string first "Point-to-Point" $out]
 [string length $out]]
  regexp  P-t-P:\[0-9\]+.\[0-9\]+.\[0-9\]+.\[0-9]+  $ips server_1
  regexp  addr:\[0-9\]+.\[0-9\]+.\[0-9\]+.\[0-9]+  $ips client_1
  regexp \[0-9\]+.\[0-9\]+.\[0-9\]+.\[0-9\]+ $server_1 server
  regexp \[0-9\]+.\[0-9\]+.\[0-9\]+.\[0-9\]+ $client_1 client
  changeaddr $client
 }

 #ping to see if connection is still up
 proc ping {i} {
  global server
  while {1} {
   if {$i == 6} {
    logon
    getip
    set $i 0
   }
   spawn ping -c 1 -n $server
   expect {
    "bytes from" break
    "100% packet loss" close
    ret=-1 close
   }
   wait
   incr i
   puts $i
   sleep 3
  }
   close
   wait
 }

 #change to your local userid and passwd and file transfer
 proc ftp {} {
 #change to your ftp server
  spawn ftp xxx.xxx.xxx
  expect "Name*:"
  send "xxxx\r"
  expect "Password:"
  send "xxxx\r"
  expect "ftp>"
 #change to your ftp server directory,i.e public_html
  send "cd xxxxx\r"
  expect {
 #change file to transfer             [file]
                "2*ftp>" [send "put xxxx.xxxx\r"]
                 "550*ftp>" ftp_error
  }
  expect {
 #change or delete file 2 transfer    [file 2]
                "2*ftp>" [send "put xxxx.xxxx\r"]
                "No such file" ftp_error
  }
  close
  wait
 }

 proc ftp_error {} {
  puts "FTP ERROR\n"
  close
  wait
 }

 # use sed to replace unique name with ip addr in a file
 proc changeaddr {client} {
 #change file names and local dns name
 #                     [DNS]              [in.file]   [out file]
  system sed 's/xxxx.xxxxxxx.xxx/$client/' xxx.xxxx > xxxx.xxxx
  close
  wait
 #change file names and local dns name or delete this
 #                      [DNS]               [in.file]   [out file]
  system sed 's/xxxx.xxxxxx.xxxx/$client/' xxxx.xxxx > xxxx.xxxx
  close
  wait
  ftp
 }


 ping 6

 while {1} {
  puts "Main loop\n"
  ping 1
  sleep 9
 }

(!) I'll just leave this as is. However, I'd suggest that the 'pppup' script documented in the 'pppd' man pages would provide some of the IP addresses that you are laboriously extracting from spawn command outputs using regexes.
Also It would make a lot of sense to write up an article around this script and publish that in LG yourself.


Copyright © 1999, James T. Dennis
Published in The Linux Gazette Issue 36 January 1999


[ Answer Guy Index ] a b c 1 2 3 4 5 6 7 9 10 11 12
15 16 18 19 20 21 22 23 24 25 26 27 28
29 31 32 33 34 35 36 37 38 39 40 41 42 44
45 46 47 48 49 50 51 52 53 54 55 56 57 60 61 62 63 64 65 66
67 69 72 76 77 78 79 80 81 82 84 85 86 87 91 94 95 96 97 98


[ Table Of Contents ] [ Front Page ] [ Previous Section ] [ Next Section ]