且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Debian系统上如何以编程方式获取IP地址?

更新时间:2023-02-12 18:12:59

请参见netdevice,通过 man netdevice 在网络上

然后可以使用SIOCGIFCONF获取枚举所有传输层地址。

See "netdevice", through man netdevice or on the web.
SIOCGIFCONF can then be used to get an enumeration of all transport layer addresses.

编辑(使用联机帮助页): man 在Linux上是非常有用的命令(或其他类UNIX系统)。它显示了大多数命令,库函数,程序等的简要说明。打开shell提示符并键入 man ls man netdevice ,你会看到我的意思。

Edit (on manpages): man is a very useful command on Linux (or other UNIX-like systems as well). It shows a brief description of most commands, library functions, programs, etc. Open a shell prompt and type man ls or man netdevice, and you'll see what I mean.

编辑(一般检索IP):最简单的方法,如果你认为C的方式太凌乱,是一个简单的shell脚本(只是从我的头顶部):

ifconfig | grep'inet addr'| awk'{print $ 2}'| sed's / addr:// g'

Edit (on general retrieving of IP): The easiest way, if you think the C way is too messy, is a simple shell script like (just from the top of my head):
ifconfig|grep 'inet addr'|awk '{print $2}'|sed 's/addr://g'

编辑(在大脑解决方案中):他所做的是使用 if_nameindex()用于查找所有网络设备名称的功能,然后将SIOCFIFCONF ioctl中的每个名称用于找到他们的IP。正如他所说,它每个设备只列出一个IP。

Edit (on the Brain solution): What he does is using the if_nameindex() function for finding all network device names, and then the SIOCFIFCONF ioctl on each of these names for finding their IP. As he says, it only lists one IP per device.