获取服务器内网地址

 1 def get_server_inner_ip(outer_ip, password, port):
 2     dest_client = paramiko.SSHClient()
 3     dest_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
 4     dest_client.connect(hostname=outer_ip, port=port, username='root', password=password)
 5     grep_v_string = "grep -v 127.0.0.1"
 6 
 7     while True:
 8         get_inner_ip_cmd = "ifconfig | grep inet | %s | head -1 | awk '{print $2}' | awk -F: '{print $2}'" % grep_v_string
 9         stdin, stdout, stderr = dest_client.exec_command(get_inner_ip_cmd, get_pty=True)
10         inner_ip = str(stdout.read().decode('utf8').split('\n')[0])
11 
12         if inner_ip == '':
13             return False
14 
15         if not inner_ip.startswith('192.168') and not inner_ip.startswith('10.'):
16             grep_v_string += " | grep -v %s" % inner_ip
17             continue
18 
19         temp_list = inner_ip.split('.')
20         if not int(temp_list[0]) == 10 and not 0 <= int(temp_list[1]) <= 255:
21             grep_v_string += " | grep -v %s" % inner_ip
22             continue
23 
24         break
25 
26     dest_client.close()
27 
28     return inner_ip

猜你喜欢

转载自www.cnblogs.com/t-road/p/12797096.html