Skip to content

Instantly share code, notes, and snippets.

@BOAScripts
Created November 29, 2022 12:24
Show Gist options
  • Select an option

  • Save BOAScripts/23fd103ae4d64be68331067e05bf0247 to your computer and use it in GitHub Desktop.

Select an option

Save BOAScripts/23fd103ae4d64be68331067e05bf0247 to your computer and use it in GitHub Desktop.
[py] List local NIC/IPs - subprocess, ipconfig
# IMPORT
import subprocess
# DEF
def List_LocalIPs():
ipsList = [] #{"adapterName", "adapterIp"}
result=subprocess.run('ipconfig',stdout=subprocess.PIPE,text=True).stdout.lower()
for l in result.split('\n'):
if 'ethernet' in l or 'wireless' in l:
adapterName = l.replace(':','')
if 'ipv4' in l:
adapterIP = l.split(':')[1].strip()
ipsList.append([adapterName,adapterIP])
print(f"NIC : {adapterName} on {adapterIP}")
return ipsList
# EXEC
localIPs = List_LocalIPs()
print(localIPs)
@BOAScripts

BOAScripts commented Nov 29, 2022 •

Copy link
Copy Markdown
Author

Print only NICs that have a default gateway

# IMPORT
import subprocess
# DEF
def List_LocalIPs():
    ipsList = [] #{"adapterName", "adapterIp","DefaultGateway"}
    result=subprocess.run('ipconfig',stdout=subprocess.PIPE,text=True).stdout.lower()
    
    for l in result.split('\n'):
        if 'ethernet' in l or 'wireless' in l:
            adapterName = l.replace(':','')
        if 'ipv4' in l:
            adapterIP = l.split(':')[1].strip()
        if 'default gateway' in l and (l.split(':')[1].strip()):
            adapterDefGtw = l.split(':')[1].strip()
            ipsList.append([adapterName,adapterIP,adapterDefGtw])
            print(f"NIC : {adapterName} on {adapterIP}. Default Gateway is:{adapterDefGtw}")

    return ipsList
# EXEC
localIPs = List_LocalIPs()
print(localIPs)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment