Python program to execute network basic commands

Program:

import subprocess

def ping():
    host = input("Enter Host: ")
    packet = int(input("\nEnter Packet: "))
    print("\n")
    ping = subprocess.getoutput(f"ping -w {packet} {host}")
    print(ping)


def tracert():
    host = input("Enter Host: ")
    print("\n")
    trace = subprocess.getoutput(f"tracert {host}")
    print(trace)

def ipconfig():
    ip = subprocess.getoutput(f"ipconfig")
    print(ip)

def netstat():
    net = subprocess.getoutput(f"netstat")
    print(net)

def nslookup():
    host = input("Enter Host: ")
    print("\n")
    ns = subprocess.getoutput(f"nslookup {host}")
    print(ns)
    
ans=True
while ans:
    print("""1. Ping
2. Tracert
3. ipconfig
4. netstat
5. nslookup
6. Exit""")

    ans=input("What would you like to do? ")
    if ans=="1": 
        ping() 
    elif ans=="2":
        tracert()
    elif ans=="3":
        ipconfig()
    elif ans=="4":
        netstat()
    elif ans=="5":
        nslookup()
    elif ans=="6":
        print("Thank You")
        ans = None
    elif ans !="":
        print("\n Not Valid Choice Try again") 

Leave a comment