How to configure wifi from the commandline

How to do things, solutions, recipes, tutorials
Message
Author
tempestuous
Posts: 5464
Joined: Fri 10 Jun 2005, 05:12
Location: Australia

#151 Post by tempestuous »

Spaccafumo wrote:How to replace this wep-128 (ascii) command:
iwconfig eth0 key s:asciipassword
with a wpa2 analogous command?
See "PART 2: WPA encryption" on the first page of this thread
http://www.murga-linux.com/puppy/viewto ... 336#159336

renketsu0
Posts: 3
Joined: Thu 14 Jun 2012, 17:50

#152 Post by renketsu0 »

I had a startup script working perfectly until a few weeks ago when all of a sudden it gets stuck in some loop during the 4 way handshake. I didn't change any configuration files on router or within puppy so I'm not sure what caused this change.

In addition to being stuck in an infinite 4-way handshake loop, after I halt the command my router goes nuts and refuses to connect any other client until I reset it.

I should note that I can connect via net-setup.sh for some reason, even though it appears to issue the same command and points to the same config file I am pointing to.

My script is

Code: Select all

#!/bin/sh

ifconfig wlan0 down
modprobe rtl8187
sleep 3s
ifconfig wlan0 up

rm /var/run/wpa_supplicant/*

wpa_supplicant -B -i wlan0 -D wext -c /etc/network-wizard/wireless/wpa_profiles/wpa_supplicant2.conf 
rm -f /var/lib/dhcpcd/*.info
rm -f /var/run/*.pid

# start dhcp
dhcpcd -t 30 -h myhost -d wlan0

# start the firewall
/etc/rc.d/rc.firewall
I remove -B and add -dd to see a bunch of stuff I don't understand, but I do notice

Code: Select all

...
Cancelling authentication timeout
State: GROUP_HANDSHAKE -> COMPLETED
CTRL-EVENT-CONNECTED - Connection to MAC:ADD completed (auth) [id=0 id_str=]
wpa_driver_wext_set_operstate: operstate 0->1 (UP)
netlink: Operstate: linkmode=-1, operstate=6
EAPOL: External notification - portValid=1
EAPOL: External notification - EAP success=1
EAPOL: SUPP_PAE entering state AUTHENTICATING
EAPOL: SUPP_BE entering state SUCCESS
EAP: EAP entering state DISABLED
EAPOL: SUPP_PAE entering state AUTHENTICATED
EAPOL: Supplicant port status: Authorized
EAPOL: SUPP_BE entering state IDLE
EAPOL authentication completed successfully
RTM_NEWLINK: operstate=1 ifi_flags=0x11043 ([UP][RUNNING][LOWER_UP])
RTM_NEWLINK, IFLA_IFNAME: Interface 'wlan0' added
RTM_NEWLINK: operstate=1 ifi_flags=0x11043 ([UP][RUNNING][LOWER_UP])
RTM_NEWLINK, IFLA_IFNAME: Interface 'wlan0' added
Wireless event: cmd=0x8b15 len=20
Wireless event: new AP: MAC:ADD
State: COMPLETED -> ASSOCIATED
wpa_driver_wext_set_operstate: operstate 1->0 (DORMANT)
netlink: Operstate: linkmode=-1, operstate=5
Associated with MAC:ADD
WPA: Association event - clear replay counter
WPA: Clear old PTK
EAPOL: External notification - portEnabled=0
EAPOL: SUPP_PAE entering state DISCONNECTED
EAPOL: Supplicant port status: Unauthorized
EAPOL: SUPP_BE entering state INITIALIZE
EAPOL: Supplicant port status: Unauthorized
EAPOL: External notification - portValid=0
EAPOL: Supplicant port status: Unauthorized
EAPOL: External notification - EAP success=0
EAPOL: Supplicant port status: Unauthorized
EAPOL: External notification - portEnabled=1
EAPOL: SUPP_PAE entering state CONNECTING
...
with MAC:ADD substituting for the real MAC addresses. It seems it actually is connecting, but then disconnecting and retrying for some reason.

Any ideas on how I can fix this? I'd even be satisfied if I could just script whatever net-setup.sh is doing when I click the "use this profile" button to execute at boot since that apparently works.[/code]

tempestuous
Posts: 5464
Joined: Fri 10 Jun 2005, 05:12
Location: Australia

#153 Post by tempestuous »

At the outset I reiterate that manual wifi configuration is only necessary if the Puppy Network Wizard does not work, or you have some special requirement.

First up, I see a problem with your script -

Code: Select all

ifconfig wlan0 down
modprobe rtl8187
Puppy will almost certainly have automatically loaded the rtl8187 driver, so it appears that you're forcibly loading this same driver a second time! This will most likely corrupt the driver (and the wifi device).
If you feel that the rtl8187 driver needs to be reloaded, it's important that you unload the driver, first. It's not good enough just to take down the interface with "ifconfig wlan0 down".

renketsu0 wrote:I'd even be satisfied if I could just script whatever net-setup.sh is doing when I click the "use this profile" button to execute at boot since that apparently works.
Once you have run the Network Wizard, and click the "save" button, you have enabled the net-setup.sh script to run at each boot up. And since it works, great ... but you appear to be still running your own separate script - so the two will be fighting each other! Which one wins depends on where in the boot sequence you have located your script!

I suspect the best solution would be to forget about your script, and just use the Network Wizard.
Don't use the Simple Network Wizard - this has a relatively high failure rate, especially with WPA encryption.

renketsu0
Posts: 3
Joined: Thu 14 Jun 2012, 17:50

#154 Post by renketsu0 »

Actually network wizard doesn't connect me on boot, not even right after install and only running the wizard once with net-setup.sh. That's the only reason I wanted to script it.

I'm glad you mentioned driver corruption though, as it caused me to take a close look at the drivers. It turns out puppy was using the r8180 for my card and not rtl8187 (even though my card is an 8187). My fault for not looking to see what it detected first. After removing that line everything works fine. Thanks!

renketsu0
Posts: 3
Joined: Thu 14 Jun 2012, 17:50

#155 Post by renketsu0 »

I have a new problem. Getting wireless running is working fine but it seems my wireless router for whatever reason disconnects all clients whenever a new client connects. Or rather when I look at the router's log it shows that the previously connected clients are stuck in some reauthentication loop and connection on the client-side is shown as dropped. This will go on until I manually disconnect and reconnect. While I know it's really time for a new router I can't help but think there's a way to code reauthentication into my script. But I have no idea how I would go about doing that. Any ideas?

tempestuous
Posts: 5464
Joined: Fri 10 Jun 2005, 05:12
Location: Australia

#156 Post by tempestuous »

I urge all newcomers only to resort to ndiswrapper as a last resort! The success rate of ndiswrapper is low, and you will generally get a more reliable wifi connection by using a true Linux driver.

Ibidem
Posts: 549
Joined: Wed 26 May 2010, 03:31
Location: State of Jefferson

Poking at sysfs

#157 Post by Ibidem »

I found out the basics of this reading SNS-retro, then poked around a little more.

All interfaces are symlinks under /sys/class/net/; these symlinks point to "../../class/<bus id>/net/<interface>".
So /sys/class/net/$INTERFACE/../../ is the directory for the device.

Within this directory, there are several files:
uevent contains the information passed at hotplug, more-or-less
For example:

Code: Select all

$ cat /sys/class/net/wlan0/../../uevent
DRIVER=rtl8192se
PCI_CLASS=28000
PCI_ID=10EC:8172
PCI_SUBSYS_ID=10EC:E020
PCI_SLOT_NAME=0000:03:00.0
MODALIAS=pci:v000010ECd00008172sv000010ECsd0000E020bc02sc80i00
DRIVER is the module name; MODALIAS is how it got loaded...
driver/module/parameters/ contains all module parameters.
If you have a wireless device, wireless/ contains several files:
beacon
crypt
fragment
level
link (I think this tells signal strenth)
misc
noise
nwid
retries
status

To find all network devices and identify the driver:

Code: Select all

#!/bin/sh
cd /sys/class/net
INTERFACES="`ls`"

WL=""
ETH=""
LO=""
for IFACE in $INTERFACES
do
	if [ -e $IFACE/wireless ]
	then
		echo "wireless ${IFACE} `grep DRIVER ${IFACE}/../../uevent`"
		WL="$WL $IFACE"
	elif [ -e $IFACE/../../uevent ]
	then
		echo "wired ${IFACE} `grep DRIVER ${IFACE}/../../uevent`"
		ETH="$ETH $IFACE"
	else
		echo "virtual $IFACE"
		LO="$LO $IFACE"
	fi
done

Ibidem
Posts: 549
Joined: Wed 26 May 2010, 03:31
Location: State of Jefferson

#158 Post by Ibidem »

I've mentioned the basic concept for this here, but I figured I'd cover it here...

wpa_cli -a <script> makes wpa_cli run as a daemon, and it automatically executes the script with interface name and CONNECTED or DISCONNECTED whenever wpa_supllicant connects or disconnects.
This can be used to run a dhcp client at the right time:

Code: Select all

#!/bin/sh
case $2 in
CONNECTED)
udhcpc -i $1 -h `hostname`
;;
DISCONNECTED)
;;
*)
;;
esac 
Some extensions:
Add a "manual terminate" feature
Run the DHCP client in the foreground, then cleanup the PID file on exit.
Kill the DHCP client on disconnect (may make flakey connections worse?)
rescan/reassociate on disconnect (likewise?)
Bring up VPN connection.

And a partial implementation:

Code: Select all

#!/bin/sh
export PIDFILE=/var/run/udhcpc/${1}.pid
dhcp_lease()(
[ -e $PIDFILE ] && ( kill `cat $PIDFILE` ; rm $PIDFILE ; )
udhcpc -i $1 -h `hostname` -p $PIDFILE 
rm $PIDFILE
)
case $2 in
CONNECTED)
dhcp_lease $1 &
;;
DISCONNECTED)
#kill `cat $PIDFILE`
wpa_cli -i $1  scan
;;
stop)
wpa_cli -i $1 disconnect
wpa_cli -i $1 terminate
;;
*)
echo "Use this script as a wpa_cli action script or $0 iface1 stop to leave iface1 ready for manual configuration"
;;
esac 

User avatar
JamesTheAwesomeDude
Posts: 99
Joined: Tue 29 Jan 2013, 17:17
Location: Classified

#159 Post by JamesTheAwesomeDude »

Okay, I can't figure out what to do:

Wi-Fi adapter, wlan0
Connecting to wireless router with WPA2 encryption
Using the carl9170usb driver
Static (custom) local IP
Custom DNS settings

What do I need to type? The stupid carl9170 driver is really crashy, and I hate having to make 40 clicks every 5 minutes to repair the internet, so I'm just gonna stick a script on the desktop labeled "Kick the Wi-Fi adapter" or something like that, that will auto-reconnect everything.
[url=http://www.murga-linux.com/puppy/viewtopic.php?t=84349][img]http://i.imgur.com/Zw6vryI.png[/img][color=#3B6EA3][b][size=200] Version 27. Dotpet. Click here.[/size]
(SFS available too!)[/b][/color][/url]

Ibidem
Posts: 549
Joined: Wed 26 May 2010, 03:31
Location: State of Jefferson

#160 Post by Ibidem »

JamesTheAwesomeDude wrote:Okay, I can't figure out what to do:

Wi-Fi adapter, wlan0
Connecting to wireless router with WPA2 encryption
Using the carl9170usb driver
Static (custom) local IP
Custom DNS settings

What do I need to type? The stupid carl9170 driver is really crashy, and I hate having to make 40 clicks every 5 minutes to repair the internet, so I'm just gonna stick a script on the desktop labeled "Kick the Wi-Fi adapter" or something like that, that will auto-reconnect everything.
Something like this (UNTESTED! CAUTION! YOU PROBABLY WILL NEED TO DEBUG AND CHANGE THIS!):

Code: Select all

#!/bin/sh
IF=wlan0
#You probably want to change these.
IP=10.0.0.100 
GW=0.0.0.0
#DNS: saved in /etc/resolv.conf, which should be static.

#modalias is a not-really-readable hw-specific ID that identifies one module.
read MOD </sys/class/net/${IF}/../../modalias
export MOD IF IP
start(){
wpa_supplicant -Dwext -B -c /etc/wpa.conf -i $IF
#I'm guessing the IP needs to be set after the interface is up.
#Really, this should be called via an action script, instead of dhcp
sleep 5
ifconfig $IF $IP
route add default gw $GW dev $IF
}
reload(){
wpa_cli -i $IF terminate
ifconfig $IF down
#Disable hardware encryption. Known to be desirable for AR9170 chips:
# http://linuxwireless.org/en/users/Drivers/carl9170
modprobe -r $MOD && modprobe $MOD nohwcrypt=1
}
reload && start 
This assumes that the WPA config is in /etc/wpa.conf (probably not true for you!)

edwin.evans
Posts: 60
Joined: Thu 15 Nov 2012, 16:06

Problem getting wifi module to load

#161 Post by edwin.evans »

tempestuous wrote:I urge all newcomers only to resort to ndiswrapper as a last resort! The success rate of ndiswrapper is low, and you will generally get a more reliable wifi connection by using a true Linux driver.
I'm trying to start my WiFi adapter and then config it, all using scripts. I have the iwconfig/ifconfig all working but it seems I need to run modprobe also. Currently the only way I know how to get my adapter to start blinking is to run rc.network but that does a lot of stuff that I don't want to do.

I'm using ndiswrapper when I load it in the Wizard and I think it is using a realtek driver. The module I'm using is http://www.amazon.com/gp/product/B003MTTJOY.

Any suggestion?

Thanks!

User avatar
Semme
Posts: 8399
Joined: Sun 07 Aug 2011, 20:07
Location: World_Hub

#162 Post by Semme »

Does iwconfig list an interface?

edwin.evans
Posts: 60
Joined: Thu 15 Nov 2012, 16:06

#163 Post by edwin.evans »

Semme wrote:Does iwconfig list an interface?
I think only if I run rc.network. I'm rebooting now to check for sure.

edwin.evans
Posts: 60
Joined: Thu 15 Nov 2012, 16:06

#164 Post by edwin.evans »

edwin.evans wrote:
Semme wrote:Does iwconfig list an interface?
I think only if I run rc.network. I'm rebooting now to check for sure.
If I run iwconfig, it doesn't list any wireless interface. Just lo and eth0. And the light on my adapter is not blinking. If I run rc.network at this point then the light starts blinking soon after the script starts.

edwin.evans
Posts: 60
Joined: Thu 15 Nov 2012, 16:06

#165 Post by edwin.evans »

edwin.evans wrote:
edwin.evans wrote:
Semme wrote:Does iwconfig list an interface?
I think only if I run rc.network. I'm rebooting now to check for sure.
If I run iwconfig, it doesn't list any wireless interface. Just lo and eth0. And the light on my adapter is not blinking. If I run rc.network at this point then the light starts blinking soon after the script starts.
OK, it seems I just need to run "modprobe ndiswrapper".

But when I try this in my script in /etc/init.d it doesn't work. It only works if it call it manually after bootup. Is there something else I need to do first or wait for? Thanks!

User avatar
Semme
Posts: 8399
Joined: Sun 07 Aug 2011, 20:07
Location: World_Hub

#166 Post by Semme »

Yeah, it's gonna be someone else. Seems I've misplaced my ndiswrapper using card and have to bail- sorry.

User avatar
JamesTheAwesomeDude
Posts: 99
Joined: Tue 29 Jan 2013, 17:17
Location: Classified

#167 Post by JamesTheAwesomeDude »

Ibidem wrote:
JamesTheAwesomeDude wrote:Okay, I can't figure out what to do:

Wi-Fi adapter, wlan0
Connecting to wireless router with WPA2 encryption
Using the carl9170usb driver
Static (custom) local IP
Custom DNS settings

What do I need to type? The stupid carl9170 driver is really crashy, and I hate having to make 40 clicks every 5 minutes to repair the internet, so I'm just gonna stick a script on the desktop labeled "Kick the Wi-Fi adapter" or something like that, that will auto-reconnect everything.
Something like this (UNTESTED! CAUTION! YOU PROBABLY WILL NEED TO DEBUG AND CHANGE THIS!):

Code: Select all

#!/bin/sh
IF=wlan0
#You probably want to change these.
IP=10.0.0.100 
GW=0.0.0.0
#DNS: saved in /etc/resolv.conf, which should be static.

#modalias is a not-really-readable hw-specific ID that identifies one module.
read MOD </sys/class/net/${IF}/../../modalias
export MOD IF IP
start(){
wpa_supplicant -Dwext -B -c /etc/wpa.conf -i $IF
#I'm guessing the IP needs to be set after the interface is up.
#Really, this should be called via an action script, instead of dhcp
sleep 5
ifconfig $IF $IP
route add default gw $GW dev $IF
}
reload(){
wpa_cli -i $IF terminate
ifconfig $IF down
#Disable hardware encryption. Known to be desirable for AR9170 chips:
# http://linuxwireless.org/en/users/Drivers/carl9170
modprobe -r $MOD && modprobe $MOD nohwcrypt=1
}
reload && start 
This assumes that the WPA config is in /etc/wpa.conf (probably not true for you!)
Hmm, couldn't manage to debug that ^, but I wrote this from scratch (many thanks to the Arch Linux Wiki):

Code: Select all

#!/bin/sh
killall wpa_supplicant
rfkill unblock 0
modprobe carl9170
rm /run/wpa_supplicant/wlan0
ip link set wlan0 up
wpa_supplicant -B -Dwext -i wlan0 -c /etc/wpa_supplicant.conf
ip addr add 192.168.2.64/24 dev wlan0
ip route add default via 192.168.2.1
Gets everything connected in <2 seconds, much faster then "manually" clicking through all the GUI options. It can also reconnect when carl9170 dies (I do not like carl9170 one bit. >:()

Before you can use the handy-dandy script I made, you have to do this:

Code: Select all

wpa_passphrase "Your_Wifi_SSID" "v3ry_g00d_p@$$phr@$3" >> /etc/wpa_supplicant.conf
[url=http://www.murga-linux.com/puppy/viewtopic.php?t=84349][img]http://i.imgur.com/Zw6vryI.png[/img][color=#3B6EA3][b][size=200] Version 27. Dotpet. Click here.[/size]
(SFS available too!)[/b][/color][/url]

der-schutzhund
Posts: 1045
Joined: Mon 26 Nov 2007, 22:07
Location: Blomberg / Germany

#168 Post by der-schutzhund »

I vainly trying to configure a wireless connection.
Went through the entire post and tried a lot but it does not work!
Using the Simple Network setup I can manually establish a connection but not automatically by script!
How should be a simple script?

Greetings

Wolfgang
Attachments
wlan-2.jpg
(25.71 KiB) Downloaded 2049 times
wlan-1.jpg
(32.57 KiB) Downloaded 2489 times

tempestuous
Posts: 5464
Joined: Fri 10 Jun 2005, 05:12
Location: Australia

#169 Post by tempestuous »

der-schutzhund wrote:I vainly trying to configure a wireless connection.
Went through the entire post and tried a lot but it does not work!
It's not necessary to go through the entire post. The complete instructions are in the 11th post on the first page of this thread - "PART 2: WPA encryption."
http://www.murga-linux.com/puppy/viewto ... 336#159336
Summary:
- first check your wifi router to see if it uses WPA or WPA2 encryption.
If WPA, use Geany to open /etc/network-wizard/wireless/wpa_profiles/wpa_supplicant.conf
If WPA2, use Geany to open /etc/network-wizard/wireless/wpa_profiles/wpa_supplicant2.conf

Add your SSID ("3210 Phone WLAN SL") and Personal Security Key (0815A31F), and save.
Assuming WPA2, you can now run a script with this -

Code: Select all

ifconfig wlan0 up
rm /var/run/wpa_supplicant/*
wpa_supplicant -i wlan0 -D wext -c /etc/network-wizard/wireless/wpa_profiles/wpa_supplicant2.conf -B
sleep 15
rm -f /var/lib/dhcpcd/*.info
rm -f /var/run/*.pid
dhcpcd -t 30 -h puppypc -d wlan0

der-schutzhund
Posts: 1045
Joined: Mon 26 Nov 2007, 22:07
Location: Blomberg / Germany

#170 Post by der-schutzhund »

Hi tempestuous,

thanks for the information! With these lines, it works!!!

When I work with the file /etc/network-wizard/wireless/wpa_profiles/wpa_supplicant.conf I had to remaster it.
Otherwise I have to make entries manually every time.
How can I set up the wireless without a remaster?

Can I enter the ssid and the Personal Security Key in the script? If so how?

Greetings

Wolfgang

Post Reply