centos配置子接口脚本,用函数写
时间: 2023-12-14 10:38:25 浏览: 23
以下是一个用函数编写的CentOS配置子接口脚本示例:
```bash
#!/bin/bash
# 定义函数创建子接口
create_subinterface() {
local interface=$1
local subinterface=$2
local ipaddress=$3
# 创建虚拟接口
ip link add link $interface name $subinterface type vlan id $subinterface
# 配置IP地址
ip addr add $ipaddress dev $subinterface
# 启用接口
ip link set dev $subinterface up
echo "子接口 $subinterface 创建成功,IP地址为 $ipaddress"
}
# 调用函数创建子接口
create_subinterface eth0 100 192.168.1.100
```
在这个脚本中,我们定义了一个名为 `create_subinterface` 的函数。该函数接受三个参数:主接口名、子接口名和IP地址。函数的主要功能是创建一个子接口,并为其配置IP地址。调用该函数的方式是提供所需的参数,例如 `create_subinterface eth0 100 192.168.1.100`。执行脚本后,将会创建一个名为 `eth0.100` 的子接口,IP地址为 `192.168.1.100`。