Recently, I needed to get the Ip, Netmask and Gateway of the TS-7400 using python, and I had somehow issues to get this info. So , this post is more a reminder to myself with a collection of 3 useful functions. Maybe, that can be useful for someone else too.
import os
import socket
import fcntl
import struct
SIOCGIFNETMASK = 0x891b
def get_network_mask(ifname):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
netmask = fcntl.ioctl(s, SIOCGIFNETMASK, struct.pack('256s', ifname))[20:24]
return socket.inet_ntoa(netmask)
def get_ip_address(ifname):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
return socket.inet_ntoa(fcntl.ioctl(
s.fileno(),
0x8915, # SIOCGIFADDR
struct.pack('256s', ifname[:15])
)[20:24])
def get_gateway(ifname):
cmd = "ip route list dev "+ ifname + " | awk ' /^default/ {print $3}'"
fin,fout = os.popen4(cmd)
result = fout.read()
return result












