herbfargus
Posts: 10
Joined: Sat Jul 18, 2015 11:51 pm

Wifi Bash Script

Sun Jul 19, 2015 12:13 am

I'm really new to coding and the Raspberry Pi, but I have played around with it, and just thought I'd share with the community what I've been working on. I've used wpa_gui and wicd-curses and varying other methods of connecting to wifi, but when it comes to very basic terminal scripts it seems all of them lacked a comprehensive overview of connections to different security types like WEP, open networks, hidden SSID's, and static ip's. I use my Pi for specific things and I don't like the gui's and daemons taking up processing but I also don't like typing in wifi configs manually either so my script is a simple terminal gui for beginners to connect to wifi.

This script is a simple terminal GUI using whiptail that can connect

-WPA/WPA2
-WEP
-Open Networks
-Static IP's
-Hidden SSID's

to install-
download the script

Code: Select all

wget https://raw.githubusercontent.com/HerbFargus/Raspberry-Pi-Wifi-Scripts/master/raspberrypiwifi.sh
type the following to make it executable

Code: Select all

sudo chmod +x raspberrypiwifi.sh
then you'll want to run it with sudo

Code: Select all

sudo ./raspberrypiwifi.sh
for now it only modifies /etc/network/interfaces but in the future I have plans to integrate wpa_supplicant.conf so it can be used with multiple networks. I know the code is a bit redundant and can definitely be improved but I'm open to new ideas and suggestions for improvement :)

It's based on
https://github.com/Death259/PiAssist
with tips taken from here
viewtopic.php?t=7592

bbmark
Posts: 1
Joined: Sun Aug 02, 2015 3:37 am

Re: Wifi Bash Script

Sun Aug 02, 2015 3:46 am

This is the most useful script I have found since I started working with the Raspberry Pi. I worked for many hours trying to get my Pi to connect to my wifi with the Edimax 7811. My router is set to wpa2. I could not figure out the correct combination of the files to get it to connect. I was able to connect my Pi to the internet with an Ethernet cable and then simply cut and paste the instructions you gave in this post and bam! I could actually scan for wifi sites and enter my site and it connected. Thanks so much. Brian.

herbfargus
Posts: 10
Joined: Sat Jul 18, 2015 11:51 pm

Re: Wifi Bash Script

Sun Aug 02, 2015 5:40 am

I'm glad it was useful to you! Just for reference I compiled a list of basically every possible configuration for wireless networks there is- that way for people who don't have an ethernet cord can set it up manually without needing to download my script. There are two main methods of configuring wifi-

1. only editing /etc/network/interfaces

2. editing both /etc/network/interfaces and /etc/wpa_supplicant/wpa_supplicant.conf (your network SSID and password will be added to wpa_supplicant whereas interfaces is edited to reference wpa_supplicant- this way enables you to be connected to multiple networks/ have it automatically reconnect if your router resets for some reason. I much prefer this way.

I could put it all on one code block but to eliminate any doubt, I'll put each full configuration in.

1. /etc/network/interfaces

WPA/WPA2

Code: Select all

auto lo

iface lo inet loopback
iface eth0 inet dhcp

allow-hotplug wlan0
auto wlan0
iface wlan0 inet dhcp
   wpa-ssid "NETWORK_NAME"
   wpa-psk "NETWORK_PASSWORD"
WEP

Code: Select all

auto lo

iface lo inet loopback
iface eth0 inet dhcp

allow-hotplug wlan0
auto wlan0
iface wlan0 inet dhcp
  wireless-essid NETWORK_NAME
  wireless-key WEP_KEY
OPEN NETWORK

Code: Select all

auto lo

iface lo inet loopback
iface eth0 inet dhcp

allow-hotplug wlan0
auto wlan0
iface wlan0 inet dhcp
  wireless-essid NETWORK_NAME
  wireless-mode managed
HIDDEN SSID

Code: Select all

auto lo

iface lo inet loopback
iface eth0 inet dhcp

allow-hotplug wlan0
auto wlan0
iface wlan0 inet dhcp
   wpa-ssid "NETWORK_NAME"
   wpa-psk "NETWORK_PASSWORD"
   wpa-scan-ssid 1
STATIC IP

Code: Select all

auto lo

iface lo inet loopback
iface eth0 inet dhcp

allow-hotplug wlan0
auto wlan0

iface wlan0 inet static 
   wpa-ssid "NETWORK_NAME" #(set these lines for their respective encryption type- wpa, wep, etc.)
   wpa-psk "NETWORK_PASSWORD"
address 192.168.0.110 #(This is the IP you want your raspberry pi to have)
netmask 255.255.255.0 #(This is almost always the same)
gateway 192.168.0.1 #(almost always the same as well. you can verify with netstat -nr)
____________________________________________________________________________________________________________________________________________________________________________________________________

2. WPA SUPPLICANT

/etc/network/interfaces when using wpa_supplicant.conf

wpa-roam (roaming PREFERRED)

Code: Select all

auto lo

iface lo inet loopback
iface eth0 inet dhcp

allow-hotplug wlan0
auto wlan0

iface wlan0 inet manual
wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
iface default inet dhcp
wpa-conf (non roaming)

Code: Select all

auto lo

iface lo inet loopback
iface eth0 inet dhcp

allow-hotplug wlan0
auto wlan0

iface wlan0 inet dhcp
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
wpa-roam (roaming static IP PREFERRED)

Code: Select all

auto lo

iface lo inet loopback
iface eth0 inet dhcp

allow-hotplug wlan0
auto wlan0

iface wlan0 inet manual
wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
iface default inet static
address 192.168.0.110 #(This is the IP you want your raspberry pi to have)
netmask 255.255.255.0 #(This is almost always the same)
gateway 192.168.0.1 #(almost always the same as well. you can verify with netstat -nr)
wpa-conf (non roaming static IP)

Code: Select all

auto lo

iface lo inet loopback
iface eth0 inet dhcp

allow-hotplug wlan0
auto wlan0

iface wlan0 inet static
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
address 192.168.0.110 #(This is the IP you want your raspberry pi to have)
netmask 255.255.255.0 #(This is almost always the same)
gateway 192.168.0.1 #(almost always the same as well. you can verify with netstat -nr)

/etc/wpa_supplicant/wpa_supplicant.conf

WPA/WPA2

Code: Select all

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
    ssid="NETWORK_NAME"
    psk="NETWORK_PASSWORD"
}
WEP

