user1854496
user1854496

Reputation: 622

How to connect to wifi in bash?

I'd like to make a simple bash script to connect to known wifi networks. Thus far I have...

#!/bin/bash
NETWORK_ID=${1:myintranet}
WIRELESS_KEY=${2:""}
WIRELESS_DEVICE=${3:wlan0}

if [ ! -n "$WIRELESS_KEY" ]; then
        read -s -p "Enter Password: " WIRELESS_KEY
fi
#ifconfig wlan0
iwconfig wlan0 essid $NETWORK_ID key s:$WIRELESS_KEY
dhclient wlan0

I enter the plain text password for the network when requested and it fails with the error
iwconfig: unknown command "s:myPassword"

But I can't find any reason why it should be expecting a command and not translating the key to hex.

Upvotes: 1

Views: 5819

Answers (1)

Kumar Vikramjeet
Kumar Vikramjeet

Reputation: 263

This is working fine for me for WEP wifi. Don't forget to name the script with .sh extension.

#!/bin/bash
NETWORK_ID=${1:myintranet}
WIRELESS_KEY=${2:xxx}
WIRELESS_DEVICE=${3:wlan0}

if [ -z "$WIRELESS_KEY" ]; then
        read -s -p "Enter Password: " WIRELESS_KEY
fi
#ifconfig wlan0
iwconfig wlan0 essid $NETWORK_ID key s:$WIRELESS_KEY
dhclient wlan0

For WPA wifi, it may not work. Consider using wpa_supplicant or configure it using wicd (wicd-gtk)

Upvotes: 1

Related Questions