This article is part of a series: you can find the first article here. If you missed the previous one, it is here.

After the previous article, we can plug-in our HackOTG and log in with SSH over the emulated Ethernet-adapter. Now, we don’t have internet-connectivity yet… Sometimes we will want to have an internet-connection to install new packages, backup our device or have a new way to connect to our device. In this article we will go through the basics of networking on your HackOTG.

Setting up the interface and scan

Log in to your HackOTG over the emulated Ethernet device:

ifconfig usb0 192.168.7.3
ssh [email protected] #password is "raspberry"

Activate your Wifi-interface:

sudo ifconfig wlan0 up

Scan for available Wifi-Hotspots:

sudo iwlist wlan0 scan | less

Associate with the access point

There are a couple different security mechanisms, in the previous performed scan you’ll find which one your chosen hotspot uses.

WEP

This is one of the oldest security mechanisms, which has many flaws but this is how you connect: (replace “testessid” and “safestpasswordever”)

sudo iwconfig wlan0 essid testessid key s:safestpasswordever

WPA

A common security mechanism is WPA. WPA has many flavours but most of them will be accesible with the following steps.

Create a config-file: (replace “MYSSID” and “passphrase”)

wpa_passphrase MYSSID passphrase > MYSSID.conf

This will create a config-file with a hashed passphrase (for security reasons). If it is a public (unsecured) WiFi, you can create the same file with these contents.

network={
 ssid="MYSSID"
 key_mgmt=NONE
 }

To connect: (replace “interface” and “MYSSID”)

sudo wpa_supplicant -B -i interface -D wext -c MYSSID.conf

The driver “wext” can be changed out for other drivers found in the “wpa_supplicant -h” command. With the built-in wifi on the Pi zero, you can use “wext”. The “&” at the end of the command will run it in the background.

Requesting an IP

Now that we are assosiated and authenticated, we need an IP, We can get it from the DHCP server running on the router with this command:

sudo dhcpcd --nohook wpa_supplicant wlan0

Automate the process

You can write a bash script, so you can connect to a wifi-hotspot with one command. You can make multiple scripts and config-files to connect to different hotspots.

sh connect_wifi_ssid.sh

connect_wifi_ssid.sh:

sudo wpa_supplicant -B -i wlan0 -D wext -c ssid.conf
sudo dhcpcd --nohook wpa_supplicant wlan0

It is a good idea to make a connect_wifi_HELP.txt, that contains the wep and wpa_passphrase command and some guidelines to connect to WEP and WAP (so you don’t need this article).

connect_wifi_HELP.sh:

##SCANNING
sudo iwlist wlan0 scan
##WEP
sudo iwconfig wlan0 essid testessid key s:safestpasswordever
##WPA 
wpa_passphrase MYSSID passphrase > MYSSID.conf 
sudo wpa_supplicant -B -i wlan0 -D wext -c MYSSID.conf
sudo dhcpcd --nohook wpa_supplicant wlan0
#unsecured? change the conf file to: 
network={ 
 ssid="MYSSID"
 key_mgmt=NONE 
}

Forwarding the connection

You can forward the connectivity of your HackOTG through your emulated Ethernet device by simply enabling ipv4-forwarding with this command:

sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"

This can be handy if you need to register or fill in a form in a captive portal. Or if you just need a WiFi-connection.

Checking the internet connection

You will have 2 active interfaces now. By performing the “route -n” command, you can see the way your traffic is going to be handled. Sometimes you will want to choose the connection to use for you internet connectivity. You can do that with these scripts:

route_wlan0.sh: (to direct all traffic through the WiFi-connection)

sudo route del default
sudo dhcpcd -n wlan0

route_usb0.sh: (to direct all traffic the emulated ethernet connection)

sudo route del default
sudo dhcpcd -n usb0

Remember, if you dont’t have connectivity to the internet, check if the proper interface is on the first line in the output of the “route -n” command. But now that we have connectivity, we can update all our software with the following commands:

sudo apt-get update
sudo apt-get upgrade
sudo apt-get dist-upgrade

Great, we can connect to wifi-hotspots now, which we can not only use for an internet connection but if you know the IP (use ‘ifconfig’) you can connect to it over wifi.

Next, we will setup our own WiFi-hotspot, so we don’t have to configure a WiFi-hotspot over the emulated Ethernet-device if we want to SSH to the device over WiFi.

You can find the next one here:

HackOTG (v1.3): Creating our own hotspot on boot