shell脚本实现读取配置文件添加指定网卡到指定端口并添加注释
时间: 2024-01-29 15:03:56 浏览: 75
好的,下面是一个示例的 shell 脚本实现读取配置文件添加指定网卡到指定端口并添加注释:
```bash
#!/bin/bash
# 读取配置文件
source ./config
# 添加网卡到指定端口并添加注释
sudo nmcli con add con-name "${CON_NAME}" ifname "${IF_NAME}" type ethernet
sudo nmcli con modify "${CON_NAME}" connection.autoconnect yes
sudo nmcli con modify "${CON_NAME}" ipv4.method manual ipv4.addresses "${IP_ADDR}/${SUB_MASK}" ipv4.gateway "${GATEWAY}"
sudo nmcli con modify "${CON_NAME}" +ipv4.dns "${DNS1}" "${DNS2}"
sudo nmcli con modify "${CON_NAME}" +ipv4.routes "${ROUTE}"
sudo nmcli con modify "${CON_NAME}" +ipv4.ignore-auto-dns yes
sudo nmcli con modify "${CON_NAME}" +ipv4.ignore-auto-routes yes
sudo nmcli con modify "${CON_NAME}" +ipv4.dns-search "${DNS_SEARCH}"
sudo nmcli con modify "${CON_NAME}" +ipv6.method ignore
sudo nmcli con modify "${CON_NAME}" +ipv6.privacy off
sudo nmcli con modify "${CON_NAME}" +ipv6.dns-search "${DNS_SEARCH}"
sudo nmcli con modify "${CON_NAME}" +ipv6.ignore-auto-dns yes
sudo nmcli con modify "${CON_NAME}" +ipv6.ignore-auto-routes yes
sudo nmcli con modify "${CON_NAME}" +ipv6.dns "${DNS1}" "${DNS2}"
sudo nmcli con modify "${CON_NAME}" +ipv6.routes "${ROUTE}"
sudo nmcli con modify "${CON_NAME}" +ipv6.route-metric "${ROUTE_METRIC}"
sudo nmcli con modify "${CON_NAME}" +ipv6.addr-gen-mode eui64
# 添加注释
sudo nmcli con modify "${CON_NAME}" connection.id "${CON_NAME} (${IF_NAME})"
```
其中,`config` 文件如下:
```bash
CON_NAME="my_connection"
IF_NAME="eth0"
IP_ADDR="192.168.1.100"
SUB_MASK="24"
GATEWAY="192.168.1.1"
DNS1="8.8.8.8"
DNS2="8.8.4.4"
DNS_SEARCH="example.com"
ROUTE="to 192.168.2.0/24 via 192.168.1.2"
ROUTE_METRIC="100"
```
你可以根据自己的需要修改 `config` 文件中的参数。
阅读全文