Code: Select all

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
     ssid="NETWORK_NAME"
     key_mgmt=NONE
     wep_tx_keyidx=0 #this forces it to use wep_key0
     wep_key0=YOURWEPKEY
}
OPEN NETWORK

Code: Select all

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
    ssid="NETWORK_NAME"
    key_mgmt=NONE
}
HIDDEN SSID

Code: Select all

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
    ssid="NETWORK_NAME" # it can be any encryption type, just make sure to add the "scan_ssid=1" line after your settings.
    key_mgmt=NONE
    scan_ssid=1
}

User avatar
KLL
Posts: 1453
Joined: Wed Jan 09, 2013 3:05 pm
Location: thailand
Contact: Website

Re: Wifi Bash Script

Mon Aug 03, 2015 2:26 am

great documentation ( but i did not try the bash script, though it looks nice )
i ref. to
1. /etc/network/interfaces
- WPA/WPA2 STATIC IP

Code: Select all

iface wlan0 inet static
   wpa-ssid "NETWORK_NAME"
   wpa-psk "NETWORK_PASSWORD"
   address 192.168.1.xxx
not only that it works, also with this it is possible to start NOOBS (silent install ) WIFI ONLY static IP
means fresh noobs SD, edit ... to silent install, see also http://kll.engineering-news.org/kllfusi ... icle_id=94 "noobs noir"
and add in
/os/Raspbian/partition_setup.sh

Code: Select all

sed /tmp/2/etc/network/interfaces -i -e '/wpa-conf/d'
sed /tmp/2/etc/network/interfaces -i -e 's|iface wlan0 inet manual|iface wlan0 inet static|'
sed /tmp/2/etc/network/interfaces -i -e '/iface wlan0/a\wpa-ssid "My-ssid" '
sed /tmp/2/etc/network/interfaces -i -e '/wpa-ssid/a\wpa-psk "We-love-Raspberry-Pi" '
sed /tmp/2/etc/network/interfaces -i -e '/wpa-psk/a\address 192.168.1.101'
sed /tmp/2/etc/network/interfaces -i -e '/address/a\gateway 192.168.1.1' 
put noobs noir SD in RPI with usb hub powered WIFI adapter, power up and wait 35min,
login putty SSH [email protected] ( even without HDMI or access to router dhcp info... (there would find a second IP..))
( you only must be sure that IP "101" is not used by any other device in the network / so ping first )

while in a raspbian only image i could edit cmdline.txt [space]ip=192.168.1.101
but i would not know how to pre set WIFI also ( from DOS) unless it would be delivered
with the "source /boot/interfaces.conf" switch in /etc/network/interfaces... there's lot to learn

but again, thanks for this great WIFI summary.

Return to “Beginners”