Balasankar C

Balasankar C

Geek. Freedom. Privacy.

Home | Blog | Talks | Setup | Feed

nmcli and adhoc wifi hotspot (a.k.a File sharing made easy by Python and nmcli)

Heyo,
Just like you, I was also using pendrives to transfer movies from my friends' laptops to mine. Yesterday, when I was at Thiruvananthapuram with SMC members for the annual meet and Swathanthra conference, Hrishi told me about python's SimpleHTTPServer package which can be used to create an HTTP Server running at any directory. Along with that, if we create an adhoc wifi-hotspot in our laptop, we can easily transfer files from our laptop to another. But, as I ditched using GUI some months back, I had to create the adhoc hotspot through command line, and who else is to the rescue that our dearest nmcli. But, I couldn't get nmcli to do the job, and fortunately Rajeeshettan was around and he did the magic. The steps are as follows (the $ sign is inserted manually to differentiate the command and its output. I ditched bash long time ago and am a hardcore lover of zsh)

  1. Create a connection using nmcli
    $ nmcli connection add type wifi ifname wlan0 ssid 'Balu-laptop'
    Connection 'wifi-wlan0' (5d29a639-97c7-49c0-8469-e845d3672d7b) successfully added.
  2. Here, Balu-laptop is the name I have given for the connection (it is called SSID) and wifi-wlan0 is the id assigned by nmcli for that connection

  3. After creating the connection, we have to edit the properties of that connection to make it an adhoc wifi hotspot. For that we have to enter the nmcli's editor mode (There should be options to do this in a single line. Have to experiment more)
    $ nmcli con edit wifi-wlan0
  4. This takes us to the command line mode of nmcli and we are greeted with a "nmcli>" prompt.

  5. Let's take a glance at the current properties of the connection

    nmcli> print
    ===============================================================================
    Connection profile details (wifi-wlan0)
    ===============================================================================
    connection.id: wifi-wlan0
    connection.uuid: 5d29a639-97c7-49c0-8469-e845d3672d7b
    connection.interface-name: wlan0
    connection.type: 802-11-wireless
    connection.autoconnect: yes
    connection.timestamp: 0
    connection.read-only: no
    connection.permissions:
    connection.zone: --
    connection.master: --
    connection.slave-type: --
    connection.secondaries:
    connection.gateway-ping-timeout: 0
    -------------------------------------------------------------------------------
    802-11-wireless.ssid: Balu-laptop
    802-11-wireless.mode: --
    802-11-wireless.band: --
    802-11-wireless.channel: 0
    802-11-wireless.bssid: --
    802-11-wireless.rate: 0
    802-11-wireless.tx-power: 0
    802-11-wireless.mac-address: --
    802-11-wireless.cloned-mac-address: --
    802-11-wireless.mac-address-blacklist:
    802-11-wireless.mtu: ഓട്ടോ
    802-11-wireless.seen-bssids:
    802-11-wireless.hidden: no
    -------------------------------------------------------------------------------
    ipv4.method: auto
    ipv4.dns:
    ipv4.dns-search:
    ipv4.addresses:
    ipv4.routes:
    ipv4.ignore-auto-routes: no
    ipv4.ignore-auto-dns: no
    ipv4.dhcp-client-id: --
    ipv4.dhcp-send-hostname: yes
    ipv4.dhcp-hostname: --
    ipv4.never-default: no
    ipv4.may-fail: yes
    -------------------------------------------------------------------------------
    ipv6.method: auto
    ipv6.dns:
    ipv6.dns-search:
    ipv6.addresses:
    ipv6.routes:
    ipv6.ignore-auto-routes: no
    ipv6.ignore-auto-dns: no
    ipv6.never-default: no
    ipv6.may-fail: yes
    ipv6.ip6-Privacy: -1 (അപരിചിതം)
    ipv6.dhcp-hostname: --
    -------------------------------------------------------------------------------
  6. Here we have to change the fields ipv4.method, ipv6.method, connection.autoconnect, 802-11-wireless.mode etc.
    1. We change the ipv4.method to shared
      nmcli> set ipv4.method shared
    2. We change the ipv6.method to ignore
      nmcli> set ipv6.method ignore
    3. We prevent network manager from automatically connecting to the connection
      nmcli> set connection.autoconnect no
    4. We change the mode of connection to adhoc from infrastructure
      nmcli> set 802-11-wireless.mode adhoc


  7. After editing, the property list looks like this===============================================================================
    Connection profile details (wifi-wlan0)
    ===============================================================================
    connection.id: wifi-wlan0
    connection.uuid: 5d29a639-97c7-49c0-8469-e845d3672d7b
    connection.interface-name: wlan0
    connection.type: 802-11-wireless
    connection.autoconnect: no
    connection.timestamp: 1418809676
    connection.read-only: no
    connection.permissions:
    connection.zone: --
    connection.master: --
    connection.slave-type: --
    connection.secondaries:
    connection.gateway-ping-timeout: 0
    -------------------------------------------------------------------------------
    802-11-wireless.ssid: Balu-laptop
    802-11-wireless.mode: adhoc
    802-11-wireless.band: --
    802-11-wireless.channel: 0
    802-11-wireless.bssid: --
    802-11-wireless.rate: 0
    802-11-wireless.tx-power: 0
    802-11-wireless.mac-address: --
    802-11-wireless.cloned-mac-address: --
    802-11-wireless.mac-address-blacklist:
    802-11-wireless.mtu: ഓട്ടോ
    802-11-wireless.seen-bssids:
    802-11-wireless.hidden: no
    -------------------------------------------------------------------------------
    ipv4.method: shared
    ipv4.dns:
    ipv4.dns-search:
    ipv4.addresses:
    ipv4.routes:
    ipv4.ignore-auto-routes: no
    ipv4.ignore-auto-dns: no
    ipv4.dhcp-client-id: --
    ipv4.dhcp-send-hostname: yes
    ipv4.dhcp-hostname: --
    ipv4.never-default: no
    ipv4.may-fail: yes
    -------------------------------------------------------------------------------
    ipv6.method: ignore
    ipv6.dns:
    ipv6.dns-search:
    ipv6.addresses:
    ipv6.routes:
    ipv6.ignore-auto-routes: no
    ipv6.ignore-auto-dns: no
    ipv6.never-default: no
    ipv6.may-fail: yes
    ipv6.ip6-Privacy: -1 (അപരിചിതം)
    ipv6.dhcp-hostname: --
    -------------------------------------------------------------------------------
  8. If everything looks fine, save the properties
    nmcli> save
  9. Quit the edit mode
    nmcli> quit
  10. Now lets activate the connection
    $ nmcli con up wifi-wlan0
  11. Now connect to the network and see if you get an IP. Suppose I get the IP 10.0.24.50. Also try it out from another computer and see if the hotspot gets detected and connect to the hotspot.
  12. Now go to the directory and run the python SimpleHTTPServer
    $ python -m SimpleHTTPServer
    Serving HTTP on 0.0.0.0 port 8000 ...
  13. Now from the other computer, visit our IP:8000 ( 10.0.24.50:8000 ) and see if the directory gets listed. If it does, all is well.