全球主机交流论坛

标题: 如何用python 取得小鸡的网卡名字? [打印本页]

作者: cbacn    时间: 2019-5-17 10:44
提示: 作者被禁止或删除 内容自动屏蔽
作者: 平僧洗头用飘柔    时间: 2019-5-17 10:46
用psutil模块。

字数补丁
作者: 平僧洗头用飘柔    时间: 2019-5-17 10:51

自己修改下



  1. #coding=utf-8
  2. import psutil
  3. #获取网卡名称和其ip地址,不包括回环
  4. def get_netcard():
  5.     netcard_info = []
  6.     info = psutil.net_if_addrs()
  7.     for k,v in info.items():
  8.         for item in v:
  9.             if item[0] == 2 and not item[1]=='127.0.0.1':
  10.                 netcard_info.append((k,item[1]))
  11.     return netcard_info
  12. if __name__ == '__main__':
  13.     print get_netcard()
复制代码

作者: 天城    时间: 2019-5-17 10:54
现成模块啊
作者: aRNoLD    时间: 2019-5-17 11:00
ifconfig 之后用grep过滤一批纵向的,再用cut命令过滤一次,这是横向的,一般能达到目的。

比如我这么取过IP地址

  1. ip -f inet addr | grep inet | grep -v 127.0.0.1 | sed "s/^\s*//g" | cut -d ' ' -f 2 | cut -d '/' -f 1
复制代码

作者: wenguonideshou    时间: 2019-5-17 11:29
你这是要  做运维开发面板?
快把问题问完了
作者: fatal    时间: 2019-5-17 13:35
aRNoLD 发表于 2019-5-17 11:00
ifconfig 之后用grep过滤一批纵向的,再用cut命令过滤一次,这是横向的,一般能达到目的。

比如我这么取过 ...

还是shell方便,字数补丁
作者: wenguonideshou    时间: 2019-5-17 13:45
ansible的facts
https://ansible-tran.readthedocs.io/en/latest/docs/playbooks_variables.html




欢迎光临 全球主机交流论坛 (https://lilynana.eu.org/) Powered by Discuz! X3.4