#!/usr/bin/perl # # This provides a fake CLI interface to a WRT54G using the ping hack # Written by Mina Naguib # # $Header: /cvsroot/wrt54gcli/wrt54gcli.pl,v 1.1.1.1 2004/05/21 20:01:41 mina Exp $ # # # CONFIGURATION: $IP = ""; $PORT = ""; $PASSWORD = ""; # END OF CONFIGURATION #################################### use strict; use vars qw($IP $PORT $PASSWORD $TERM $OUT); use Term::ReadLine; use LWP::UserAgent; use HTTP::Request::Common qw(POST); # # Init # $| = 1; $TERM = new Term::ReadLine("WRT54G Fake CLI") || die "Failed to initialize a new readline terminal: $!\n"; $OUT = $TERM->OUT || \*STDOUT; welcome(); sanity(); # # Main loop - get input and send it to process(), print return as output # while (defined($_ = $TERM->readline("wrt54g # "))) { if (/\S/) { $TERM->addhistory($_); print $OUT process($_), "\n"; } } # # Takes in a command # Talks with the WRT54G box # Returns the output # sub process { my $command = shift; my $response; my $browser = new LWP::UserAgent || die "Failed to create a new LWP::UserAgent browser: $!\n"; my $request = POST "http://$IP:$PORT/apply.cgi", [ "submit_button" => "Ping", "submit_type" => "start", "action" => "Apply", "change_action" => "gozila_cgi", "ping_times" => "1", # "`$command >/tmp/ping.log 2>&1`", "ping_ip" => "`$command >/tmp/ping.log 2>&1`", # "127.0.0.1", ]; $request->authorization_basic("", $PASSWORD); $response = $browser->request($request); if (!$response->is_success) { die "Failed to talk to http://$IP:$PORT/apply.cgi: " . $response->status_line() . "\n"; } else { $response = $response->content; $response =~ s/\r//g; if ($response =~ /new Array\(\n(.*?)\n\);\n/s) { $response = $1; $response =~ s/^,//gm; $response =~ s/^\s*//gm; $response =~ s/\s*$//gm; $response =~ s/^"//gm; $response =~ s/"$//gm; $response =~ s/\\"/"/g; return $response; } else { return "Error: Did not produce any result"; } } } # # Does sanity checking on the configs # sub sanity { $IP || die "IP address is undefined. Please edit $0 and configure the \$IP variable\n"; $PORT || die "PORT is undefined. Please edit $0 and configure the \$PORT variable\n"; $PASSWORD || die "PASSWORD is undefined. Please edit $0 and configure the \$PASSWORD variable\n"; } # # Prints the welcome message # sub welcome { print STDERR "\nWRT54G FAKE CLI\n"; print STDERR "Written by Mina Naguib \\n"; print STDERR "Released under the GPL license.\n"; print STDERR "ReadLine capabilities provided by ", $TERM->ReadLine(), "\n"; print STDERR "\nCurrent configuration: $IP:$PORT (if wrong, edit $0 and correct at top of file)\n"; print STDERR "\nRemember: You can not run any interactive programs, only simple stdin/stdout types\n"; print STDERR "\n"; }