LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Password
Slackware This Forum is for the discussion of Slackware Linux.

Notices

Reply
 
Search this Thread
Old 06-29-2008, 04:33 PM   #1
dgallig
LQ Newbie
 
Registered: Jun 2008
Location: florence
Distribution: slackware
Posts: 5

Rep: Reputation: 0
script to connect to wifi with various configurations


Hello all ,

First post here ...

As I move for work from one place to another , I was tired to have to launch various scripts manually .
So I wrote a script to connect to various wireless networks at boot time ...
The script has been written for Slackware , but can be used with any distro .

Edited :
Maybe this lack of some explanation ...

The script is intented for use when you need to connect to differente networks ...

example :

net 1 ) wap , essid mywap , password mywappass , use dhcp
net 2 ) wep , essid mywep , password myweppass , statip ip 192.168.0.x , gw 192.168.0.x , dns 212.212.212.x
net 3 ) wap , essid hidden , passwd anotherpass , static ip 192.168.2.x , etc

When you boot the pc ( probably a laptop ) , the script launch a scan for available networks ,
and if it finds a match for an essid in the conf file , or a mac address in case of a hidden essid ,
it connects to that network .





In the next post ,I'll write the link to it , seems it's not permitted in the first one


David Galligani

Last edited by dgallig; 07-03-2008 at 06:06 AM. Reason: maybe lacks of some explanation
 
Old 06-29-2008, 04:35 PM   #2
dgallig
LQ Newbie
 
Registered: Jun 2008
Location: florence
Distribution: slackware
Posts: 5

Original Poster
Rep: Reputation: 0
Hello again


If someone wants to try it , or wants to help me debugging it ,
the file can be downloaded from my web http://alternativamente.eu/rc.ap-0.1.tar.gz

For anything , I can be reached at david at alternativamente dot eu

Thx

David Galligani
 
Old 06-29-2008, 05:06 PM   #3
dgallig
LQ Newbie
 
Registered: Jun 2008
Location: florence
Distribution: slackware
Posts: 5

Original Poster
Rep: Reputation: 0
Maybe it's better I post it here ...
This is the script :

Code:
#!/bin/bash
#
#rc.ap version 0.1
#
#Connect wireless interface to known networks 
#By David Galligani david at alternativamente dot eu
#Copyright (C) 2008 David Galligani

#This program is free software; you can redistribute it and/or
#modify it under the terms of the GNU General Public License
#as published by the Free Software Foundation; either version 2
#of the License, or (at your option) any later version.

#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.

#You should have received a copy of the GNU General Public License
#along with this program; if not, write to the Free Software
#Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.


#To do :
#Debug !


WLAN=eth1						#Interface to configure 

FOUND_CELLS=/tmp/cells;			#Files where we store 
FOUND_ESSIDS=/tmp/essids;			#the scan output
KNOWN_CELLS=/etc/rc.d/rc.ap.conf;	#Conf file for Slackware
#KNOWN_CELLS=/etc/ap.conf;		#Conf file for System V distros ( Debian , Suse , etc )
WPA_CONF=/tmp/wpa_supplicant.conf	#wpa_supplicant conf ( if wpa is used )
SLEEP="1"						#Should we sleep between one try and another ? ( useful for debug ) 
SLEEP_TIME="1"					#Sleep time in seconds
WHICH="/usr/bin/which" 			#Chances are that this is the only path you have to change



##########################################################
#Path of the binaries we'r going to use
RM=`$WHICH rm`
IWCONFIG=`$WHICH iwconfig`
IWLIST=`$WHICH iwlist`
WPA_SUPPLICANT=`$WHICH wpa_supplicant`
IFCONFIG=`$WHICH ifconfig`
ROUTE=`$WHICH route`
DHCLIENT=`$WHICH dhcpcd`
GREP=`$WHICH grep`
CAT=`$WHICH cat`
CUT=`$WHICH cut`
ECHO=`$WHICH echo`
SLP=`$WHICH sleep`

