sanchescom / php-wifi

Cross-platform PHP library for scan, connect and disconnect Wi-Fi networks.
GNU General Public License v3.0
51 stars 14 forks source link

TypeError on Ubuntu Server 18.04 LTS with nmcli #11

Open fooness opened 5 years ago

fooness commented 5 years ago

php-wifi works on my macOS environment (development).

On Ubuntu Server 18.04 LTS (production), though, unfortunately I get an error.

Argument 1 passed to Sanchescom\WiFi\System\AbstractNetworks::explodeAvailableNetworks() must be of the type string, integer given, called in /var/www/html/vendor/sanchescom/php-wifi/src/System/Linux/Networks.php on line 52

In the debugger, the first line (line 65) of the following function from /var/www/html/vendor/sanchescom/php-wifi/src/System/AbstractNetworks.php is highligthed:

protected function explodeAvailableNetworks(string $networksString): array
{
  return explode("\n", trim($networksString));
}

Via SSH, that’s what I get when running nmcli:

nmcli
wlp2s0: unavailable
        "Intel Wireless 7265 (Dual Band Wireless-AC 7265)"
        wifi (iwlwifi), MY:MA:CA:DD:RE:SS, hw, mtu 1500

eno1: unmanaged
        "Intel Ethernet Connection (7) I219-LM"
        ethernet (e1000e), MY:MA:CA:DD:RE:SS, hw, mtu 1500

lo: unmanaged
        "lo"
        loopback (unknown), 00:00:00:00:00:00, sw, mtu 65536

Use "nmcli device show" to get complete information about known devices and
"nmcli connection show" to get an overview on active connection profiles.

Consult nmcli(1) and nmcli-examples(5) manual pages for complete usage details.

Any ideas what the problem might be?

Thank you very much.

sanchescom commented 5 years ago

Could you execute this command and show me result

LANG=C nmcli --terse --fields active,ssid,bssid,mode,chan,freq,signal,security,wpa-flags,rsn-flags device wifi list

fooness commented 5 years ago

LANG=C nmcli --terse --fields active,ssid,bssid,mode,chan,freq,signal,security,wpa-flags,rsn-flags device wifi list executes, but returns nothing.

sanchescom commented 5 years ago

I suppose you need to configure the operation system. Try to read here:

https://unix.stackexchange.com/questions/334477/nmcli-shows-nothing https://unix.stackexchange.com/questions/158328/network-manager-not-listing-wifi

fooness commented 5 years ago

I think the problem is that Ubuntu Server 18.04 uses netplan with nerworkd for setting up ethernet and wifi interfaces.

For my purposes it would be enough to use nmcli to only list wifi networks, there’s no need to save the wifi network and passphrase to some file, I only need the bssid of the available networks.

So maybe it’s possible to set up nmcli to be able to list wifi networks but not to interfere with netplan and networkd?

Or maybe it’s possible to use netplan to scan for wifi networks in php-wifi?

fooness commented 5 years ago

Seems I can relatively easy change netplan’s renderer from networkd to NetworkManager (which uses ncmli).

Unfortunately though, the nmcli […] list command in php-wifi does not scan (or rather nmcli […] rescan the network before creating the array of available wifi networks.

Is it somehow possible to force a rescan before creating the array?

Thank you!

sanchescom commented 5 years ago

You mean that after renderer was changed from networkd to NetworkManager the nmcli doesn't scan anyway? Why do you need rescan the network in this case?

fooness commented 5 years ago

Scanning works, kind of. But the peridocal scanning of nmcli is not enough. Imaginge you’d like to get a list of available wifi networks in between of information-of-last-scan-lost and new-scan-didn’t-happen-yet.

In regards of man nmcli:

wifi list [--rescan | auto | no | yes ] [ifname ifname] [bssid BSSID] ] List available Wi-Fi access points. The ifname and bssid options can be used to list APs for a particular interface or with a specific BSSID, respectively.

By default, nmcli ensures that the access point list is no older than 30 seconds and triggers a network scan if necessary. The --rescan can be used to either force or disable the scan regardless of how fresh the access point list is.

wifi rescan [ifname ifname] [ssid SSID...] Request that NetworkManager immediately re-scan for available access points. NetworkManager scans Wi-Fi networks periodically, but in some cases it can be useful to start scanning manually (e.g. after resuming the computer). By using ssid, it is possible to scan for a specific SSID, which is useful for APs with hidden SSIDs. You can provide multiple ssid parameters in order to scan more SSIDs.

So maybe … it would be possible to use nmcli device wifi list --rescan or nmcli device wifi list --rescan yes, but unfortunately Ubuntu Server 18.04 does not use this version of nmcli it seems. The version used is 1.10.6 … here’s the corresponding man page and it’s missing the --rescan parameter.

Another approach would be executing LANG=C nmcli device wifi rescan && sleep 5 && (or something alike) in php-wifi before the LANG=C nmcli […] wifi list function is executed. But I’m struggling with implementing this.