0
点赞
收藏
分享

微信扫一扫

shell 根据网卡名字自动生成网卡文件,然后up。

#!/bin/bash

# 获取所有网卡名称
interface_list=$(ip a | awk '/^[0-9]+: / {gsub(":", ""); print $2}' | grep -v  lo | grep -v docker)

network_scripts_dir="/etc/sysconfig/network-scripts/"

for interface in $interface_list; do
  config_file="ifcfg-${interface}"
  config_file_path="${network_scripts_dir}${config_file}"

  # 检查是否存在配置文件
  if [ ! -f "$config_file_path" ]; then
    echo "生成配置文件: $config_file_path"

    # 创建配置文件
    touch "$config_file_path"

    # 将基本配置写入文件
    echo "DEVICE=${interface}" > "$config_file_path"
    echo "BOOTPROTO=dhcp" >> "$config_file_path"
    echo "ONBOOT=yes" >> "$config_file_path"
    echo "TYPE=Ethernet" >> "$config_file_path"
  fi
done

for interface in $interface_list; do
  # 检查网卡状态(UP 或 DOWN)
  interface_status=$(ip link show $interface | awk '/<.*>/ {print $9}')

  if [ "$interface_status" == "DOWN" ]; then
    echo "激活网卡:$interface"
    # 用root权限激活网卡
    ifup $interface  &
  else
    echo "网卡已激活:$interface"
  fi
done

wait

举报

相关推荐

0 条评论