def get_interface_ip_addresses(interfaceName): """ <Purpose> Returns the IP address associated with the interface. <Arguments> interfaceName: The string name of the interface, e.g. eth0 <Returns> A list of IP addresses associated with the interface. """ # Launch up a shell, get the feed back # We use ifconfig with the interface name. ifconfig_process = portable_popen.Popen( ["/sbin/ifconfig", interfaceName.strip()]) ifconfig_output, _ = ifconfig_process.communicate() ifconfig_lines = textops.textops_rawtexttolines(ifconfig_output) # Look for ipv4 addresses target_lines = textops.textops_grep("inet", ifconfig_lines) # and not ipv6 target_lines = textops.textops_grep("inet6", target_lines, exclude=True) # Only take the ip(s) target_lines = textops.textops_cut(target_lines, delimiter=":", fields=[1]) target_lines = textops.textops_cut(target_lines, delimiter=" ", fields=[0]) # Create an array for the ip's ipaddressList = [] for line in target_lines: # Strip the newline and any spacing line = line.strip("\n\t ") ipaddressList.append(line) # Done, return the interfaces return ipaddressList
def get_interface_ip_addresses(interfaceName): """ <Purpose> Returns the IP address associated with the interface. <Arguments> interfaceName: The string name of the interface, e.g. eth0 <Returns> A list of IP addresses associated with the interface. """ # Launch up a shell, get the feed back # We use ifconfig with the interface name. ifconfig_process = portable_popen.Popen(["/sbin/ifconfig", interfaceName.strip()]) ifconfig_output, _ = ifconfig_process.communicate() ifconfig_lines = textops.textops_rawtexttolines(ifconfig_output) # Look for ipv4 addresses target_lines = textops.textops_grep("inet", ifconfig_lines) # and not ipv6 target_lines = textops.textops_grep("inet6", target_lines, exclude=True) # Only take the ip(s) target_lines = textops.textops_cut(target_lines, delimiter=":", fields=[1]) target_lines = textops.textops_cut(target_lines, delimiter=" ", fields=[0]) # Create an array for the ip's ipaddressList = [] for line in target_lines: # Strip the newline and any spacing line = line.strip("\n\t ") ipaddressList.append(line) # Done, return the interfaces return ipaddressList
def get_available_interfaces(): """ <Purpose> Returns a list of available network interfaces. <Returns> An array of string interfaces """ # Common headers # This list contains common header elements so that they can be stripped common_headers_list = ["Name", "Kernel", "Iface"] # Netstat will return all interfaces, but also has some duplication. #If Netstat is not on machine then secondary command IP will be used as substitute # Cut will get the first field from each line, which is the interface name. # Sort prepares the input for uniq, which only works on sorted lists. # Uniq, is somewhat obvious, it will only return the unique interfaces to remove duplicates. # Launch up a shell, get the feedback network_status_process = None #netstat process if(network_status_process == None): try: network_status_process = portable_popen.Popen(["netstat", "-i"]) netstat_stdout, _ = network_status_process.communicate() netstat_lines = textops.textops_rawtexttolines(netstat_stdout) target_lines = textops.textops_cut(netstat_lines, delimiter=" ", fields=[0]) unique_lines = set(target_lines) # Create an array for the interfaces interfaces_list = [] for line in unique_lines: # Strip the newline line = line.strip("\n") # Check if this is a header if line in common_headers_list: continue interfaces_list.append(line) # Done, return the interfaces return interfaces_list except Exception, e: pass
def get_available_interfaces(): """ <Purpose> Returns a list of available network interfaces. <Returns> An array of string interfaces """ # Common headers # This list contains common header elements so that they can be stripped common_headers_list = ["Name", "Kernel", "Iface"] # Netstat will return all interfaces, but also has some duplication. # Cut will get the first field from each line, which is the interface name. # Sort prepares the input for uniq, which only works on sorted lists. # Uniq, is somewhat obvious, it will only return the unique interfaces to remove duplicates. # Launch up a shell, get the feedback netstat_process = portable_popen.Popen(["netstat", "-i"]) netstat_stdout, _ = netstat_process.communicate() netstat_lines = textops.textops_rawtexttolines(netstat_stdout) target_lines = textops.textops_cut(netstat_lines, delimiter=" ", fields=[0]) unique_lines = set(target_lines) # Create an array for the interfaces interfaces_list = [] for line in unique_lines: # Strip the newline line = line.strip("\n") # Check if this is a header if line in common_headers_list: continue interfaces_list.append(line) # Done, return the interfaces return interfaces_list
continue interfaces_list.append(line) # Done, return the interfaces return interfaces_list except Exception, e: pass #ip process if(network_status_process == None): try: network_status_process = portable_popen.Popen(["ip", "addr"]) ip_stdout, _ = network_status_process.communicate() ip_lines = textops.textops_rawtexttolines(ip_stdout) target_lines = textops.textops_cut(ip_lines, delimiter=" ", fields=[1]) unique_lines = set(target_lines) # Create an array for the interfaces interfaces_list = [] for line in unique_lines: # Strip the newline line = line.strip("\n") # Check if this is a header if line in common_headers_list: continue if line == '': #for the excess white space charecters that ip has continue interfaces_list.append(line[0:-1])