# Just for the appereance
RES_COL=30
SETCOLOR_SUCCESS="$ECHO -en \\033[1;32m"
SETCOLOR_FAILURE="$ECHO -en \\033[1;31m"
SETCOLOR_WARNING="$ECHO -en \\033[1;33m"
SETCOLOR_NORMAL="$ECHO -en \\033[0;39m"
CLEAR="$ECHO -en ";
###########################################################

write_out() {
	MOVE_TO_COL="$ECHO -en \\033[${RES_COL}G"
	$MOVE_TO_COL
	$CLEAR
	$MOVE_TO_COL
	$SETCOLOR_FAILURE
	$ECHO -n "  $Essid                            "
	$SETCOLOR_NORMAL
	return 1
}



get_ap_conf() {							#Grepping all the parameters from the conf file 
	AP=`$ECHO $K_Cell    | $CUT -d";" -f1`;	#If someone knows a better way to do this 
	ESSID=`$ECHO $K_Cell | $CUT -d";" -f2`;	#please let me know .
	PWD=`$ECHO $K_Cell   | $CUT -d";" -f3`;
	DHCP=`$ECHO $K_Cell  | $CUT -d";" -f4`;
	IP=`$ECHO $K_Cell    | $CUT -d";" -f5`;
	MASK=`$ECHO $K_Cell  | $CUT -d";" -f6`;
	GW=`$ECHO $K_Cell    | $CUT -d";" -f7`;
	DNS1=`$ECHO $K_Cell  | $CUT -d";" -f8`;
	DNS2=`$ECHO $K_Cell  | $CUT -d";" -f9`;
	PROTO=`$ECHO $K_Cell  | $CUT -d";" -f10`;
	KEY_MGMT=`$ECHO $K_Cell  | $CUT -d";" -f11`;
	PAIRWISE=`$ECHO $K_Cell  | $CUT -d";" -f12`;
	GROUP=`$ECHO $K_Cell  | $CUT -d";" -f13`;
}

