#!/usr/bin/perl -w ######################################################################## #A nifty script for manipulatina and automating some networking in Arch linux, it uses the tools: # #iwlist, iwconfig, dchpcd/dhclient and netcfg with wpa_supplicant for it`s tasks. It pings yahoo.com to # #determine the network satus and then executes some tasks from the pseudo code below. The script is # #imperative and for now no package or modularization is needed since it is small task. # #Beta: # #Date: 14/05/2012 last edited on 24/05/2012 # #Ilian Zapryanov # #heatblazer@gmail.com # ######################################################################## #While waiting for user quit or continue: # # If not quit, check do we have net: # # while we don`t check various added networks: # # if we can`t connect to wifi, connect the mtel usb 3G # # kill all wifi dhclients and start using mtel # # If pressed C^+D kill all all network and self kill the script # ######################################################################## use strict; ############### S U B R O U T I N E S ############### #PROMPT sub prompt { print shift, "\n"; chomp(my $inp = ); return $inp or ""; } #Process data file sub process_data { my $net_name = shift; #open that file and concat the data to it, set the name to something verbose and #understandable, for now it will be sv_vlas.txt since this netwrok works there open(FH, "> $net_name") or die "Can`t open the variable for writing: $!\n"; while ( ) { print FH $_; #record data from beta_net file to temporary file } print "Recorded data to : /home/$net_name"; } ######################################################################## my $netcfg_var = "sv_vlas"; print ("Killing all available DHC/DHCPCDs...\n"); system("killall dhcpcd") ? print "Killed dhcpcd.\n" : print "dhcpcd is dead or not present\n"; system("killall dhclient") ? print "Killed dhclient. \n" : print "dhclient is dead or not present\n"; system("ifconfig | grep wlan0") ? print "wlan0 is up and ready\n" : system("ifconfig wlan0 up"); print "PRESS 'RETURN' TO START NETWORKING OR CTRL+D TO QUIT \n"; #Test process data while ( chomp(my $inpt = ) != 0 ) { my $retry = 0; my $nstat = system("ping -c 2 yahoo.com > /dev/null 2>&1"); print "Current network status is $nstat. \n"; while ( $nstat != 0 ) { # do we have net? #if not try to scan and connect to our nets system("ifconfig wlan0 up") if ( (my $ifconf = `ifconfig`) !~ /wlan0/ ); my $work_net = system('iwlist scan | grep "GKT" ') and $retry++; my $home_net = system("iwlist scan | grep 'ilian' ") and $retry++; my $vlas_home = system("iwlist scan | grep 'VIVACOM_NET' ") and $retry++; my $mtel = system("lsusb | grep \"Huawei\"") and $retry++; if ( $home_net == 0 ) { print "Connected on $retry attempt.\n"; system("killall dhcpcd | killall dhclient ") or print "Reset network .. \n"; system("iwconfig wlan0 essid \"ilian\" key 1234512345" ); system("dhcpcd wlan0"); $nstat = system("ping -c 2 yahoo.com"); last if $nstat == 0 ; } elsif ( $work_net == 0 ) { print "Connected on $retry attempt.\n"; system("killall dhcpcd | killall dhclient ") or print "Reset network .. \n"; system("iwconfig wlan0 essid \"GKT\" key s:joana" ) ; system("dhcpcd wlan0"); $nstat = system("ping -c 2 yahoo.com"); last if $nstat == 0 ; } #this requires netcfg daemon with a custom script added to it from /etc/network.d/ (see __DATA__ below ) elsif ( $vlas_home == 0 ) { print "Connected on $retry attempt.\n"; system("killall dhcpcd | killall dhclient " ) or print "Reset network .. \n"; process_data($netcfg_var); #THIS GOES IN USER`s HOME DIR system("netcfg sv_vlas") or die("Can`t connect to file in home dir\n"); system("dhcpcd wlan0"); $nstat = system("ping -c 2 yahoo.com"); last if $nstat == 0; } elsif ( $mtel == 0 ) { print "Connected on $retry attempt.\n"; print "Stopping dhcpcd, dhclient..\n"; #downs wifi too here... system("killall dhcpcd | killall dhclient | ifconfig wlan0 down"); system("netcfg -d $netcfg_var"); #make a global variable here... please print ("Trying mtel gprs...\n"); system("wvdial") or print("Can`t start mtel gprs...\n"); $nstat = system("ping -c 2 yahoo.com"); last if $nstat == 0; } elsif ( $retry % 8 == 0 ) { print "$retry attempted... probably no networks available...\n"; print "Continue?\n"; print "Press 'ENTER' to continue or CTRL+D to quit the script: "; last; } } #end net set loop } #end check net loop #kill networks and clean all print "FINISHED NETWORKINGS....\n"; system("killall dhclient") ? print"Killed." : print "No dhclient"; system("kiallall dhcpcd") ? print "Killed. " : print "No dhcpcd"; system("netcfg -d $netcfg_var") ? print "Stopped mynet" : print "No mynet"; print "Bye, bye :) Come again :)\n\n"; #suicide script too system("killall beta_net"); #data for netcfg Sv.vlas network __DATA__ CONNECTION='wireless' DESCRIPTION='A simple WPA encrypted wireless connection' INTERFACE='wlan0' SECURITY='wpa' ESSID='VIVACOM_NET' ## Uncomment if the supplied ESSID is hexadecimal #ESSID_TYPE='hex' KEY='t1o2d3o4r5' IP='dhcp' # Uncomment this if your ssid is hidden #HIDDEN=yes