Created
January 20, 2013 09:39
-
-
Save syxanash/4577529 to your computer and use it in GitHub Desktop.
it simply switches on and off the wireless device, on Hamlet routers.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
# (C) syxanash 2011 - syxanash[ at ] gmail.com | |
use strict; | |
use warnings; | |
use WWW::Mechanize; | |
use Getopt::Long; | |
use Tk; | |
use vars qw($VERSION); | |
##### User Credential ##### | |
my $username = 'username'; | |
my $password = 'password'; | |
########################### | |
my $VERSION = '0.7.2'; | |
my $current_setting = ''; | |
##### Start painting the GUI ##### | |
# Main Window | |
my $mw = MainWindow->new(); | |
$mw->title( 'Hamlet WiFi Switcher ' . $VERSION ); | |
# Checks the current wireless configuration | |
$current_setting = check_status(); | |
#Gender | |
my $frm_gender = $mw->Frame(); | |
my $lbl_gender = $frm_gender->Label( | |
-text => "Desideri attivare il Wireless sulla tua rete?" ); | |
my $rdb_m = $frm_gender->Radiobutton( | |
-text => "Si", | |
-value => "attivato", | |
-variable => \$current_setting | |
); | |
my $rdb_f = $frm_gender->Radiobutton( | |
-text => "No", | |
-value => "disattivato", | |
-variable => \$current_setting | |
); | |
my $but = $mw->Button( -text => "fatto", -command => \&push_button ); | |
#Text Area | |
my $textarea = $mw->Frame(); | |
my $txt = $textarea->Text( -width => 55, -height => 4 ); | |
my $srl_y = | |
$textarea->Scrollbar( -orient => 'v', -command => [ yview => $txt ] ); | |
my $srl_x = | |
$textarea->Scrollbar( -orient => 'h', -command => [ xview => $txt ] ); | |
$txt->configure( | |
-yscrollcommand => [ 'set', $srl_y ], | |
-xscrollcommand => [ 'set', $srl_x ] | |
); | |
$txt->insert( 'end', '[?] Wireless attualmente: ' . $current_setting . "\n" ); | |
#Geometry Management | |
$lbl_gender->grid( -row => 1, -column => 1 ); | |
$rdb_m->grid( -row => 1, -column => 2 ); | |
$rdb_f->grid( -row => 1, -column => 3 ); | |
$frm_gender->grid( -row => 3, -column => 1, -columnspan => 2 ); | |
$but->grid( -row => 4, -column => 1, -columnspan => 2 ); | |
$txt->grid( -row => 1, -column => 1 ); | |
$srl_y->grid( -row => 1, -column => 2, -sticky => "ns" ); | |
$srl_x->grid( -row => 2, -column => 1, -sticky => "ew" ); | |
$textarea->grid( -row => 5, -column => 1, -columnspan => 2 ); | |
MainLoop; | |
##### Various sub functions ##### | |
#This function will be executed when the button is pushed | |
sub push_button { | |
$txt->delete( '1.0', 'end' ); | |
$txt->insert( 'end', | |
"[?] Il Wireless verra' $current_setting tra pochi secondi...\n" ); | |
switch_device($current_setting); | |
} | |
sub switch_device { | |
my $switching_choice = shift; | |
my $mech = establish_connection(0); | |
my $current_status; | |
if ( $switching_choice eq 'attivato' ) { | |
$switching_choice = 1; | |
} | |
elsif ( $switching_choice eq 'disattivato' ) { | |
$switching_choice = 0; | |
} | |
else { | |
$txt->insert( 'end', | |
'[!] Hai inserito una scelta sbagliata...' . "\n" ); | |
} | |
$mech->submit_form( with_fields => { wlan_APenable => $switching_choice }, | |
); | |
if ( $mech->success() ) { | |
$mw->messageBox( | |
-title => 'Hamlet WiFi Switcher', | |
-icon => 'info', | |
-type => 'ok', | |
-message => 'Cambiamenti fatti con successo!' | |
); | |
} | |
else { | |
$mw->messageBox( | |
-title => 'Hamlet WiFi Switcher', | |
-icon => 'warning', | |
-type => 'ok', | |
-message => 'Ooops qualcosa e\' andato storto...' | |
); | |
} | |
$current_status = check_status(); | |
$txt->insert( 'end', | |
"[?] Il Wireless e\' stato $current_status con successo.\n" ); | |
} | |
sub check_status { | |
my $mech_content = establish_connection(1); | |
my $content; | |
my $current_status; | |
$content = $mech_content->content; | |
if ( $content =~ m{VALUE="1" CHECKED>}i ) { | |
$current_status = 'attivato'; | |
} | |
elsif ( $content =~ m{VALUE="0" CHECKED>}i ) { | |
$current_status = 'disattivato'; | |
} | |
else { | |
$mw->messageBox( | |
-title => 'Hamlet WiFi Switcher', | |
-icon => 'warning', | |
-type => 'ok', | |
-message => 'Errore nel parsing della pagina HTML!' | |
); | |
$txt->insert( 'end', '[!] Errore nel parsing della pagina HTML...', | |
"\n" ); | |
} | |
return $current_status; | |
} | |
sub establish_connection { | |
my $flag = shift; | |
my $mech; | |
my $mech_content; | |
$mech = WWW::Mechanize->new( autocheck => 0 ); | |
$mech->credentials( $username => $password ); | |
$mech_content = | |
$mech->get( 'http://' | |
. $username . ':' | |
. $password | |
. '@192.168.1.254/basic/home_wlan.htm' ); | |
unless ( $mech_content->is_success ) { | |
$mw->messageBox( | |
-title => 'Hamlet WiFi Switcher', | |
-icon => 'warning', | |
-type => 'ok', | |
-message => 'Non riesco a stabilire una connessione: ' | |
. $mech_content->status_line() | |
); | |
$txt->insert( 'end', | |
'[!] Non riesco a stabilire una connessione: ' | |
. $mech_content->status_line() | |
. "\n" ); | |
} | |
$flag == 1 | |
? return $mech_content | |
: return $mech; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment