首页 > 代码库 > python网络编程学习2016/11/18

python网络编程学习2016/11/18

 1.打印本机设备名以及IPv4地址

#!/usr/bin/python2.7

# -*- coding=utf-8 -*-
import socket

 

def print_machine_info():

host_name = socket.gethostname()                       #本机设备名

  ip_address = socket.gethostbyname(host_name)   #设备名对应的ip 地址

print "%s \‘s ip is %s" % (host_name,ip_address)  #打印

 

if __name__ == ‘__main__‘:

print_machine_info() 

 

2.获取远程设备的IP地址信息

#!/usr/bin/python2.7

import socket

 
def print_machine_info():
remote_hostname = ‘www.baidu.com‘
  try:
print "%s \‘s ip is %s" % (remote_hostname,socket.gethostbyname(remote_hostname))
  except socket.error, err_msg:
  print "%s: %s" %(remote_hostname, err_msg)

 

if __name__ == ‘__main__‘:
print_machine_info() 

 

 

 

 

 

3.计算cisco无线AP需要dhcp的option43 

#/usr/bin/python2.7
# -*- coding=utf-8 -*-
import socket
from binascii import hexlify
import sys
def wlc_option43_hex():

 

  wlc_number = raw_input(‘Enter the wlc number:‘)
  if wlc_number == ‘1‘:
  put_number = raw_input(‘Enter the wlc ip:‘)
for ip_addr in [put_number]:
wlc_ip_addr = socket.inet_aton(ip_addr)
unpacked_ip_addr = socket.inet_ntoa(wlc_ip_addr)
hex_prefix = ‘f104‘
print "IP Address: %s => option43 hex %s"\
%(unpacked_ip_addr,str(hex_prefix)+hexlify(wlc_ip_addr)) 
else:
while True:
put_number = raw_input(‘Enter the wlc ip:‘)
if put_number!= ‘q‘:
for ip_addr in [put_number]:
wlc_ip_addr  = socket.inet_aton(ip_addr)
unpacked_ip_addr = socket.inet_ntoa(wlc_ip_addr)
hex_prefix = ‘f108‘
print "IP Address: %s => option43 hex %s"\
%(unpacked_ip_addr,str(hex_prefix)+hexlify(wlc_ip_addr)) 
else:
sys.exit()
if __name__ == ‘__main__‘:
wlc_option43_hex()

 

效果:

当无线控制器的数量为1时: 

Enter the wlc number:1

Enter the wlc ip:10.10.9.252

IP Address: 10.10.9.252 => option43 hex f1040a0a09fc

 

当无线控制器的数量为2时:

Enter the wlc number:2

Enter the wlc ip:10.10.8.252

IP Address: 10.10.8.252 => option43 hex f1080a0a08fc

Enter the wlc ip:10.10.123.252

IP Address: 10.10.123.252 => option43 hex f1080a0a7bfc

Enter the wlc ip:q

 

python网络编程学习2016/11/18