画面上でドラッグしてコピー&ペーストしてください。ソースでは&や<>が化けます。
#!/usr/local/bin/perl
use Net::Telnet ();
use MIME::Base64 ();
use Sys::Syslog qw(:DEFAULT setlogsock);
use IO::Socket;
# YOUR hn.org name and password go here.
$username = "xxxxxxxx";
$password = "yyyyyyyy";
my ($RouterAddr, $RouterName, $RouterPass);
$RouterName = 'zzzzzzzz';
$RouterAddr = 'aaa.bbb.ccc.ddd';
$RouterPass = '********';
$logfile = '/var/log/hn-tx4l.log';
sub GetRouterInfo{
my(@lines);
$t = new Net::Telnet ( Timeout => 10 );
$t->open($RouterAddr);
$t->waitfor('/Password:.*$/');
$t->print($RouterPass);
$t->waitfor('/Number:.*$/');
$t->print("24");
$t->waitfor('/Number:.*$/');
$t->print("8");
$t->prompt("/$RouterName\> /");
my @cmd = ("ip ifconfig enif1", "exit");
foreach(@cmd){
push(@lines, $t->cmd("$_"));
}
$t->waitfor('/Number\:.*$/');
$t->print("99");
$t->close;
return(@lines);
}
sub GetRouterAddr{
my($ip);
my(@info) = &GetRouterInfo;
foreach(@info){
if (/inet\s*(\d*\.\d*\.\d*\.\d*)\,/) {
$ip = $1;
last;
}
}
return($ip);
}
sub OldIp{
open(LOG, "$logfile") || die("Can't open $logfile :$!\n");
my @lines = <LOG>
close(LOG);
my($ti, $ip);
$ip = 'nodata';
foreach(@lines){
if (/^(\d*)\s\[.*\]\s(\d*\.\d*\.\d*\.\d*)$/) {
$ti = $1; $ip = $2;
last;
}
}
return($ti, $ip);
}
sub Write{
my($ti, $dt, $ip) = @_;
open(LOG, "$logfile");
my @lines = <LOG>
close(LOG);
unshift(@lines, "$ti [$dt] $ip\n");
open(LOG, ">$logfile");
print LOG @lines;
close(LOG);
}
sub GetDateTime{
my $ti = time;
my @t = localtime($ti);
my @z = ('00' .. '99', '00' .. '99');
my $dt="$z[$t[5]]/$z[$t[4]+1]/$z[$t[3]] $z[$t[2]]:$z[$t[1]]:$z[$t[0]]";
return($ti, $dt);
}
### MAIN ###
($sec, $datetime) = &GetDateTime;
$myip = &GetRouterAddr;
print "$sec [$datetime] $myip\n";
($oldsec, $oldip) = &OldIp;
if ($oldip eq "nodata"){
print "This is the first try! Effective from next time.\n";
&Write($sec, $datetime, $myip);
exit;
}
elsif ($myip eq $oldip){
if ($sec - $oldsec > 2592000) {
print "30 days from the last updating. HN is accessed.\n";
&Write($sec, $datetime, $myip);
} else {
print "Ok. Ip address has not changed.\n";
exit;
}
} else {
print "IP address had changed. HN is accessed.\n";
&Write($sec, $datetime, $myip);
}
### hammernode.pl start ###
# If you're behind a firewall or HTTP proxy, set this to 1.
# If you're not sure, set it to 1; that's the safer setting anyway.
# If you KNOW you're not behind a firewall or proxy, set to 0.
$firewall = 1;
# Okay, that's all you'll need to configure *here*. You're not done,
# though... You still need to configure this to run automatically, and to
# use the correct IP address. For Linux users with pppd, the easiest way
# to put a line like this in /etc/ppp/ip-up :
#
# /usr/local/sbin/hammernode.pl $4
#
# If that doesn't make much sense, see `man pppd' which details what
# parameters pppd sends to ip-up.
#
# A clever trick for dhcpcd users... put this in your
# /etc/dhcpcd/dhcpcd-eth0.exe file:
#
# source /etc/dhcpc/dhcpcd-eth0.info && /usr/local/sbin/hammernode.pl $IPADDR
#
# (I discovered this was necessary -- my cable modem company puts an
# "invisible" HTTP proxy in between me and the 'net, and my domains were
# being assigned the address of the proxy.
#
# (C)2000-2001 David E. Smith <dave@bureau42.com> and released to the
# public under the terms of the GNU General Public License.
#
# Modified by Daniel Hagan <dhagan@colltech.com> on 4/2001 to use IO::Socket,
# Syslog, and some error checking. Now logs all output to daemon facility.
#
# Other changes made/suggested by Aurelien Beaujean <aure@dagobah.eu.org>
# Sorry, but I can't type the accent over the first "e" in Aurelien.
#
###############
# If you don't let syslogd listen on an inet port, uncomment this line.
#
#setlogsock 'unix';
openlog ("hammernode", 'pid', 'daemon');
#$myip = shift;
if ($firewall && !$myip) {
syslog ('err', "FATAL: IP required as command line argument when \$firew
all is set to true.");
exit(1);
}
$target = "dup.hn.org";
$version = "v0.22pl0";
$pass = MIME::Base64::encode_base64("$username:$password");
if ($firewall == 1) {
$url = "/vanity/update/?VER=1&IP=$myip" ;
} else {
$url = "/vanity/update/?VER=1" ;
}
syslog ('info', 'Setting domain to %s.', $myip);
$sock = new IO::Socket::INET(
PeerAddr => "$target",
PeerPort => 'http(80)'
);
if (!$sock) {
syslog('err', "FATAL: Connect to $target, port 80, failed: %m");
exit(1);
}
$sock->autoflush(1);
$sock->print("GET $url HTTP/1.0\r\n");
$sock->print("User-Agent: hammenode.pl $version\r\n");
$sock->print("Host: $target\r\n");
$sock->print("Authorization: Basic $pass\r\n\r\n");
# Legacy sleep from v0.21pl2. May be necessary on some slow PC platforms.
# If you are having problems, try uncommenting this first.
#
#sleep 5;
@result = $sock->getlines();
undef $sock; #Close the socket
$result = join '', @result;
$result =~ m/DDNS_Response_Code=(\d+)/i;
$code = $1;
if ($code == 101) {
syslog ('info', "Succeeded in setting domain to $myip.");
exit(0);
} else {
syslog ('notice', "Received DDNS Reponse Code $code, probably failed.");
exit(1);
}
|
自分の環境に合わせて変更する HN.ORGのユーザネームとパスワード BLR-TX4Lのsystem name, LAN側のIPアドレス, ログインパスワード 好きなログファイル名をフルパスで指定。 あらかじめ「touch ファイル名」としてファイルを作成し、アクセス権限を設定しておく。 60s*60min*24h*30days=2592000s (前回の更新から30日経ったら再更新) ここからhammernode.pl スタート $myip にはルータから取得したアドレスが入っているのでコメントにする。 |