连接到无线网络后调用脚本

连接到特定的无线网络后,我有办法调用shell脚本吗? 我想这样做的原因是我必须先登录网络才能开始使用它,如果可能的话我想自动化。

我读到了这个问题: 每次连接到特定的无线网络时,有没有办法运行脚本?

但我真的不确定如何使用暴发户来做到这一点。

为我之前的答案道歉,这是我多年前所做的。 似乎情况发生了变化。

事实certificate,当连接发生变化时,Network Manager会运行/etc/NetworkManager/dispatcher.d/目录中的所有脚本(由root拥有,可执行,其他用户无法读取,而不是setuid)的所有脚本(上,下,preup,堕落)。

环境变量由网络管理员设置并传递给此脚本。 您将对CONNECTION_UUID环境变量感兴趣(包含唯一的字符串)。

因此,要解决您的问题(在连接特定无线网络时执行脚本):

1)找出你感兴趣的无线连接的uuid(通过查看/etc/NetworkManager/system-connections/目录中的相应连接文件)。

2)如果环境变量CONNECTION_UUID与上面(1)中的无线网络的uuid匹配,则编写一个bash(或perl,或python,或其他)脚本,它可以执行您想要的操作。

3)将此脚本放入/etc/NetworkManager/dispatcher.d/并适当设置所有者和权限。

进一步阅读:man networkmanager(以及上面提到的目录中的脚本)。

示例脚本:

 #!/bin/bash ##################################### # MounterBeast Script # /etc/NetworkManager/dispatcher.d/02remotemount # Copyright 2011 Nathan E. Williams # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . # # Usage: # This script must be customized for your configuration. # By default, the script will attempt to mount a CIFS share # when a specified MAC address is found at the network gateway, # or over sshfs if the MAC address of the gateway is not the specified MAC. # eg I mount over CIFS to the servers internal IP when at home, and # over sshfs when away from home. # # id gateway mac without physically checking the sticker: # $ arp -n -a $(ip route show 0.0.0.0/0 | awk '{print $3}') | awk '{print $4}' # # Testing: # up) sudo /etc/NetworkManager/dispatcher.d/02remotemount wlan0 up # down) sudo /etc/NetworkManager/dispatcher.d/02remotemount wlan0 down ##################################### # # Configuration: # targetmac='xx:xx:xx:xx:xx:xx' mount_user='$USER' mount_pass='pass' internal_server_name='192.168.1.102' external_server_name='my.dyndns.com' share_name="music" mount_point='/mnt/remote' ssh_port='22' # # Should not need to edit below # gateway=$(ip route show 0.0.0.0/0 | awk '{print $3}') mactest=$(arp -n -a $gateway | awk '{print $4}') if [[ "$mactest" == "$targetmac" ]] then case "$2" in up) sleep 5 mount -t cifs -o username=$mount_user,password=$mount_pass //$internal_server_name/$share_name $mount_point ;; down) umount -l $mount_point ;; esac else case "$2" in up) sleep 5 sshfs -p $ssh_port $external_server_name:$share_name $mount_point ;; down) umount -l $mount_point ;; esac fi exit $? 

我不知道是否有办法使用网络管理器,有一个可能,但我有另一个解决方案。 你可以安装Wicd:

 sudo apt-get install wicd 

Wicd直接在gtk界面上提供支持,为每个可以连接的网络添加前脚本和后脚本支持。 小心Wicd将卸载Network-Manager工作(它们都有冲突)所以如果出现问题你应该下载.deb的Network-Manager或者带一个Live-CD / Live-USB。

Wicd易于使用且连接速度更快,但缺少Network-Manager(如VPN)的一些高级function。 这是一个截图:

WICD

是的,/ /etc/NetworkManager/dispatcher.d/ NetworkManager / /etc/NetworkManager/dispatcher.d/ Shell脚本是一个非常好的主意。

还有一个使用NetworkManager的Dbus方法,更有趣,更复杂: man nm-settings

从NetworkManager的Man页面关于dispatcher的shell参数的简历:

每个脚本都接收两个参数,第一个是刚刚激活的设备的接口名称,第二个是动作。

操作可以是:up,down,vpn-up,vpn-down,hostname,dhcp4-change,dhcp6-change。 (发布手册页:2012年1月17日)

这是一个在网络接口启动后重启OpenVPN的非常简单的脚本:

 if [ "$2" = "up" ]; then /etc/init.d/openvpn restart fi exit $?