def get_ip_address(self, interface):
# Not a simple subprocess.call due to the need for a PIPE in the command
# https://docs.python.org/2/library/subprocess.html#replacing-shell-pipeline
command_line_1 = "ip addr show " + interface
args1 = shlex.split(command_line_1)
command_line_2 = "grep -Po 'inet \K[\d.]+'"
args2 = shlex.split(command_line_2)
command_line_1 = subprocess.Popen(args1, stdout=subprocess.PIPE)
command_line_2 = subprocess.Popen(args2, stdin=command_line_1.stdout, stdout=subprocess.PIPE)
command_line_1.stdout.close()
ip_address = command_line_2.communicate()[0]
ip_address = ip_address.rstrip()
if ip_address == '':
ip_address = 'NOT CONNECTED'
return str(ip_address)
评论列表
文章目录