AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 13963
Accepted
Brock Dute
Brock Dute
Asked: 2010-11-19 23:14:00 +0800 CST2010-11-19 23:14:00 +0800 CST 2010-11-19 23:14:00 +0800 CST

连接无线网络后调用脚本

  • 772

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

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

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

10.04 scripts networking
  • 3 3 个回答
  • 22365 Views

3 个回答

  • Voted
  1. Best Answer
    finley
    2010-11-21T01:18:24+08:002010-11-21T01:18:24+08:00

    为我之前的回答道歉,这是我几年前会这样做的方式。似乎情况发生了变化。

    事实证明,网络管理器运行/etc/NetworkManager/dispatcher.d/目录中的所有脚本(那些由 root 拥有的,可执行的,其他用户无法读取的,并且不是 setuid),当连接发生变化时(up、down、preup、predown) .

    环境变量由网络管理器设置并传递给此脚本。您将对 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 $?
    
    • 19
  2. Nyamiou The Galeanthrope
    2010-11-20T17:38:18+08:002010-11-20T17:38:18+08:00

    我不知道网络管理器是否有办法做到这一点,可能有,但我有另一种解决方案给你。您可以安装 Wicd :

    sudo apt-get install wicd
    

    Wicd 直接在 gtk 界面上支持为您可以连接的每个网络添加前脚本和后脚本支持。请注意 Wicd 会卸载 Network-Manager 以使其正常工作(它们都存在冲突),因此如果出现问题,您应该下载 Network-Manager 的 .deb 或随身携带 Live-CD/Live-USB。

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

    威德

    • 2
  3. Yvan
    2014-06-11T07:07:14+08:002014-06-11T07:07:14+08:00

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

    还有一个带NetworkManager的Dbus方法,更好玩,更复杂:man nm-settings.

    NetworkManager 手册页中关于 shell 参数的简历dispatcher:

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

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

    这是一个非常简单的脚本,用于在网络接口出现后重新启动 OpenVPN up:

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

相关问题

  • 使用 dpkg 手动安装软件包是否会阻止未来的升级路径?

  • 如何完全删除 Ruby + Rails + Gems?

  • 从 8.04 LTS 升级到 10.04 LTS 的体验?

  • 删除在线状态菜单,但保留注销菜单?

  • 什么可能会阻止 xrandr 屏幕之间的鼠标移动?[关闭]

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何安装 .run 文件?

    • 7 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    如何获得 CPU 温度?

    • 21 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Marko Smith

    如何使用命令行将用户添加为新的 sudoer?

    • 7 个回答
  • Marko Smith

    更改文件夹权限和所有权

    • 9 个回答
  • Marko Smith

    你如何重新启动Apache?

    • 13 个回答
  • Marko Smith

    如何卸载软件?

    • 11 个回答
  • Marko Smith

    如何删除 PPA?

    • 26 个回答
  • Martin Hope
    NES 如何启用或禁用服务? 2010-12-30 13:03:32 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    Olivier Lalonde 如何在结束 ssh 会话后保持进程运行? 2010-10-22 04:09:13 +0800 CST
  • Martin Hope
    David B 如何使用命令行将用户添加为新的 sudoer? 2010-10-16 04:02:45 +0800 CST
  • Martin Hope
    Hans 如何删除旧内核版本以清理启动菜单? 2010-08-21 19:37:01 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve