'''This program runs commands to document the computer and internet settings '''

import sys
import subprocess
import datetime
import time
from time import gmtime, strftime


##Functions

def myLocalTime():
    x = datetime.datetime.now() # Current Time and Date
    x = str(x)
    seconds = time.time() #Seconds since January 1st 1970
    local_time = time.ctime(seconds)
    local_time = str(local_time)
    return local_time

def runWindowsCMD(yourStringCMD):

    f.write("\nRunning CMD: " + yourStringCMD) 
    print("\nRunning CMD: " + yourStringCMD)
    subprocess1 = subprocess.Popen(yourStringCMD, shell=True, stdout=subprocess.PIPE)
    cmd_return = subprocess1.stdout.read()
    cmd_return = str(cmd_return)
    cmd_return = cmd_return.replace("\\r","")
    cmd_return = cmd_return.replace("n'","")
    cmd_return = cmd_return.replace("b'","")
    cmd_return = cmd_return.replace("\\n","\n")
    cmd_return = cmd_return.replace("\\"," ")

    f.write("\n" + cmd_return + "\n")
    print("\n" + cmd_return + "\n") 
    
    #end runWindowsCMD

now = datetime.datetime.now()
year = str(now.year)
month = str(now.month)
day = str(now.day)

myFileName = "System Log V4 -- " + month + "-" + day + "-" + year + ".txt"
f = open(myFileName,"a")  
f.write("Script by Lorn MacIlravie -- www.lornmac.com --- lorn@lornmac.com -- 605-603-6849")

print("\nStart of Script\n")
f.write("\nStart of Script\n")
print(myLocalTime())
f.write(myLocalTime())
print("\n")
f.write("\n")

print('This script will run the following commands: hostname, systeminfo, inpconfig /all, ping (the IP address that you enter below), ping 127.0.0.1, ping 8.8.8.8, ping -n 100 -l 128 1.1.1.1, tracert 8.8.4.4, tasklist, getmac, arp - a, route print,netstat, and exit. It will log it to a file called "System Log V4 -- ' + month + '-' + day + '-' + year + '.txt"\n')


ipAddressToPing = input("Enter Local IP address to ping: ")
print(ipAddressToPing + " was entered.")
f.write("\nThe user entered " + ipAddressToPing + " to ping.\n")

#Enter any CMDs you want to run here

runWindowsCMD("hostname")
runWindowsCMD("systeminfo")
runWindowsCMD("ipconfig /all")
# runWindowsCMD("ping 192.168.69.1")#Midlothian Router
runWindowsCMD("ping " + ipAddressToPing)#User defined IP address

# runWindowsCMD("ping 192.168.1.1")#Common router address
# runWindowsCMD("ping 192.168.2.1")#Common router address
runWindowsCMD("ping 127.0.0.1")#Local Host loopback
runWindowsCMD("ping -n 100 -l 128 1.1.1.1")
runWindowsCMD("ping 8.8.8.8")#Main Google Server
runWindowsCMD("tracert 8.8.4.4")#Backup Google Server - This finds the local service provider too.
runWindowsCMD("tasklist")
#runWindowsCMD("pathping 8.8.4.4")
runWindowsCMD("getmac")
#runWindowsCMD("nbtstat -r")
runWindowsCMD("arp -a")
#runWindowsCMD("arp -a 192.168.69.1")
runWindowsCMD("route print")
runWindowsCMD("netstat")
runWindowsCMD("exit")

print("\nEnd of Script\n")
f.write("\nEnd of Script\n")
print(myLocalTime())
f.write(myLocalTime())
print("\n")
f.write("\n")

f.close()

sys.exit()

