0

Given the following table

$ connmanctl services
*AO MyNetwork               wifi_dc85de828967_68756773616d_managed_psk
    OtherNET                wifi_dc85de828967_38303944616e69656c73_managed_psk 
    AnotherOne              wifi_dc85de828967_3257495245363836_managed_wep
    FourthNetwork           wifi_dc85de828967_4d7572706879_managed_wep
    AnOpenNetwork           wifi_dc85de828967_4d6568657272696e_managed_none

I'd like to be able to connect to a network, e.g. OtherNET, using the string OtherNET rather than the long wifi_dc85de828967_38303944616e69656c73_managed_psk, as I don't want to count the times I press Tab and/or check that the wifi_ line in the prompt corresponds to the intended network.

Is this possible with connman only? Or do I really have to write a wrapper myself?

The man page of connmanctl contains

   services
          Shows a list of all available services. This  includes  the
          nearby wifi networks, the wired ethernet connections, blue‐
          tooth devices, etc.  An asterisk in front  of  the  service
          indicates that the service has been connected before.

and

   connect service
          Connects  to  the  given  service. Some services need a so-
          called provisioning file in order to connect to  them,  see
          connman-service.config(5).

which both don't say much about the format of the output or the use of the command.

Similarly, the wiki on Arch Linux refers to the last column as the second field beginning with wifi_.

Enlico
  • 23,259
  • 6
  • 48
  • 102

1 Answers1

0

Since nobody has answered yet, I have found some spare time to code the following wrapper, which basically does the following

  • keeps reading as long as the input is not exit;
  • when an input is provided which starts with connect, the following word is used to pattern-match one line (only the first matching line) from connman services; the last field of this line (the one which starts with wifi_) is forwarded to connmanctl connect;
  • any other non-empty input is forwarded to connmanctl as it is.
  • a certain number (to be passed through the variable NOINPUTS_BEFORE_EXIT whose default is 3) of empty inputs causes the script to exit.

The script is the following

#!/usr/bin/env bash
name=$(basename $0)
noinputs_before_exit=${NOINPUTS_BEFORE_EXIT:=3}
while [[ $cmd != 'exit' ]]; do
  echo -n "$name " 1>&2
  read cmd
  if [[ -z "$cmd" ]]; then
    (( --noinputs_before_exit == 0 )) && exit
  else
    noinputs_before_exit=$NOINPUTS_BEFORE_EXIT
    if [[ $cmd =~ ^connect\  ]]; then
      connmanctl connect $(connmanctl services | awk '/'"${cmd#* }"'/ { print $NF; exit }')
    else
      connmanctl $cmd
    fi
  fi
done

The script limitations are at least the following:

  • (less importantly, to me) I have no idea whether it meets any security requirements;
  • it does not allow Tab-completion;
  • (most importantly, to me) it does not use the readline library, so line editing impossible.
Enlico
  • 23,259
  • 6
  • 48
  • 102