I use the following perl script to update my dynamic domain name from the
ifup command in my diald.conf, but it doesn't exit upon completion. I was
hoping someone could point out the problem.
#!/usr/bin/perl -w
use Socket;
use strict;
my $configfile = "$ENV{'HOME'}/.jldyndns";
my $defaultserver = "dyndns.justlinux.com";
my $defaultport = "2345";
my $proto_version = 1.0;
my $description = "[justlinux.com dyndns client v1.1]";
my $lastok;
my @validkeys = ( "server", "port", "username", "password", "hostname",
"ip", "conf" );
my @requiredkeys = ( "username", "password", "hostname" );
my $operation = "update";
my %config;
my $argc = @ARGV;
my $configure = 0;
local (*SOCK);
for (my $i = 0; $i < $argc; $i ++) {
if ($ARGV[$i] =~ /^--/) {
my ($key, $value) = parse_arg($ARGV[$i]);
if (valid_key($key)) {
$config{$key} = $value;
}
}
elsif (lc($ARGV[$i]) eq "-u") {
$operation = "update";
}
elsif (lc($ARGV[$i]) eq "-d") {
$operation = "delete";
}
elsif (lc($ARGV[$i]) eq "-c") {
$configure = 1;
}
}
if ($config{'conf'}) {
$configfile = $config{conf};
}
if ($configure) {
configure();
exit(1);
}
read_data($configfile, \%config);
check_required();
if (!$config{'ip'}) {
$config{'ip'} = "auto";
}
if (!$config{'server'}) {
$config{'server'} = $defaultserver;
}
if (!$config{'port'}) {
$config{'port'} = $defaultport;
}
if (connect_server(\*SOCK, $config{'server'},$config{'port'})) {
my $msg = "";
if (! ($msg = get_data(\*SOCK))) {
if ($msg = command(\*SOCK, "VER $proto_version $description")) {
server_error($msg);
}
if ($msg = command(\*SOCK, "USER $config{'username'}")) {
server_error($msg);
}
if ($msg = command(\*SOCK, "PASS $config{'password'}")) {
server_error($msg);
}
if ($msg = command(\*SOCK, "HOST $config{'hostname'}")) {
server_error($msg);
}
if ($msg = command(\*SOCK, "OPER $operation")) {
server_error($msg);
}
if (lc($operation) eq "update") {
if ($msg = command(\*SOCK, "IP $config{'ip'}")) {
server_error($msg);
}
}
if ($msg = command(\*SOCK, "DONE")) {
server_error($msg);
}
close(SOCK);
print $lastok, "\n";
}
else {
server_error($msg);
}
}
exit(1);
sub connect_server {
my ($socket, $server, $port) = @_;
my $iaddr = inet_aton($server) || comm_error("Error resolving host:
$server");
my $paddr = sockaddr_in($port, $iaddr);
my $proto = getprotobyname('tcp');
socket($socket, PF_INET, SOCK_STREAM, $proto) ||
comm_error("Can't create socket: $!");
connect($socket, $paddr) ||
comm_error("Can't connect to $server on port $port\n$!");
select($socket);
$| = 1;
select(STDOUT);
}
sub comm_error {
my ($msg) = @_;
print "Communications error\n";
print $msg;
exit(0);
}
sub server_error {
my ($msg) = @_;
print "Update failed\n";
print "server returned: $msg\n";
exit(0);
}
sub get_data {
my ($sock) = @_;
my $line = <$sock>;
chomp $line;
if ($line =~ /^OK/i) {
if (length($line) > 2) {
$lastok = substr($line,3,length($line));
}
return undef;
}
else {
return substr($line,4,length($line));
}
}
sub send_data {
my ($sock, $msg) = @_;
print $sock "$msg\n";
}
sub command {
my ($sock, $msg) = @_;
send_data($sock, $msg);
if (my $err = get_data($sock)) {
return $err;
}
else {
return undef;
}
}
sub read_data {
my ($file, $config) = @_;
open(DATA, $file);
while (!eof(DATA)) {
my $line = <DATA>;
chomp $line;
if ($line ne "" && $line !~ /^\#/) {
my ($key, $value) = split(/=/,$line);
$key = lc($key);
if (valid_key($key)) {
if (!$$config{$key}) {
$$config{$key} = $value;
}
}
else {
close(DATA);
usage_error("Unkown parameter '$key'");
}
}
}
close(DATA);
}
sub parse_arg {
my ($arg) = @_;
$arg =~ s/^--//;
my ($key, $value) = split(/=/,$arg);
return (lc($key), $value);
}
sub valid_key {
my ($key) = @_;
if (grep /^$key$/, @validkeys) {
return 1;
}
else {
return 0;
}
}
sub usage_error {
my ($msg) = @_;
print "Error: $msg\n";
print "\n";
print "Usage: $0 [OPTIONS]\n";
print "\n";
print " -c create config file: `$configfile`\n";
print " -u update dns entry [DEFAULT] \n";
print " -d delete dns entry (go offline)\n";
print "\n";
print "The following options will be read from the config file but they can
can be \n";
print "overridden by using one of these command line arguments:\n\n";
print " --conf=... alternate config file to be used\n";
print " --server=... server used for connection (default:
'$defaultserver')\n";
print " --port=... port number used for connection (default:
'$defaultport')\n";
print " --username=... username to authenticate with\n";
print " --password=... your password\n";
print " --hostname=... the name of the host you are updating or
deleteing\n";
print " --ip=... the ip address to send (default is your current ip
address)\n";
print "\n";
exit(0);
}
sub check_required {
foreach my $key (@requiredkeys) {
if (!$config{$key}) {
usage_error("No '$key' specified");
}
}
}
sub configure {
my ($server, $port, $username, $password, $hostname);
print "Storing configuration in: '$configfile'\n";
print "\n";
print " Dynamic DNS server [$defaultserver]: ";
chomp($server = <STDIN>);
$server = $server ? $server : $defaultserver;
print " port [$defaultport]: ";
chomp ($port = <STDIN>);
$port = $port ? $port : $defaultport;
print " Username: ";
chomp ($username = <STDIN>);
print " Password: ";
chomp ($password = <STDIN>);
print " Hostname (i.e. host.penguinpowered.com): ";
chomp ($hostname = <STDIN>);
open (CONFIG, ">$configfile") || die "Error opening `$configfile` for
writing\n$!\n";
print CONFIG "SERVER=$server\n";
print CONFIG "PORT=$port\n";
print CONFIG "USERNAME=$username\n";
print CONFIG "PASSWORD=$password\n";
print CONFIG "HOSTNAME=$hostname\n";
close(CONFIG);
chmod (0600, $configfile);
print "\nConfiguration updated\n";
}
-
To unsubscribe from this list: send the line "unsubscribe linux-diald" in
the body of a message to [EMAIL PROTECTED]