在模块sh中使用变量

时间:2013-12-17 18:35:11

标签: python

我正在编写一个脚本来显示服务器网卡的所有内容,但我有一个问题。

如何在Python中使用sh模块正确使用变量?

我的代码示例:

allinterfaces = os.popen('/sbin/ifconfig -s |awk {\'print $1\'} |grep -v Iface')#.read().strip()

for interface in allinterfaces:
   print interface
   print sh.grep(sh.ifconfig('eth0'), "-oP", "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}")

我的问题是:如何为变量更改'eth0'?

1 个答案:

答案 0 :(得分:1)

import socket
print([ip for ip in socket.gethostbyname_ex(socket.gethostname())[2] if not ip.startswith("127.")][:1])