在 bash 中检测网络设备的名称?

Bra*_*don 12 bash

我试图鼓起一个黑客,将通过 bash 输出一个人的计算机上的活动网络设备的名称。我正在为工作而努力。我该怎么做?我不想简单地使用“wlan0”或“eth0”或任何通用废话,因为某些操作系统使用不同的名称(例如 pfSense)。

ste*_*ver 37

It depends what you mean by 'active' - if you just want to see the names of all the network devices on the system, you can look at the contents of the /sys/class/net directory e.g.

$ ls /sys/class/net
eth0  lo  wlan0
Run Code Online (Sandbox Code Playgroud)

To see the status, you could use the ip command on any link objects - you can parse the output to get the particular fields you want e.g. to see just the device name and state

$ ip -o link show | awk '{print $2,$9}'
lo: UNKNOWN
eth0: DOWN
wlan0: UP
Run Code Online (Sandbox Code Playgroud)

If you are running a modern desktop version of Ubuntu (with interfaces managed by the network-manager service), then you should be able to get a similar device status list using nmcli

$ nmcli dev status
DEVICE     TYPE              STATE
wlan0      802-11-wireless   connected
eth0       802-3-ethernet    unavailable
Run Code Online (Sandbox Code Playgroud)

or, to limit the output to particular fields in a way that's more easily used in a script

$ nmcli --terse --fields DEVICE,STATE dev status
wlan0:connected
eth0:unavailable
Run Code Online (Sandbox Code Playgroud)

If you are using network-manager, you could also access device and connection properties via DBUS - see for example Dbus Tutorial - Fun with Network Manager


cha*_*aos 3

您可以用来ifconfig检测活动的网络设备,以供较小的输出使用ifconfig -sifconfig打印活动接口,-a您可以打印系统识别为网络接口的所有接口。

或者使用ip addr.


Mik*_*rth 0

我可能不直接知道答案,但我会给您一个网络命令列表,也许从那里您可以尝试将其与您需要执行的操作连接起来。

要查看网络并输出单台计算机上活动网络设备的名称,您可以尝试以下命令:

ifconfig
Run Code Online (Sandbox Code Playgroud)

该命令允许我们检测活动的网络设备。您还可以使用ifconfig -s命令来减少输出使用。您可以使用此命令来配置特定接口

ip addr
Run Code Online (Sandbox Code Playgroud)

该命令可以让我们查看网络网络信息。

tcpdump
Run Code Online (Sandbox Code Playgroud)

该命令是一个网络嗅探器。它的作用是从网络接口捕获数据包并为您解释它们。

如果您收到权限被拒绝的消息,只需:**gksu yourcommand**让它获得 root 权限并强制执行该命令。

findsmb
Run Code Online (Sandbox Code Playgroud)

您可以使用此命令列出有关响应 SMB 名称查询的计算机的信息。

来源: http: //www.gymforgeeks.com/topic/358445-/