title: Connect to wifi with bash script
body: <p>I've created a bash script that is supposed to connect to WiFi dynamically. Whether it is WPA, WEP, without password, with static ip or using DHCP.</p>
  
  <p>There is the script: </p>
  
  <pre><code>#!/bin/bash
  echo "Open script..."
  #NO PASSWORD
  if [ $3 ==  0 ]
  then
      echo "Without Password..."
      iw dev wlan0 connect $1
  fi
  
  if [ $3 == "WEP" ]
  then
      echo "Auth mode WEP..."
      iw wlan0 connect $1 key s:$2
  fi
  
  if [ $3 == "WPA" ] || [ $3 == "WPA2" ]
  then
      echo "Auth mode WPA"
      echo "ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
      update_config=1
      network={
              ssid=\"$1\"
              psk=\"$2\"
      }" | sudo tee --append /etc/wpa_supplicant/wpa_supplicant.conf &gt; 
   /dev/null
  
      echo "Finished to write on wpa supplicant..."
      sudo wpa_supplicant -B -i wlan0 -c/etc/wpa_supplicant/wpa_supplicant.conf -Dwext
  fi
  
  if [ $4 == "DHCP" ]
  then
      echo "dhcp mode on..."
      dhclient wlan0
  else 
      echo "dhcp mode off..."
      #ifconfig wlan0 $5 netmask $6 up
      #route add default gw $7
      #echo "nameserver $8"&gt;/etc/resolv.conf
   fi
  
   #SET DNS 2
   if [ $9 != 0 ]
   then
      echo "dn2 configuration..."
      #echo "nameserver $8"&gt;/etc/resolv.conf
   fi
  
   #RESTART UP
   echo "restarting process..."
   #ifconfig eth0 down
   #/etc/init.d/networking restart
   #pm2 stop 0
   #pm2 delete 0
   #pm2 start ecosystem.json
  </code></pre>
  
  <p>That's all. Some commands are commented because it is not finished yet. My problem is that the script isn't working. I gave it permissions on  <code>wpa_supplicant.conf</code>, but it still does not want to work.</p>
  
  <p>My errors are: </p>
  
  <pre><code>Finish to write on wpa_supplicant...
  Successfully initialized wpa_supplicant
  ioctl[SIOCSIWENCODEEXT]: Invalid argument
  ioctl[SIOCSIWENCODEEXT]: Invalid argument
  dhcp mode on...
  restarting process...
  stderr RTNETLINK answers: Operation not permitted
  </code></pre>
  
  <p>All variables work fine when I run the script, but somehow it still can't connect to WiFi. I've tried to use <code>nl80211</code> instead and it gives me this error:</p>
  
  <pre><code>Failed to create interface 
  p2p-dev-wlan0: -16 (Device or resource busy) nl80211: Failed to create 
  a P2P Device interface p2p-dev-wlan0 P2P: Failed to enable P2P Device 
  interface
  </code></pre>
  
  <p>More verbose errors: </p>
  
  <pre><code>nl80211: TDLS supported
  nl80211: Supported cipher 00-0f-ac:1
  nl80211: Supported cipher 00-0f-ac:5
  nl80211: Supported cipher 00-0f-ac:2
  nl80211: Supported cipher 00-0f-ac:4
  nl80211: Using driver-based off-channel TX
  nl80211: Supported vendor command: vendor_id=0x1018 subcmd=1
  nl80211: Use separate P2P group interface (driver advertised support)
  nl80211: use P2P_DEVICE support
  nl80211: interface wlan0 in phy phy0
  nl80211: Set mode ifindex 3 iftype 2 (STATION)
  nl80211: Subscribe to mgmt frames with non-AP handle 0x1b9f4a0
  nl80211: Register frame type=0xd0 (WLAN_FC_STYPE_ACTION) 
  nl_handle=0x1b9f4a0 match=040a
  nl80211: Register frame command failed (type=208): ret=-114 (Operation 
  already in progress)
  nl80211: Register frame match - hexdump(len=2): 04 0a
  nl80211: Register frame type=0xd0 (WLAN_FC_STYPE_ACTION) 
  nl_handle=0x1b9f4a0 match=040b
  nl80211: Register frame command failed (type=208): ret=-114 (Operation 
  already in progress)
  nl80211: Register frame match - hexdump(len=2): 04 0b
  nl80211: Register frame type=0xd0 (WLAN_FC_STYPE_ACTION) 
  nl_handle=0x1b9f4a0 match=040c
  nl80211: Register frame command failed (type=208): ret=-114 (Operation 
  already in progress)
  nl80211: Register frame match - hexdump(len=2): 04 0c
  nl80211: Register frame type=0xd0 (WLAN_FC_STYPE_ACTION) 
  nl_handle=0x1b9f4a0 match=040d
  nl80211: Register frame command failed (type=208): ret=-114 (Operation 
  already in progress)
  nl80211: Register frame match - hexdump(len=2): 04 0d
  nl80211: Register frame type=0xd0 (WLAN_FC_STYPE_ACTION) 
  nl_handle=0x1b9f4a0 match=090a
  nl80211: Register frame command failed (type=208): ret=-114 (Operation 
  already in progress)
  nl80211: Register frame match - hexdump(len=2): 09 0a
  nl80211: Register frame type=0xd0 (WLAN_FC_STYPE_ACTION) 
  nl_handle=0x1b9f4a0 match=090b
  nl80211: Register frame command failed (type=208): ret=-114 (Operation 
  already in progress)
  nl80211: Register frame match - hexdump(len=2): 09 0b
  nl80211: Register frame type=0xd0 (WLAN_FC_STYPE_ACTION) 
  nl_handle=0x1b9f4a0 match=090c
  nl80211: Register frame command failed (type=208): ret=-114 (Operation 
  already in progress)
  nl80211: Register frame match - hexdump(len=2): 09 0c
  nl80211: Register frame type=0xd0 (WLAN_FC_STYPE_ACTION) 
  nl_handle=0x1b9f4a0 match=090d
  nl80211: Register frame command failed (type=208): ret=-114 (Operation 
  already in progress)
  nl80211: Register frame match - hexdump(len=2): 09 0d
  nl80211: Register frame type=0xd0 (WLAN_FC_STYPE_ACTION) 
  nl_handle=0x1b9f4a0 match=0409506f9a09
  nl80211: Register frame command failed (type=208): ret=-114 (Operation 
  already in progress)
  nl80211: Register frame match - hexdump(len=6): 04 09 50 6f 9a 09
  nl80211: Register frame type=0xd0 (WLAN_FC_STYPE_ACTION) 
  nl_handle=0x1b9f4a0 match=7f506f9a09
  nl80211: Register frame command failed (type=208): ret=-114 (Operation 
  already in progress)
  nl80211: Register frame match - hexdump(len=5): 7f 50 6f 9a 09
  nl80211: Register frame type=0xd0 (WLAN_FC_STYPE_ACTION) 
  nl_handle=0x1b9f4a0 match=0801
  nl80211: Register frame command failed (type=208): ret=-114 (Operation 
  already in progress)
  nl80211: Register frame match - hexdump(len=2): 08 01
  nl80211: Register frame type=0xd0 (WLAN_FC_STYPE_ACTION) 
  nl_handle=0x1b9f4a0 match=040e
  nl80211: Register frame command failed (type=208): ret=-114 (Operation 
  already in progress)
  nl80211: Register frame match - hexdump(len=2): 04 0e
  nl80211: Register frame type=0xd0 (WLAN_FC_STYPE_ACTION)              
  nl_handle=0x1b9f4a0 match=06
  nl80211: Register frame command failed (type=208): ret=-114 (Operation     
  already in progress)
  nl80211: Register frame match - hexdump(len=1): 06
  nl80211: Register frame type=0xd0 (WLAN_FC_STYPE_ACTION)     nl_handle=0x1b9f4a0 match=0a07
  nl80211: Register frame command failed (type=208): ret=-114 (Operation already in progress)
   nl80211: Register frame match - hexdump(len=2): 0a 07
   nl80211: Register frame type=0xd0 (WLAN_FC_STYPE_ACTION)         nl_handle=0x1b9f4a0 match=0a11
   nl80211: Register frame command failed (type=208): ret=-114 (Operation already in progress)
   nl80211: Register frame match - hexdump(len=2): 0a 11
   nl80211: Register frame type=0xd0 (WLAN_FC_STYPE_ACTION)     nl_handle=0x1b9f4a0 match=1101
   nl80211: Register frame command failed (type=208): ret=-114 (Operation     already in progress)
   nl80211: Register frame match - hexdump(len=2): 11 01
   nl80211: Register frame type=0xd0 (WLAN_FC_STYPE_ACTION)     nl_handle=0x1b9f4a0 match=1102
   nl80211: Register frame command failed (type=208): ret=-114 (Operation already in progress)
   nl80211: Register frame match - hexdump(len=2): 11 02
   nl80211: Register frame type=0xd0 (WLAN_FC_STYPE_ACTION) nl_handle=0x1b9f4a0 match=0505
   nl80211: Register frame command failed (type=208): ret=-114 (Operation already in progress)
   nl80211: Register frame match - hexdump(len=2): 05 05
   nl80211: Failed to register Action frame processing - ignore for now
   netlink: Operstate: ifindex=3 linkmode=1 (userspace-control), operstate=5 (IF_OPER_DORMANT)
   nl80211: driver param='(null)'
   Add interface wlan0 to a new radio phy0
   nl80211: Regulatory information - country=00
   nl80211: 2402-2472 @ 40 MHz 20 mBm
   nl80211: 2457-2482 @ 20 MHz 20 mBm (no IR)
   nl80211: 2474-2494 @ 20 MHz 20 mBm (no OFDM) (no IR)
   nl80211: 5170-5250 @ 80 MHz 20 mBm (no IR)
   nl80211: 5250-5330 @ 80 MHz 20 mBm (DFS) (no IR)
   nl80211: 5490-5730 @ 160 MHz 20 mBm (DFS) (no IR)
   nl80211: 5735-5835 @ 80 MHz 20 mBm (no IR)
   nl80211: 57240-63720 @ 2160 MHz 0 mBm
   nl80211: Added 802.11b mode based on 802.11g information
   wlan0: Own MAC address: b8:27:eb:d0:13:64
   wpa_driver_nl80211_set_key: ifindex=3 (wlan0) alg=0 addr=(nil) key_idx=0 set_tx=0 seq_len=0 key_len=0
   wpa_driver_nl80211_set_key: ifindex=3 (wlan0) alg=0 addr=(nil) key_idx=1 set_tx=0 seq_len=0 key_len=0
   wpa_driver_nl80211_set_key: ifindex=3 (wlan0) alg=0 addr=(nil) key_idx=2 set_tx=0 seq_len=0 key_len=0
   wpa_driver_nl80211_set_key: ifindex=3 (wlan0) alg=0 addr=(nil) key_idx=3 set_tx=0 seq_len=0 key_len=0
   wpa_driver_nl80211_set_key: ifindex=3 (wlan0) alg=0 addr=(nil) key_idx=4 set_tx=0 seq_len=0 key_len=0
   wpa_driver_nl80211_set_key: ifindex=3 (wlan0) alg=0 addr=(nil) key_idx=5 set_tx=0 seq_len=0 key_len=0
   wlan0: RSN: flushing PMKID list in the driver
   nl80211: Flush PMKIDs
   TDLS: TDLS operation supported by driver
   TDLS: Driver uses internal link setup
   TDLS: Driver does not support TDLS channel switching
   wlan0: WPS: UUID based on MAC address: 7630b69b-9582-57c5-9979-4b06864bafb4
   ENGINE: Loading dynamic engine
   ENGINE: Loading dynamic engine
   EAPOL: SUPP_PAE entering state DISCONNECTED
   EAPOL: Supplicant port status: Unauthorized
   nl80211: Skip set_supp_port(unauthorized) while not associated
   EAPOL: KEY_RX entering state NO_KEY_RECEIVE
   EAPOL: SUPP_BE entering state INITIALIZE
   EAP: EAP entering state DISABLED
   Using existing control interface directory.
   ctrl_interface_group=108 (from group name 'netdev')
   wlan0: Added interface wlan0
   wlan0: State: DISCONNECTED -&gt; DISCONNECTED
   nl80211: Set wlan0 operstate 0-&gt;0 (DORMANT)
   netlink: Operstate: ifindex=3 linkmode=-1 (no change), operstate=5 (IF_OPER_DORMANT)
   nl80211: Create interface iftype 10 (P2P_DEVICE)
   Failed to create interface p2p-dev-wlan0: -16 (Device or resource busy)
   nl80211: Failed to create a P2P Device interface p2p-dev-wlan0
   P2P: Failed to create P2P Device interface
   P2P: Failed to enable P2P Device interface
   Daemonize..
   dhcp mode on...
   restarting process...
   stderr RTNETLINK answers: Operation not permitted
  </code></pre>
  
  <p>Can someone help me?</p>
  
  <p>Edit:
  Wpa supplicant after running script: </p>
  
  <pre><code>ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=network
      update_config=1
      network={
              ssid="WIFI"
              psk="Password"
      }
  ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
      update_config=1
      network={
              ssid="WIFI"
              psk="Password"
      }
  ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
      update_config=1
      network={
              ssid="WIFI"
              psk="Password"
      }
  ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
      update_config=1
      network={
              ssid="WIFI"
              psk="Password"
      }
   ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
      update_config=1
      network={
              ssid="WIFI"
              psk="Password"
      }
   ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
      update_config=1
      network={
              ssid="WIFI"
              psk="Password"
      }
  </code></pre>
  
  <p>I have just noticed that the same interface has been repeated several times, but it is due more to the fact that I have launched the script over and over again</p>