connect(){


if [ "$PROTO" = "WEP" ];then 						#If WEP
	$IWCONFIG $WLAN essid "$ESSID"  key s:"$PWD" 
	if [ $? = 0 ];then 
		$MOVE_TO_COL ;
		$CLEAR ; $SETCOLOR_SUCCESS ;
		$CLEAR ;
		$ECHO -en " Connected to $ESSID ! \n " ;
		
	fi
else 											#If WPA
	
	WPA_PID=`pidof wpa_supplicant`				# Check if another session is running ....
	if [ $? = 0 ];then
		kill $WPA_PID ;	
	fi
	if [ -e  /var/run/wpa_supplicant/$WLAN ];then
		 $RM /var/run/wpa_supplicant/* 
	fi
	
	#Creating a temporary wpa.conf we use to connect
	$ECHO "ctrl_interface=/var/run/wpa_supplicant" >$WPA_CONF
	$ECHO "network={">>$WPA_CONF
	$ECHO "ssid=\"$ESSID\"">>$WPA_CONF
	$ECHO "key_mgmt=$KEY_MGMT">>$WPA_CONF
	$ECHO "proto=$PROTO">>$WPA_CONF
	$ECHO "pairwise=$PAIRWISE">>$WPA_CONF
	$ECHO "group=$GROUP">>$WPA_CONF
	$ECHO "psk=\"$PWD\"">>$WPA_CONF
	$ECHO "}">>$WPA_CONF
	$IWCONFIG $WLAN essid "$ESSID" # Why I have to do this ???
	 $WPA_SUPPLICANT -i $WLAN -c $WPA_CONF >/dev/null 2>&1 & 
	if [ $? == "0" ];then 
		$MOVE_TO_COL ;
		$CLEAR ;
		$SETCOLOR_SUCCESS ;
		$CLEAR ;
		$ECHO -en " Connected to $ESSID ! \n " ;
	fi
fi

 $SETCOLOR_NORMAL 
	if [ $DHCP = no ];then
		$IFCONFIG $WLAN $IP netmask $MASK
		$ROUTE add default gw $GW 2>/dev/null
		$ECHO -en "\n"
		$ECHO -en "$WLAN configured with ip : $IP \n"
		$ECHO -en "GW set as $GW\n";
		$ECHO -en "\n"
		if [ $DNS1 ];then 
			$ECHO "nameserver $DNS1"> /etc/resolv.conf ;
			if [ $DNS2 ];then 
				$ECHO "nameserver $DNS2" >> /etc/resolv.conf ;
			fi
		fi
	else
		$DHCLIENT $WLAN
	fi	
}

main(){
$IFCONFIG $WLAN up 											#Bringing up the device
$IWLIST $WLAN scan | $GREP Address > $FOUND_CELLS ;				#Scanning for 
$IWLIST $WLAN scan | $GREP ESSID |  $CUT -d":" -f2   > $FOUND_ESSIDS #availables AP


for K_Cell in `$CAT $KNOWN_CELLS`; do 					#Trying to match essids 
	get_ap_conf;
	$CAT $FOUND_ESSIDS  2> /dev/null | while read Essid ; do
		write_out ; 
		if [ $SLEEP == "1" ];then 
			$SLP $SLEEP_TIME
		fi
		if [ \"$ESSID\" == "$Essid" ];then 
			connect;
		fi
	done
	
	for Cell in `$CAT $FOUND_CELLS | $CUT -d" " -f15` ; do    #Checking match on MAC ADDRESS 
		write_out ;									# for networks with hidden essid 
		if [ "$AP" != '' ];then
			if [ $AP = $Cell ];then 
				connect;
				exit ;
			fi
		fi
	done
	
done	  

}

if [ -e $KNOWN_CELLS ];then 
	$GREP "#" $KNOWN_CELLS > /dev/null 
	if [ $? = 0 ];then 
		$ECHO -en "Please delete commented lines\n ( lines beginning with a #)  from config file \n before using this script \n " 
		exit
	fi
	$ECHO -n "Connecting to  WiFi AP :  "; main ; 
else
	$ECHO " No config file  found ... Aborting "
fi
and this is an example conf file ( script won't work if comments are not removed )

Code:
#Parameters , in order :
#Ap Mac Addr#Ap Essid # Ap passwd # Use Dhcp ( yes|no ) #Static Ip addr # Subnet mask # Gateway # Primary DNS # Secundary DNS # Protocol # WPA Key Mgmnt #WPA Pairwise #WPA Group 
#
#WAP example  with static address and MAC Address 
00:C0:00:EE:11:07;My-ESSID;mypassword;no;192.168.12.11;255.255.255.0;192.168.12.254;212.216.112.112;195.235.113.3;WPA;WPA-PSK;TKIP;TKIP
#WEP Example with no MAC Address , with dhcp
;My-ESSID;mypassword;yes;;;;;;WEP

Last edited by dgallig; 06-29-2008 at 05:08 PM.
 
Old 07-03-2008, 06:10 AM   #4
dgallig
LQ Newbie
 
Registered: Jun 2008
Location: florence
Distribution: slackware
Posts: 5

Original Poster
Rep: Reputation: 0
I 've seen variuos downloads of the tar.gz ...
But haven't received any comments ...
Does it works ?
Lacks something ?
Crash ?

Please let me know
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
installed f4 not able connect to internet....tried al the configurations... DphoenixATlinux Linux - Newbie 1 09-29-2007 07:57 PM
Difficulty to connect with wifi jayhel Slackware 20 01-16-2007 09:55 PM
can't connect wifi at home, but can connect everywhere else Javi2 Suse/Novell 1 08-25-2005 06:49 PM
can't connect via ftp on my lan...this is iptapbles configurations.... loboautoma Linux - Networking 12 02-05-2005 09:43 AM
What app. should I use to connect via wifi? Unknown_User Linux - Wireless Networking 3 01-08-2005 05:14 PM


All times are GMT -5. The time now is 02:27 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration