Skip to content

Instantly share code, notes, and snippets.

@samm-git
Created August 24, 2015 14:13
Show Gist options
  • Save samm-git/69ca943d837e25124610 to your computer and use it in GitHub Desktop.
Save samm-git/69ca943d837e25124610 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use strict;
use LWP::UserAgent;
# ip address of the router
my $routerip = "192.168.8.1";
# ussd command to send, change for your ISP
my $ussd = "*123*523#";
# program starts here
my $ua = LWP::UserAgent->new;
$ua->agent("check_fup/0.1 ");
# request token from the modem
my $token = 0;
my $req = HTTP::Request->new(GET => 'http://'.$routerip.'/api/webserver/token');
my $res = $ua->request($req);
if ($res->is_success) {
die "Unable to parse token response" if $res->content !~ /<token>(\d+)<\/token>/;
$token = $1;
}
else {
die("Unable to get token from modem");
}
# call release api to exit ussd menu
$req = HTTP::Request->new(GET => 'http://'.$routerip.'/api/ussd/release');
$res = $ua->request($req);
my $xml = '<?xml version="1.0" encoding="UTF-8"?><request><content>'.$ussd.'</content><codeType>CodeType</codeType><timeout></timeout></request>';
$req = HTTP::Request->new(POST => 'http://'.$routerip.'/api/ussd/send',
[ ":__RequestVerificationToken" => $token ],
$xml);
$res = $ua->request($req);
die("unable to send USSD command") if !$res->is_success;
# sleep 5 seconds before reply. Web interface checks status here?
sleep 5;
# grab USSD reply and print it to the console
$req = HTTP::Request->new(GET => 'http://'.$routerip.'/api/ussd/get', [ ":__RequestVerificationToken" => $token ]);
$res = $ua->request($req);
if ($res->is_success) {
$res->content =~ /<content>(.+)<\/content>/sm;
print $1."\n"
}
else {
die("unable to get USSD reply");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment