I have been doing large scale deployments of Raspberry Pi’s for some of my students and their class projects; and after doing… say.. two of them decided it would be easier to script the initial setup. The process isn’t hard but I thought I would document it in case anyone else was in a similar situation.
I start by connecting the Pi’s to a network via cable (some people carry handkerchiefs, I carry switches.) Raspbin starts with DHCP enabled and SSH configured for a default user, meaning we can use that to get the wireless configured. Here is basically what I do in my script.
Start by doing a port scan for any ssh connections on the network once the Pi is attached. For example:
nmap -T5 -n -p 22 –open –min-parallelism 200 172.16.0.0/24
We do this to pre-load our local arp table with IP & MAC addresses. This will speed up the process of finding any Raspberry registered MAC addresses (Raspberry has their own MAC range.) You can then search for Raspberry nics’ specifically by doing:
arp -a | grep b8:27:eb
SSH (or better yet copy your private key via ssh-copy-id) to the IP address(es) returned from the above command. Make sure to change the password afterwards. The default username and password for the SSH connection are:
Plugin your wireless USB (unless you have a PI3 or later) and run the following command to see the wireless card:
The result will be a list of physical wireless devices. You’re looking for the entry next to Interface mostly likely something like wlan0. Run the iwlist command to get a list of wireless access points you can connect to.
iwlist wlan0 scanning
Specifically you’re looking for the value next to ESSID. Find the one you want to connect to. To setup the encryption for secure wireless run the following command to add a specific network entry for your ESSID. Replace XXXX with the ESSID name you want to connect to.
wpa_passphrase “XXXX” >> /etc/wpa_supplicant/wpa_supplicant.conf
Now type the wireless access point password and hit enter. Finally restart the wireless interface to load the new network and get an IP address. Replace wlan0 with the Interface name you used for scanning a couple steps above.
The ifconfig is to see what your new wireless IP address is. You can then safely disconnect the wired network cable and SSH back into the PI on the wireless nic. The PI can safely be restarted at this point as the wireless will auto-connect on restart.