Jan-31-2018, 07:09 PM
Hi All,
First time post and first python script ever! :)
I am trying to create a python script that will have one argument passed to it:
python IPToHostName.py 1.1.1.1
I would like to get the host name of the ip address passed to that script and write that to a log file named the data and time the script was run but I am having issues.
First time post and first python script ever! :)
I am trying to create a python script that will have one argument passed to it:
python IPToHostName.py 1.1.1.1
I would like to get the host name of the ip address passed to that script and write that to a log file named the data and time the script was run but I am having issues.
import sys import socket import time print(sys.argv[1]) #Debug n = time.strftime("%Y%m%d-%H%M%S") print(n) #Debug logfile = "C:/Stuff/Scripts/Python/%s.txt" % n IP = sys.argv[1] #Set the varible named "IP" = the sourceip from QRadar print(IP) print(logfile) with open(logfile, 'a') as f: try: host = socket.gethostbyaddr(IP.rstrip()) hostname=host[0] print >> f, '', IP.rstrip(),",",hostname except Exception as e: print >> f, '', IP.rstrip(),",NULL"