Re: Getting my WAN IP address from ppp0
Hi Zach, Well, this almost works for me. #!perl my $ifconfig = `ifconfig`; # the ifconfig command gives the current network information $ifconfig =~ /inet (\d+\.\d+\.\d+\.\d+)/; # extract the ip address with a regular expression This does extract my IP address, but the one for the loopback device lo0. The ifconfig output looks like this: lo0: flags=8049UP,LOOPBACK,RUNNING,MULTICAST mtu 16384 inet6 ::1 prefixlen 128 inet6 fe80::1 prefixlen 64 scopeid 0x1 inet 127.0.0.1 netmask 0xff00 gif0: flags=8010POINTOPOINT,MULTICAST mtu 1280 stf0: flags=0 mtu 1280 en0: flags=8863UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST mtu 1500 tunnel inet -- ether 00:30:65:ef:a8:f0 media: autoselect (10baseT/UTP half-duplex) status: active supported media: none autoselect 10baseT/UTP half-duplex 10baseT/UTP full-duplex 10baseT/UTP full-duplex,hw-loopback 100baseTX half-duplex 100baseTX full-duplex 100baseTX full-duplex,hw-loopback 1000baseTX full-duplex 1000baseTX full-duplex,hw-loopback 1000baseTX full-duplex,flow-control 1000baseTX full-duplex,flow-control,hw-loopback fw0: flags=8822BROADCAST,SMART,SIMPLEX,MULTICAST mtu 2030 tunnel inet -- lladdr 00:30:65:ff:fe:ef:a8:f0 media: autoselect full-duplex status: inactive supported media: autoselect full-duplex ppp0: flags=8051UP,POINTOPOINT,RUNNING,MULTICAST mtu 1492 inet 212.212.2.212 -- 212.128.128.12 netmask 0xff00 Now I would like to start reading the output from just the last line. Is there anyway to start reading it from the end to the beginning and stop once the inet line is found? my $ip = $1; what's $1? Why is that needed? I suppose it is an internal perl variable like $_, right? The reason I don't want to use dyndns is, that I don't completely trust it and I might sometimes not need it for more than 30 days which leads to expiration of the address if I read things correctly. Thanks a lot for your help. Stephan
Re: Getting my WAN IP address from ppp0
Hi Zach, Well, this almost works for me. #!perl my $ifconfig = `ifconfig`; # the ifconfig command gives the current network information $ifconfig =~ /inet (\d+\.\d+\.\d+\.\d+)/; # extract the ip address with a regular expression This does extract my IP address, but the one for the loopback device lo0. The ifconfig output looks like this: lo0: flags=8049UP,LOOPBACK,RUNNING,MULTICAST mtu 16384 inet6 ::1 prefixlen 128 inet6 fe80::1 prefixlen 64 scopeid 0x1 inet 127.0.0.1 netmask 0xff00 gif0: flags=8010POINTOPOINT,MULTICAST mtu 1280 stf0: flags=0 mtu 1280 en0: flags=8863UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST mtu 1500 tunnel inet -- ether 00:30:65:ef:a8:f0 media: autoselect (10baseT/UTP half-duplex) status: active supported media: none autoselect 10baseT/UTP half-duplex 10baseT/UTP full-duplex 10baseT/UTP full-duplex,hw-loopback 100baseTX half-duplex 100baseTX full-duplex 100baseTX full-duplex,hw-loopback 1000baseTX full-duplex 1000baseTX full-duplex,hw-loopback 1000baseTX full-duplex,flow-control 1000baseTX full-duplex,flow-control,hw-loopback fw0: flags=8822BROADCAST,SMART,SIMPLEX,MULTICAST mtu 2030 tunnel inet -- lladdr 00:30:65:ff:fe:ef:a8:f0 media: autoselect full-duplex status: inactive supported media: autoselect full-duplex ppp0: flags=8051UP,POINTOPOINT,RUNNING,MULTICAST mtu 1492 inet 212.212.2.212 -- 212.128.128.12 netmask 0xff00 If you want the ip address of PPPoE connection, change the first line of the script to my $ifconfig = `ifconfig ppp0`; HTH, Rick Smith
Solution: Getting my WAN IP address from ppp0 and upload via ftp
Hey Zach, Rick and everyone who helped me on this: I just finished a very simple script that reads out my IP-address from the ppp0 device, creates a small HTML file and uploads it to my webhoster so i can access my g4 from anywhere in the world. The script is the patchwork of a beginner, so any advice on making it nicer is greatly appreciated. It is run as a cronjob to assure the up-to-dateness of the IP. Anyway, here's the script: #!/usr/bin/perl use Net::FTP; #all the neccessary info for loggin into the ftp server my $hostname = 'myftpserver.com'; my $user ='username'; my $pass = 'password'; #where on the server should which file go? my $dir = '/public_html'; my $file = '/Users/Shared/g4server.html'; #read out my ip address my $ifconfig = `ifconfig ppp0`; # the ifconfig command gives the current network information $ifconfig =~ /inet (\d+\.\d+\.\d+\.\d+)/; # extract the ip address with a regular expression my $ip = $1; # write it to a html file open (OUTPUT,/Users/Shared/g4server.html); print OUTPUT HTMLHEADTITLEg4server address/TITLE/HEADBODY; print OUTPUT H1Access g4server.local/H1\n; print OUTPUT 'a href=http://'; print OUTPUT $ip; print OUTPUT Apache/abr /\n; print OUTPUT 'a href=https://'; print OUTPUT $ip; print OUTPUT :1Webmin/abr /\n; print OUTPUT /BODY/HTML; close (OUTPUT); #upload the html file $ftp = Net::FTP-new($hostname); $ftp-login($user, $pass); $ftp-cwd($dir); $ftp-put($file); $ftp-quit; Maybe it will brighten up some other beginners day :-) Stephan
Re: Solution: Getting my WAN IP address from ppp0 and upload via ftp
* Stephan Hochhaus [EMAIL PROTECTED] I just finished a very simple script that reads out my IP-address from the ppp0 device, creates a small HTML file and uploads it to my webhoster so i can access my g4 from anywhere in the world. The script is the patchwork of a beginner, so any advice on making it nicer is greatly appreciated. It is run as a cronjob to assure the up-to-dateness of the IP. Anyway, here's the script: #!/usr/bin/perl use strict; use warnings; use Net::FTP; #all the neccessary info for loggin into the ftp server my $hostname = 'myftpserver.com'; my $user ='username'; my $pass = 'password'; #where on the server should which file go? my $dir = '/public_html'; my $file = '/Users/Shared/g4server.html'; #read out my ip address my $ifconfig = `ifconfig ppp0`; # the ifconfig command gives the current network information $ifconfig =~ /inet (\d+\.\d+\.\d+\.\d+)/; # extract the ip address with a regular expression my $ip = $1; # no sense in writing no data if we did not get any... die error: could not parse address from ifconfig unless $ip; # write it to a html file open (OUTPUT,/Users/Shared/g4server.html); # you declare $file above, but do no use it here? # anyways, error checking is a must! open (OUTPUT, $file) or die error: could not write $file: $!\n; print OUTPUT HTMLHEADTITLEg4server address/TITLE/HEADBODY; print OUTPUT H1Access g4server.local/H1\n; print OUTPUT 'a href=http://'; print OUTPUT $ip; print OUTPUT Apache/abr /\n; print OUTPUT 'a href=https://'; print OUTPUT $ip; print OUTPUT :1Webmin/abr /\n; print OUTPUT /BODY/HTML; # a here document is much shorter, and cleaner: print OUTPUT HTMLSTUFF; HTMLHEADTITLEg4server address/TITLE/HEADBODY H1Access g4server.local/H1 pa href=http://$ip/;Apache/a/p pa href=https://$ip:1/;Webmin/a/p /body/html HTMLSTUFF close (OUTPUT); # when writing to a file, *always* check the status on close, as this is # when you figure out that the disk is full or something else went wrong # and have a chance to log the error or fallback... close OUTPUT or die error: problem closing $file: $!\n; #upload the html file $ftp = Net::FTP-new($hostname); $ftp-login($user, $pass); $ftp-cwd($dir); $ftp-put($file); $ftp-quit; There's probably error checking that can be done on the FTP calls, as well... Other improvements would be to write out the HTML file to a temporary file (see the File::Temp perl module), and doing a rename on that to the proper filename, but only if the files are different. This would allow you to skip uploading to FTP should nothing have changed, and reduce the chance that you hit the page when the file is being updated.