Shell 获取网卡名称及对应ip脚本

以下是一个使用 ifconfig 获取网卡名及对应 IP 的简单 Shell 脚本:

#!/bin/bash

echo "网卡名和对应 IP:" > output.txt

# 使用 ifconfig 命令获取网卡信息,并使用 awk 进行处理
ifconfig | awk '/^[a-zA-Z0-9]+/{interface=$1; next} /inet /{print interface ": " $2}' >> output.txt

echo "结果已保存到 output.txt 文件中。"

这个脚本首先运行 ifconfig 命令获取系统的网络配置信息,然后使用 awk 提取网卡名和对应 IP 地址,并最终输出。请注意,该脚本可能需要在具有适当权限的系统上运行。

posted @ 2024-03-04 20:29  Lz_蚂蚱  阅读(592)  评论(0)    收藏  举报