EscapeCharacter
05-21-2003, 06:28 AM
i wrote this Perl/Tk script to make connecting very easy. Made it mostly cause my parents were always complaining that if the net went down and i wasnt around they had to wait for me to get home to get back on the net. so here it is enjoy :).
#!/usr/bin/perl -w
# PPPoE Connect
# Simple app to help my parents get connected while im not here
use strict;
use Tk;
use Net::Ping;
my($statustext);
# Declare widgets
my($window, $canvas, $stbutton, $spbutton, $statbar, $sframe, $mframe, $progress);
# Declare lockfile
my $lockfile = "/home/escape/.pppnet.lock";
# check out page 378 in Mastering Perl/Tk for instructions on piping
$statustext = "STATUS: UNKNOWN";
sub checkConnected(){
my $ping = Net::Ping->new("icmp", 2);
# I ping yahoo because its always there :)
my $test = $ping->ping("yahoo.com", 5);
if($test){
# If ping is successful
$statustext = "STATUS: CONNECTED";
$stbutton->configure(-state => "disabled");
$spbutton->configure(-state => "normal");
}
elsif(!$test){
# If ping fails and returns undef
$statustext = "STATUS: DISCONNECTED";
$stbutton->configure(-state => "normal");
$spbutton->configure(-state => "disabled");
}
}
sub netstop(){
# Clean up all process that may not be cleaned by adsl-stop
system("/usr/sbin/adsl-stop");
system("/usr/bin/killall -9 pppd 2>/dev/null");
system("/usr/bin/killall -9 pppoe 2>/dev/null");
system("/usr/bin/killall -9 adsl-connect 2>/dev/null");
system("/usr/bin/killall -9 adsl-connect 2>/dev/null");
system("/sbin/ifconfig eth0 down 2>/dev/null");
$statustext = "STATUS: DISCONNECTED";
$stbutton->configure(-state => "disabled");
$window->after(1000, sub{
$stbutton->configure(-state => "normal");
$spbutton->configure(-state => "disabled");
}
);
}
sub netstart(){
system("/usr/sbin/adsl-start 2>/dev/null");
$statustext = "STATUS: Connecting...";
$window->update;
# Check if the connection was establish successfully
$window->after(3000, \&checkConnected());
}
# Be sure only one instance of pppoe connect is running
if(!(open(H, "$lockfile"))){
print "PPPoE Connect not running\n";
close(H);
}
else{
print "PPPoE Connect already running, or lockfile($lockfile) exists\n";
close(H);
exit(0);
}
$window = MainWindow->new(-title => 'PPPoE Connect');
# Check the status of the connect every 6 minutes and update accordingly
$window->repeat(60000, \&checkConnected);
$window->geometry("300x50+300+300");
$sframe = $window->Frame(-relief => 'sunken', -bd => 2)->pack(-side => 'bottom', -fill => 'x');
$mframe = $window->Frame()->pack(-side => 'top', -expand => 0);
$stbutton = $mframe->Button(-text => 'Start', -command => \&netstart);
$spbutton = $mframe->Button(-text => 'Stop', -command => \&netstop);
$progress = $sframe->Label(-text => 'Stuffs', -textvariable => \$statustext);
$progress->pack(-side => 'left', -anchor => 'w');
$stbutton->pack(-side => 'right', -anchor => 'e');
$spbutton->pack(-side => 'left');
&checkConnected();
print "Creating Lockfile\n";
system("touch $lockfile");
&MainLoop();
print "Removing Lockfile\n";
system("rm $lockfile");
exit(0);
also if you got some tips for me or i did something wrong help a guy out.
#!/usr/bin/perl -w
# PPPoE Connect
# Simple app to help my parents get connected while im not here
use strict;
use Tk;
use Net::Ping;
my($statustext);
# Declare widgets
my($window, $canvas, $stbutton, $spbutton, $statbar, $sframe, $mframe, $progress);
# Declare lockfile
my $lockfile = "/home/escape/.pppnet.lock";
# check out page 378 in Mastering Perl/Tk for instructions on piping
$statustext = "STATUS: UNKNOWN";
sub checkConnected(){
my $ping = Net::Ping->new("icmp", 2);
# I ping yahoo because its always there :)
my $test = $ping->ping("yahoo.com", 5);
if($test){
# If ping is successful
$statustext = "STATUS: CONNECTED";
$stbutton->configure(-state => "disabled");
$spbutton->configure(-state => "normal");
}
elsif(!$test){
# If ping fails and returns undef
$statustext = "STATUS: DISCONNECTED";
$stbutton->configure(-state => "normal");
$spbutton->configure(-state => "disabled");
}
}
sub netstop(){
# Clean up all process that may not be cleaned by adsl-stop
system("/usr/sbin/adsl-stop");
system("/usr/bin/killall -9 pppd 2>/dev/null");
system("/usr/bin/killall -9 pppoe 2>/dev/null");
system("/usr/bin/killall -9 adsl-connect 2>/dev/null");
system("/usr/bin/killall -9 adsl-connect 2>/dev/null");
system("/sbin/ifconfig eth0 down 2>/dev/null");
$statustext = "STATUS: DISCONNECTED";
$stbutton->configure(-state => "disabled");
$window->after(1000, sub{
$stbutton->configure(-state => "normal");
$spbutton->configure(-state => "disabled");
}
);
}
sub netstart(){
system("/usr/sbin/adsl-start 2>/dev/null");
$statustext = "STATUS: Connecting...";
$window->update;
# Check if the connection was establish successfully
$window->after(3000, \&checkConnected());
}
# Be sure only one instance of pppoe connect is running
if(!(open(H, "$lockfile"))){
print "PPPoE Connect not running\n";
close(H);
}
else{
print "PPPoE Connect already running, or lockfile($lockfile) exists\n";
close(H);
exit(0);
}
$window = MainWindow->new(-title => 'PPPoE Connect');
# Check the status of the connect every 6 minutes and update accordingly
$window->repeat(60000, \&checkConnected);
$window->geometry("300x50+300+300");
$sframe = $window->Frame(-relief => 'sunken', -bd => 2)->pack(-side => 'bottom', -fill => 'x');
$mframe = $window->Frame()->pack(-side => 'top', -expand => 0);
$stbutton = $mframe->Button(-text => 'Start', -command => \&netstart);
$spbutton = $mframe->Button(-text => 'Stop', -command => \&netstop);
$progress = $sframe->Label(-text => 'Stuffs', -textvariable => \$statustext);
$progress->pack(-side => 'left', -anchor => 'w');
$stbutton->pack(-side => 'right', -anchor => 'e');
$spbutton->pack(-side => 'left');
&checkConnected();
print "Creating Lockfile\n";
system("touch $lockfile");
&MainLoop();
print "Removing Lockfile\n";
system("rm $lockfile");
exit(0);
also if you got some tips for me or i did something wrong help a guy out.