尝试使用 python fabric 获取主机的公共(public) ip 地址
def publicip():
ip = local("curl -s 'http://checkip.dyndns.org' | sed 's/.*Current IP Address: \([0-9\.]*\).*/\'\1/g\'")
print (red(ip))
错误:
Fatal error: local() encountered an error (return code 2) while executing 'curl -s 'http://checkip.dyndns.org' | sed 's/.*Current IP Address: \([0-9\.]*\).*/'/g''
请您参考如下方法:
您运行的主机上可能没有安装 Curl。无论如何你都不需要它,因为你可以像这样在 Python 中轻松地做到这一点:
import urllib2
u = urllib2.urlopen('http://checkip.dyndns.org')
line = u.next()
print line.split("<")[6].split().pop()
