Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Cool Archlinux network script

by heatblazer (Scribe)
on May 26, 2012 at 05:00 UTC ( [id://972569]=CUFP: print w/replies, xml ) Need Help??

Hello monks, I haven`t posted for a while.

I`ve finalized my network script, still it`s in beta tho, because I am learning Perl more and more. Here is it

#!/usr/bin/perl -w ###################################################################### +## #A nifty script for manipulatina and automating some networking in Arc +h 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 pse +udo code below. The script is # #imperative and for now no package or modularization is needed since i +t 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 th +e script # ###################################################################### +## use strict; ############### S U B R O U T I N E S + ############### #PROMPT sub prompt { print shift, "\n"; chomp(my $inp = <STDIN>); 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 somethi +ng 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 writin +g: $!\n"; while ( <DATA> ) { 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 "dhc +lient is dead or not present\n"; system("ifconfig | grep wlan0") ? print "wlan0 is up and ready\n" : sy +stem("ifconfig wlan0 up"); print "PRESS 'RETURN' TO START NETWORKING OR CTRL+D TO QUIT \n"; #Test process data while ( chomp(my $inpt = <STDIN>) != 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 $re +try++; 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 D +IR 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 w +lan0 down"); system("netcfg -d $netcfg_var"); #make a global variab +le 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 availa +ble...\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 my +net"; 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

Hope some Arch users will find it useful. Make sure to replace the __DATA__ and some if-elses with your own.

Replies are listed 'Best First'.
Re: Cool Archlinux network script
by jwkrahn (Abbot) on May 26, 2012 at 07:36 UTC
    return $inp or "";

    Because of the low precedence of the or operator that will not do what you seem to think it will:

    $ perl -le' sub prompt { my $inp = shift; return $inp or "zzz"; } print prompt( $_ ) for undef, 0, 1, 2; ' 0 1 2

    You need to use the higher precedence || operator:

    $ perl -le' sub prompt { my $inp = shift; return $inp || "zzz"; } print prompt( $_ ) for undef, 0, 1, 2; ' zzz zzz 1 2



    while ( chomp(my $inpt = <STDIN>) != 0 ) {

    That is usually written as:

    while ( my $inpt = <STDIN> ) { chomp $inpt;



    system("ifconfig wlan0 up") if ( (my $ifconf = `ifconfig`) + !~ /wlan0/ );

    You never use the $ifconf variable anywhere else in your program so why are you creating it?    Just do:

    system 'ifconfig wlan0 up' if `ifconfig` !~ /wlan0/;

      Hmm.. I`ve forgot about it... I was trying some experiments with it with SIGNAL global hash...

      Thanks for replying. I think that these low and high precedences are confusing... from now on I`ll just stick to ?: operator...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://972569]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-03-19 06:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found