November 1st, 2012, 04:41 AM
Code that will write into cmd
I am new to programming and I would like to create a program that will open cmd and then run a few commands. Using python 2.7 can i create a program that will open cmd (already done) and run a few commands like ipconfig, ping and a few others without me typing in the cmd window?
November 1st, 2012, 06:20 AM
If you only need to run one program, and don't need to capture the program's output, you can use os.system():
However, you would have much better results overall using the subprocess module:
subprocess.call("ping nytimes.com") # same as os.system()
# capture the called program's output
p = subprocess.Popen("ping nytimes.com", stdout=subprocess.PIPE)
# get just the first line of output from 'ping'
lines = p.stdout.read().splitlines()
firstline = lines
# extract the IP address from the ping trace
startpos = firstline.find('[')
endpos = firstline.find(']')
print firstline[startpos + 1:endpos]