def ip(self, interface_name, newip):
"""
This function automatically will change netmask address.
Checking interface first, if interface name found in Get().interfaces()
validating Ipv4. After that applied ip address to interface
interface_name: Applied Interface
newip: New Ip Address
"""
interface_check = Get().interfaces
valid_ipv4 = validators.ipv4(newip)
if not interface_name in interface_check:
raise WrongInterfaceName("Wrong Interface Name %s" % interface_name)
elif not valid_ipv4 is True:
raise NotValidIPv4Address("Not Valid IPv4 Address %s" % newip)
else:
ifname = interface_name.encode(encoding='UTF-8')
ipbytes = socket.inet_aton(newip)
ifreq = struct.pack('16sH2s4s8s', ifname, AF_INET, b'\x00'*2, ipbytes, b'\x00'*8)
fcntl.ioctl(self.sock, SIOCSIFADDR, ifreq)
评论列表
文章目录