I am writing a python Script to view "netstat" command output periodically and save it to a file. In case any port changes are there between different outputs of netstat.Print those lines to another file and save.
Sample output of netstat command:
tcp 0 77 100.73.96.7:56855 31.13.79.246:https LISTEN
tcp 0 32 100.73.96.7:46551 68.232.44.121:https LISTEN
tcp 0 1 100.73.96.7:60538 198.252.206.16:http LISTEN
tcp 0 77 100.73.96.7:51728 103.31.6.32:https LISTEN
my script is like this: I am able to print the netstat command periodical to a file.
import subprocess
import time,threading
def myfun():
p = subprocess.Popen(["netstat", "-at"], stdout=subprocess.PIPE)
out = p.stdout.read()
print out
myfile = open("myfile","a")
myfile.write(out)
myfile.close()
print(time.ctime())
threading.Timer(10,myfun).start()
myfun()
How to proceed further. Anybody help