Bro*_*ute 15 10.04 scripts networking
一旦我连接到特定的无线网络,有没有办法让我调用 shell 脚本?我想这样做的原因是我必须先登录网络,然后才能开始使用它,如果可能的话,我想将其自动化。
我读到这个问题:有没有办法在每次连接到特定无线网络时运行脚本?
但我真的不确定如何使用 upstart 来做到这一点。
fin*_*ley 17
为我之前的回答道歉,这是我多年前的做法。似乎事情发生了变化。
事实证明/etc/NetworkManager/dispatcher.d/,当连接发生变化时(up、down、preup、predown),网络管理器会运行目录中的所有脚本(root 拥有的脚本、可执行的脚本、其他用户无法读取的脚本和 setuid 脚本) .
环境变量由网络管理器设置并传递给此脚本。您会对 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 <http://www.gnu.org/licenses/>.
#
# 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.
# e.g. 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 $?
Run Code Online (Sandbox Code Playgroud)