python获取设备主机名和IP地址

时间:2021-04-02 13:33:09
import socket


def print_machine_info():
    host_name = socket.gethostname()
    ip_address = socket.gethostbyname(host_name)
    print("Host name: %s" % host_name)
    print("IP address: %s" % ip_address)
    hostname = socket.gethostbyaddr('192.168.201.227')
    print(hostname)


def print_device_remote_info():
      host_name = 'swissmic.cn'
      print(" Host name: %s" % host_name)
      print(" IP address: %s" % socket.gethostbyname(host_name))