From bd1a6723bafed30d7edd496f3cdf530472b89798 Mon Sep 17 00:00:00 2001 From: Michael Gernoth Date: Tue, 3 Feb 2009 21:56:25 +0100 Subject: [PATCH] config file for perl client --- rsbs2 => rsbs2.pl | 107 ++++++++++++++++++++++++++++++---------------- rsbs2rc | 8 ++++ 2 files changed, 77 insertions(+), 38 deletions(-) rename rsbs2 => rsbs2.pl (86%) create mode 100644 rsbs2rc diff --git a/rsbs2 b/rsbs2.pl similarity index 86% rename from rsbs2 rename to rsbs2.pl index 51a02d5..5255b58 100755 --- a/rsbs2 +++ b/rsbs2.pl @@ -8,10 +8,7 @@ use Digest::MD5 qw(md5); my $ua = LWP::UserAgent->new; my $sid; - -my $host = "192.168.50.6"; -my $user = "admin"; -my $pass = "admin"; +my $host; my @fw_vars = qw(ENABLE_LAN_AUTONEG ENABLE_LAN_100 ENABLE_LAN_FDUPLEX GATEWAY IP_ADDRESS NETMASK TFTP_FIRMWARE_FILE TFTP_ADDR_FIRMWARE ENABLE_DHCP @@ -229,6 +226,17 @@ sub board_properties { } } +sub show_boarddesc { + my $reqstr=''; + my $boarddesc64 = _req($reqstr)->{RESP}->{BPROPLIST}->{BPROP}->{VAL}; + my $boarddesc = decode_base64($boarddesc64); + my @board = split(//, $boarddesc); + foreach my $byte (@board) { + printf ("0x%02x ", ord($byte)); +} +print "\n"; +} + sub show_all_vars { foreach my $fwvar (@fw_vars) { showprop($fwvar); @@ -256,41 +264,64 @@ sub syslog_debug { print "Debug messages will be sent to ${destination_ip} (broadcast: ${bcast})\n"; } -#Login... -my $response = $ua->get("http://${host}/cgi/challenge"); -die $response->status_line if (!($response->is_success)); - -my $xmlin = XMLin($response->decoded_content); -die "Error getting Challenge: ".$xmlin->{RC} if ($xmlin->{RC} ne '0x0'); -my $challenge = $xmlin->{CHALLENGE}; -print "Challenge: ${challenge}\n"; - -$sid = $response->headers->header('Set-Cookie'); -die "No SessionID!" if (!defined($sid)); -chomp($sid); -$sid =~ s/.*sid=(.*);.*/$1/; -print "SID: ${sid}\n"; - -my $login_hash = _hash($pass, $challenge); -print "Hash: ${login_hash}\n"; - -my $request = HTTP::Request->new(GET => "http://${host}/cgi/login?user=${user}&hash=${login_hash}"); -$request->header(Cookie => "sid=$sid"); -$response = $ua->request($request); -die("While trying to login: " . $response->status_line . "\n") unless ($response->is_success); - -$xmlin = XMLin($response->decoded_content); -die "Error logging in: ".$xmlin->{RC} if ($xmlin->{RC} ne '0x0'); - -$reqstr=''; -my $boarddesc64 = _req($reqstr)->{RESP}->{BPROPLIST}->{BPROP}->{VAL}; -my $boarddesc = decode_base64($boarddesc64); -my @board = split(//, $boarddesc); -foreach my $byte (@board) { - printf ("0x%02x ", ord($byte)); +sub login { + my $user = shift; + my $pass = shift; + + my $response = $ua->get("http://${host}/cgi/challenge"); + die $response->status_line if (!($response->is_success)); + + my $xmlin = XMLin($response->decoded_content); + die "Error getting Challenge: ".$xmlin->{RC} if ($xmlin->{RC} ne '0x0'); + my $challenge = $xmlin->{CHALLENGE}; + print "Challenge: ${challenge}\n"; + + $sid = $response->headers->header('Set-Cookie'); + die "No SessionID!" if (!defined($sid)); + chomp($sid); + $sid =~ s/.*sid=(.*);.*/$1/; + print "SID: ${sid}\n"; + + my $login_hash = _hash($pass, $challenge); + print "Hash: ${login_hash}\n"; + + my $request = HTTP::Request->new(GET => "http://${host}/cgi/login?user=${user}&hash=${login_hash}"); + $request->header(Cookie => "sid=$sid"); + $response = $ua->request($request); + die("While trying to login: " . $response->status_line . "\n") unless ($response->is_success); + + $xmlin = XMLin($response->decoded_content); + die "Error logging in: ".$xmlin->{RC} if ($xmlin->{RC} ne '0x0'); + + $sid; } -print "\n"; -printf("byte 22: 0x%x\n", ord($board[22])); + +open(INIFILE,"<$ENV{HOME}/.rsbs2rc") || die("can't open config: $ENV{HOME}/.rsbs2rc: $!"); +my %Config = (); +my @sections = (); +while() { + chomp; + + next if (m/^#/); + + if (m/^\s*\[(.*)\]\s*$/) { + push @sections, $1; + next; + } + + if (@sections) { + if (m/^\s*(.+)\s*=\s*(.*)\s*$/) { + ${$Config{$sections[$#sections]}}{$1} = $2; + } + } +} +close(INIFILE); + +$host = ${$Config{$sections[0]}}{"host"}; + +$sid = login(${$Config{$sections[0]}}{"user"}, ${$Config{$sections[0]}}{"pass"}); + +show_boarddesc(); #showprop("FP_REMOTE_POWER"); #showprop("FP_REMOTE_BOOT"); diff --git a/rsbs2rc b/rsbs2rc new file mode 100644 index 0000000..2bfac85 --- /dev/null +++ b/rsbs2rc @@ -0,0 +1,8 @@ +# This is a template for ~/.rsbs2rc + +[test] +host=192.168.50.6 +user=admin +pass=admin +;poweronms=200 +;poweroffms=4500 -- 2.39.5