NoPaste

tlp-Skript

von Taomon

SNIPPET_TEXT:
  1. alex@Taomon:~$ cat /etc/NetworkManager/dispatcher.d/99tlp-rdw-nm
  2. #!/bin/sh
  3. # tlp-rdw - network manager dispatcher hook:
  4. #           enable/disable radios on ifup/ifdown
  5. #
  6. # Copyright (c) 2016 Thomas Koch <linrunner at gmx.net>
  7. # This software is licensed under the GPL v2 or later.
  8.  
  9. # --- Constants
  10. readonly LIBDIR="/usr/share/tlp-pm"
  11. readonly LIBS="tlp-functions tlp-rf-func"
  12.  
  13. # --- Functions
  14.  
  15. check_switch_lock() { # switch listed radio devices
  16.     # and time-lock them afterwards if actually switched
  17.     # $1: device where the event originated -- do nothing if its time-locked
  18.     # $2: list of device types to switch
  19.     # $3: on/off
  20.     local dev
  21.  
  22.     # quit if the originating *radio* device is time-locked (not LAN)
  23.     [ "$1" != "LAN" ] && check_timed_lock "${RDW_NM_LOCK}_$1" && return 1
  24.  
  25.     for dev in $2; do
  26.         if [ -n "$dev" ] && [ "$dev" != "$1" ]; then
  27.             # device type is valid and not the originating one
  28.             # --> do switch with state change lock
  29.             device_switch $dev $3 "${RDW_NM_LOCK}_${dev}" $RDW_LOCKTIME
  30.         fi
  31.     done
  32.  
  33.     return 0
  34. }
  35.  
  36. # --- Source libraries
  37. for lib in $LIBS; do
  38.     if [ ! -f $LIBDIR/$lib ]; then
  39.         echo "Error: missing function library \'$LIBDIR/$lib\'." 1>&2
  40.         exit 1
  41.     fi
  42.     . $LIBDIR/$lib
  43. done
  44.  
  45. # --- MAIN
  46. read_defaults
  47. check_tlp_enabled || exit 0
  48. add_sbin2path
  49.  
  50. # Get args
  51. iface="$1"
  52. action="$2"
  53. itype="unknown"
  54.  
  55. # Quit for invalid interfaces
  56. [ -n "$iface" ] && [ "$iface" != "none" ] || exit 0
  57.  
  58. # Quit for actions other than "up" and "down"
  59. [ "$action" = "up" ] || [ "$action" = "down" ] || exit 0
  60.  
  61. echo_debug "nm" "+++ rdw_nm($iface).$action"
  62. if [ -n "$addpath" ]; then
  63.     echo_debug "path" "PATH=$oldpath[$addpath]"
  64. else
  65.     echo_debug "path" "PATH=$oldpath"
  66. fi
  67.  
  68. # Determine interface type
  69. if cmd_exists $NMCLI ; then
  70.     # nmcli is available --> check if nmcli dev output matches interface
  71.     itype="$($NMCLI dev | awk '$1 ~ /^'$iface'$/ { print $2; }')"
  72.  
  73.     if [ -z "$itype" ]; then
  74.         # iface is not found in nmcli dev output: many WWAN devices have
  75.         # different devices for control and the actual network connection
  76.         # --> check if interface matches a WWAN device
  77.         get_wwan_ifaces
  78.         if wordinlist "$iface" "$wanifaces"; then
  79.             itype="wwan"
  80.         else
  81.             # fallback:
  82.             # if interface type detection with nmcli failed, then try to
  83.             # deduct it using interface name: it can happen if e.g.
  84.             # usb network card is unplugged
  85.             case "$iface" in
  86.                 en* | eth*)
  87.                     itype="ethernet"
  88.                     ;;
  89.  
  90.                 wl*)
  91.                     itype="wifi"
  92.                     ;;
  93.  
  94.                 ww*)
  95.                     itype="wwan"
  96.                     ;;
  97.  
  98.                 *)
  99.                     itype="unknown"
  100.                     ;;
  101.             esac
  102.         fi
  103.     fi
  104.     echo_debug "nm" "rdw_nm($iface).$action: type=$itype [nmcli]"
  105. else
  106.     # nmcli is not available
  107.     echo_debug "nm" "rdw_nm($iface)$action.nmcli_not_available"
  108. fi
  109.  
  110. case $action in
  111.     up) # interface up, disable configured interfaces
  112.  
  113.         case $itype in
  114.             *ethernet)
  115.                 check_switch_lock LAN "$DEVICES_TO_DISABLE_ON_LAN_CONNECT" off
  116.                 ;;
  117.  
  118.             *wireless|wifi)
  119.                 check_switch_lock wifi "$DEVICES_TO_DISABLE_ON_WIFI_CONNECT" off
  120.                 ;;
  121.  
  122.             gsm|wwan)
  123.                 check_switch_lock wwan "$DEVICES_TO_DISABLE_ON_WWAN_CONNECT" off
  124.                 ;;
  125.         esac
  126.         ;; # up
  127.  
  128.     down) # interface down, enable configured interfaces
  129.         case $itype in
  130.             *ethernet)
  131.                 check_switch_lock LAN "$DEVICES_TO_ENABLE_ON_LAN_DISCONNECT" on
  132.                 ;;
  133.  
  134.             *wireless|wifi)
  135.                 check_switch_lock wifi "$DEVICES_TO_ENABLE_ON_WIFI_DISCONNECT" on
  136.                 ;;
  137.  
  138.             gsm|wwan)
  139.                 check_switch_lock wwan "$DEVICES_TO_ENABLE_ON_WWAN_DISCONNECT" on
  140.                 ;;
  141.         esac
  142.         ;; # down
  143.  
  144. esac
  145.  
  146. exit 0

Quellcode

Hier kannst du den Code kopieren und ihn in deinen bevorzugten Editor einfügen. PASTEBIN_DOWNLOAD_SNIPPET_EXPLAIN