#!/usr/bin/perl # # smoothwall # # author : Douglas E. Warner # # used to control whether my smoothwall box # is connected or disconnected # use strict; # program variables my $progname = "smoothwall"; my $version = "0.1.0"; # curl variables my $curl = "/usr/bin/curl -s"; # smoothwall variables my $smoothwall = "http://smoothwall"; my $indexpage = "/cgi-bin/index.cgi"; my $dialpage = "/cgi-bin/dial.cgi"; my $dialuser = "dial"; my $dialpass = ""; # other variables my $smoothwall_page; my $smoothwall_status = "unknown"; my $maxtries = 3; sub Usage { print("== $progname $version ==\n"); print("usage: $progname (connect|disconnect|status)\n"); print(" connect makes smoothwall connect to the internet using\n"); print(" the currently selected dial profile\n"); print(" disconnect makes smoothwall disconnect from the internet\n"); print(" status returns 'connected', 'dialing', or 'disconnected' (without quotes)\n"); print("\n"); print("return values:\n"); print(" 255 : error\n"); print(" 128 : dialing\n"); print(" 0 : disconnected\n"); print(" >0 : connected; if 'connect' command given, # of attempts taken\n"); print("\n"); exit(255); } # end Usage sub GetStatus { $smoothwall_page = `$curl $smoothwall$indexpage`; if ($smoothwall_page =~ /Modem idle/) { $smoothwall_status = "disconnected"; } elsif ($smoothwall_page =~ /Connected/) { $smoothwall_status = "connected"; } elsif ($smoothwall_page =~ /Dialing/) { $smoothwall_status = "dialing"; } # end check to see what smoothwall is currently doing } # end GetStatus if (scalar(@ARGV) != 1 || ( $ARGV[0] ne "connect" && $ARGV[0] ne "disconnect" && $ARGV[0] ne "status")) { print("not the right param\n"); Usage(); } # end if no options, output usage # get the current smoothwall page to see what's going on GetStatus(); # if the user wanted the status, return and quit if ($ARGV[0] eq "status") { print("$smoothwall_status\n"); if ($smoothwall_status eq "disconnected") { exit(0); } elsif ($smoothwall_status eq "dialing") { exit(128); } elsif ($smoothwall_status eq "connected") { exit(1); } else { exit(255); } # end exit with proper status # if the user wanted to connect, do it until it works } elsif ($ARGV[0] eq "connect") { my $attempt = 0; # keep trying to connect until it works, or we try too much while($smoothwall_status ne "connected" && $attempt <= $maxtries) { $attempt++; print("attempt $attempt of $maxtries"); system("$curl -o /dev/null -u $dialuser:$dialpass -d 'ACTION=Connect' --url $smoothwall$dialpage"); GetStatus(); while ($smoothwall_status eq "dialing") { print("."); sleep 1; GetStatus(); } # end wait until this is done dialing print("\n"); } # end while print("$smoothwall_status\n"); if ($smoothwall_status eq "connected") { exit($attempt); } elsif ($smoothwall_status eq "disconnected") { exit(0); } else { exit(255); } # end exit with proper status # if the user wanted to disconnect, lets do that } elsif ($ARGV[0] eq "disconnect") { print("disconnecting"); system("$curl -o /dev/null -u $dialuser:$dialpass -d 'ACTION=Disconnect' --url $smoothwall$dialpage"); GetStatus(); while ($smoothwall_status eq "dialing") { print("."); sleep 1; GetStatus(); } # end wait until this is done hanging up print("\n"); print("$smoothwall_status\n"); exit(0); } # end do what the user wants