从命令行连接到无线网络

Bal*_*aji 5 command-line wireless wpa-supplicant

我需要编写一个 shell 脚本来连接到两个可用的 wi-fi 连接之一。一种是不安全的连接,另一种是安全的连接。我的问题有两部分-

  1. 当我连接到安全连接时,如何从命令行(或通过执行 shell 脚本)连接到不安全(未加密且不需要密码)的连接?

    我按照http://www.ubuntugeek.com/how-to-troubleshoot-wireless-network-connection-in-ubuntu.html 中的步骤进行安全连接。我将所有命令放在一个脚本中并执行它(我确保接口名称和 essid 是正确的)

    sudo dhclient -r wlan0
    sudo ifconfig wlan0 up
    sudo iwconfig wlan0 essid "UAPublic"
    sudo iwconfig wlan0 mode Managed
    sudo dhclient wlan0
    
    Run Code Online (Sandbox Code Playgroud)

    但是没有任何反应:我没有断开当前网络并连接到新网络。

  2. 当我想连接到安全的 wi-fi 网络时,我从https://askubuntu.com/a/138476/70665了解到我需要使用wpa_supplicant. 但是当我通过UI连接时,我在界面中输入了很多细节

    • 安全性:wpa 和 wpa2 企业
    • 身份验证:PEAP
    • CA证书:Equifax...
    • PEAP 版本:自动
    • 内部认证:MSCHAPv2
    • 用户名 :
    • 密码 :

如何使用 wpa_supplicant 在命令行中提及所有这些细节?配置文件

network={
        ssid="ssid_name"
        psk="password"
}
Run Code Online (Sandbox Code Playgroud)

对我不起作用。

roa*_*dmr 8

我假设您使用的是 Ubuntu 桌面,因为您没有另外指定。

您可以通过在/etc/NetworkManager/system-connections. 查看现有文件以了解格式的外观。

创建连接后,您可以使用该nmcli命令从命令行管理 NetworkManager,执行诸如启用、禁用和查询连接之类的操作。

顺便说一下,系统测试工具(复选框)有一个脚本可以完成此操作:创建一个连接并启用它,并使用您在命令行上提供的参数。

例如,这会创建到开放的“duck”网络的连接:

sudo /usr/share/checkbox/scripts/create_connection duck
Run Code Online (Sandbox Code Playgroud)

这将使用 WPA2 安全性创建与网络的连接,并使用“wings”密码:

sudo /usr/share/checkbox/scripts/create_connection -S wpa -K wings duck
Run Code Online (Sandbox Code Playgroud)

该脚本是用 Python 编写的,因此您应该很容易查看并适应您的需求。

脚本的帮助是这样说的:

Usage: create_connection [options] SSID

Options:
  -h, --help            show this help message and exit
  -S SECURITY, --security=SECURITY
                        The type of security to be used by the connection.
                        One of wpa and wep. No security will be used if
                        nothing is specified.
  -K KEY, --key=KEY     The encryption key required by the router.
  -U UUID, --uuid=UUID  The uuid to assign to the connection for use by
                        NetworkManager. One will be generated if not
                        specified here.
  -R RETRIES, --retries=RETRIES
                        The number of times to attempt bringing up the
                        connection until it is confirmed as active.
  -I INTERVAL, --interval=INTERVAL
                        The time to wait between attempts to detect the
                        registration of the connection.
Run Code Online (Sandbox Code Playgroud)