获取服务器内网地址

# A类:10.0.0.0 - 10.255.255.255
# B类:172.16.0.0 - 172.31.255.255
# C类:192.168.0.0 - 192.168.255.255
def get_server_inner_ip(outer_ip, password, port):
    dest_client = paramiko.SSHClient()
    dest_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    dest_client.connect(hostname=outer_ip, port=port, username='root', password=password)
    grep_v_string = "grep -v 127.0.0.1"

    while True:
        get_inner_ip_cmd = "ifconfig | grep inet | %s | head -1 | awk '{print $2}' | awk -F: '{print $2}'" % grep_v_string
        stdin, stdout, stderr = dest_client.exec_command(get_inner_ip_cmd, get_pty=True)
        inner_ip = str(stdout.read().decode('utf8').split('
')[0])

        if inner_ip == '':
            return False

        temp_list = inner_ip.split('.')
        if not inner_ip.startswith('192.168.') and 
                not inner_ip.startswith('10.') and 
                (not int(temp_list[0]) == 172 and not 16 <= int(temp_list[1]) <= 31):
            grep_v_string += " | grep -v %s" % inner_ip
            continue

        break

    dest_client.close()

    return inner_ip
原文地址:https://www.cnblogs.com/t-road/p/12797096.html