This command seems to be kinda outdated because it only supports wep:
iwconfig wlan0 essid WIFI_NETWORK key s:WIFI_PASSWORD dhclient wlan0
I found an alternative way that supports wpa2, but it involved changing a .conf file.
I want really to connect to wifi networks just by running the script. :(