Last active
April 17, 2018 14:50
-
-
Save nanpuyue/af4c7feb9e23f439c1beccaa454fd3a1 to your computer and use it in GitHub Desktop.
多网络配置切换管理器
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # date: 2017-04-23 | |
| # author: nanpuyue <[email protected]> https://blog.nanpuyue.com | |
| CONF_FILE=~/.config/network_switcher.conf | |
| RESERVED_UUID="defa5e5c-16c4-426d-83ea-aa187f6e2ea6" | |
| read_conf(){ | |
| if [[ ! -f "$CONF_FILE" ]]; then | |
| touch "$CONF_FILE" | |
| fi | |
| case "$1" in | |
| "netcard") | |
| cat "$CONF_FILE"|grep -Po "(?<=$RESERVED_UUID\s).*" | |
| ;; | |
| "network") | |
| cat "$CONF_FILE"|grep -v "$RESERVED_UUID" | |
| ;; | |
| *) | |
| cat "$CONF_FILE"|grep -P "^$1\s" | |
| ;; | |
| esac | |
| } | |
| edit_conf(){ | |
| sed -ri "s/^$1\s+.*$/$*/g" "$CONF_FILE" && \ | |
| zenity --notification --window-icon="info" \ | |
| --text="\n已修改选定的网络配置" | |
| } | |
| del_conf(){ | |
| sed -ri "/^$1\s+.*$/d" "$CONF_FILE" && \ | |
| zenity --notification --window-icon="info" \ | |
| --text="\n已删除选定的网络配置" | |
| } | |
| select_network(){ | |
| local uuid=$(zenity --list --width=600 --height=400 --title="选择网络配置" \ | |
| --text="选中并单击确定,或直接双击选择" --hide-column="1" \ | |
| --column="UUID" --column="名称" --column="IP地址" --column="子网掩码" --column="默认网关" --column="DNS" \ | |
| $(read_conf "network") \ | |
| "$@" \ | |
| ) | |
| echo "$uuid" | |
| } | |
| edit_network(){ | |
| local uuid=$(select_network) | |
| if [[ -n "$uuid" ]]; then | |
| local conf=$(zenity --list --width=600 --height=400 --title="编辑网络配置" \ | |
| --text="双击编辑,各栏均不能留空,单击空白处或回车完成编辑\n如无 DNS 填 0,多个 DNS 以英文分号分割,单击确定保存" \ | |
| --editable --print-column=all --hide-column="1" \ | |
| --column="UUID" --column="名称" --column="IP地址" --column="子网掩码" --column="默认网关" --column="DNS" \ | |
| $(read_conf "$uuid") \ | |
| "d" "删除此网络配置"|\ | |
| tr -d " "|tr "|" " " | |
| ) | |
| if [[ -n "$conf" ]]; then | |
| case "$conf" in | |
| "d "*) | |
| del_conf "$uuid" | |
| ;; | |
| *) | |
| edit_conf $conf | |
| ;; | |
| esac | |
| fi | |
| fi | |
| } | |
| new_network(){ | |
| local conf=$(zenity --forms --title="新建网络配置" \ | |
| --text="以下各栏均不能留空,单击确定保存\n如无 DNS 填 0,多个 DNS 以英文分号分割" \ | |
| --add-entry="名称" --add-entry="IP地址" --add-entry="子网掩码" --add-entry="默认网关" --add-entry="DNS"|\ | |
| tr -d " "|tr "|" " " | |
| ) | |
| local conf_name=$(echo $conf|awk '{printf $1}') | |
| local conf_ip=$(echo $conf|awk '{printf $2}') | |
| if [[ -n "$conf" ]]; then | |
| echo $(cat /proc/sys/kernel/random/uuid) $conf >> "$CONF_FILE" && \ | |
| zenity --notification --window-icon="info" \ | |
| --text="\n已添加网络配置: $conf_name ( $conf_ip )" | |
| fi | |
| } | |
| mask_to_prefix(){ | |
| local num=0 | |
| for n in ${1//./ };do | |
| if (( $n < 128 )) && (( $n != 0 ));then | |
| exit 1 | |
| elif [[ $(echo "obase=2;$n"|bc) =~ ^(1*)0*$ ]];then | |
| if (( $(( $num % 8 )) == 0 ));then | |
| num=$(( $num + ${#BASH_REMATCH[1]} )) | |
| elif (( ${#BASH_REMATCH[1]} > 0 ));then | |
| exit 1 | |
| fi | |
| else | |
| exit 1 | |
| fi | |
| done | |
| echo $num | |
| } | |
| switch_network(){ | |
| local conf_uuid=$(select_network) | |
| local netcard=$(read_conf "netcard") | |
| if [[ -n "$conf_uuid" ]] && [[ -z "$netcard" ]]; then | |
| set_netcard | |
| netcard=$(read_conf "netcard") | |
| fi | |
| if [[ -n "$conf_uuid" ]] && [[ -n "$netcard" ]]; then | |
| local conf=$(read_conf $conf_uuid) | |
| local conf_ip=$(echo $conf|awk '{printf $3}') | |
| local conf_mask=$(echo $conf|awk '{printf $4}') | |
| local conf_prefix=$(mask_to_prefix $conf_mask) | |
| local conf_gateway=$(echo $conf|awk '{printf $5}') | |
| local conf_dns=$(echo $conf|awk '{printf $6}'|tr ";" " ") | |
| [[ "$conf_dns" =~ ^([0-9]+\.){3}[0-9]+ ]] || conf_dns="" | |
| local con_uuid=$(nmcli con show|grep -P "$netcard\s*$"|awk '{printf $2}') | |
| nmcli con mod "$con_uuid" ipv4.method "manual" | |
| nmcli con mod "$con_uuid" ipv4.address "$conf_ip/$conf_prefix" | |
| nmcli con mod "$con_uuid" ipv4.gateway "$conf_gateway" | |
| nmcli con mod "$con_uuid" ipv4.dns "$conf_dns" | |
| sh -c "nmcli con down $con_uuid && nmcli con up $con_uuid" && \ | |
| zenity --info --title="网络配置已切换" \ | |
| --text="\nIP地址:\t$conf_ip\n掩码:\t$conf_mask\n网关:\t$conf_gateway\nDNS:\t${conf_dns:=无}" | |
| exit 0 | |
| fi | |
| } | |
| set_netcard(){ | |
| local current=$(read_conf "netcard") | |
| local netcard=$(zenity --list --height=300 --hide-header --title="请选择默认网卡" \ | |
| --text="当前默认网卡:$current" \ | |
| --column="网卡" \ | |
| $(ls /sys/class/net) \ | |
| ) | |
| if [[ -n "$netcard" ]]; then | |
| if [[ $(cat "$CONF_FILE"|grep $RESERVED_UUID) ]]; then | |
| sed -i "s/^\($RESERVED_UUID\).*$/\1\ $netcard/g" "$CONF_FILE" | |
| else | |
| echo "$RESERVED_UUID $netcard" >> "$CONF_FILE" | |
| fi | |
| zenity --notification --window-icon="info" \ | |
| --text="\n已设置默认网卡为: $netcard" | |
| fi | |
| } | |
| select_operation(){ | |
| local operation=$(zenity --list --height=250 \ | |
| --title="请选择操作" \ | |
| --text="选中并单击确定,或直接双击" \ | |
| --hide-header --hide-column="1" \ | |
| --column="序号" --column="操作" \ | |
| "1" "切换网络配置" \ | |
| "2" "编辑网络设置" \ | |
| "3" "新建网络配置" \ | |
| "4" "设置默认网卡" \ | |
| ) | |
| case "$operation" in | |
| "1") | |
| switch_network | |
| ;; | |
| "2") | |
| edit_network | |
| ;; | |
| "3") | |
| new_network | |
| ;; | |
| "4") | |
| set_netcard | |
| ;; | |
| *) | |
| exit 0 | |
| ;; | |
| esac | |
| select_operation | |
| } | |
| select_operation |
Author
nanpuyue
commented
Sep 17, 2017



Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment