422,347 Members | 1,858 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 422,347 IT Pros & Developers. It's quick & easy.

Running Windows commands with Python

P: 6
Searching this forum I found an example of running Unix commands from Python;I wonder if there is a way to get the output of windows commands on a string using Python.
I believe command line functionality is built in into Python only for *nix environment.Is there's a windows solution?
Thanks in advance;
Miago.
Feb 3 '07 #1
Share this Question
Share on Google+
14 Replies


bartonc
Expert 5K+
P: 6,596
Searching this forum I found an example of running Unix commands from Python;I wonder if there is a way to get the output of windows commands on a string using Python.Thanks in advance;
Miago.
It sort of depends on which Window app you are trying to interface to. Most Microsoft products and some third party products are COM (Common Object Model) aware. With the PythonWin package, you can send commands to these apps as if you were using a VB script. If your python program has a full GU interface you could Select the text that you want, then Copy using the VB commands then get the clipboard contents in your program. Or you might be able to SaveAs text and read the resulting file. I've seen an example that copied text from MS Word and pasted it to a web page. The possibilities are endless, but I'm not very familiar with PythonWin. There are some on this forum who are, though. I use wxPython so all my Python programs have almost complete access to Windows' features like the clipboard. PythonWin is another way to do this, but you pretty much have to know the Windows API first.
Hope this is useful to you,
Barton
Feb 3 '07 #2

Motoma
Expert 2.5K+
P: 3,235
Searching this forum I found an example of running Unix commands from Python;I wonder if there is a way to get the output of windows commands on a string using Python.Thanks in advance;
Miago.
You may be able to do this by reassigning sys.stdout.
Feb 4 '07 #3

bartonc
Expert 5K+
P: 6,596
Searching this forum I found an example of running Unix commands from Python;I wonder if there is a way to get the output of windows commands on a string using Python.Thanks in advance;
Miago.
Good point, motoma. I was thinking of Windows apps, but there may be a way to redirect the output of a Windows command that is designed to echo text to the command-line. Anybody have any experience with this?
Feb 4 '07 #4

Expert 100+
P: 1,510
Searching this forum I found an example of running Unix commands from Python;I wonder if there is a way to get the output of windows commands on a string using Python.Thanks in advance;
Miago.
try
Expand|Select|Wrap|Line Numbers
  1. import subprocess
  2. line=subprocess.Popen("test", stdout=subprocess.PIPE).communicate()[0]
  3.  
the output from program test.exe will be in line
Feb 4 '07 #5

dshimer
Expert 100+
P: 136
I am always interested in the number of ways you can accomplish things in Python, I use the following, does it do basically the same thing, or do you see problems or disadvantages to it?
Expand|Select|Wrap|Line Numbers
  1. import os
  2. CommandOutput=os.popen('dir/w').read()
or if I am expecting to get back many lines and want it placed in a list I use
Expand|Select|Wrap|Line Numbers
  1. import os,string
  2. CommandOutputList=string.split(os.popen('dir/w').read())
Feb 5 '07 #6

Expert 100+
P: 1,510
I am always interested in the number of ways you can accomplish things in Python, I use the following, does it do basically the same thing, or do you see problems or disadvantages to it?
Expand|Select|Wrap|Line Numbers
  1. import os
  2. CommandOutput=os.popen('dir/w').read()
or if I am expecting to get back many lines and want it placed in a list I use
Expand|Select|Wrap|Line Numbers
  1. import os,string
  2. CommandOutputList=string.split(os.popen('dir/w').read())
I do not know sufficent python to indicate which is the best approach! do we have a python expert on hand to comment?
Feb 5 '07 #7

P: 6
Dear dudes, I did as you advised,but to no success.I type this:
Code:
import os
DNS=raw_input("DNS")
CommandOutput=os.popen('tracert %s').read()%(DNS)
print CommandOutput

And this is what I get:
"Impossible to resolve to destination system"

And I don't really understand how to implement this:
import subprocess
DNS=raw_input("Enter DNS")
line=subprocess.Popen("tracert %s", stdout=subprocess.PIPE).communicate()[0] %(DNS)
I should place an integer somewhere,for I receive this message:

line=subprocess.Popen("tracert %s", stdout=subprocess.PIPE).communicate()[0] %(DNS)
Traceback (most recent call last):
File "<input>", line 1, in ?
File "C:\Python24\lib\subprocess.py", line 533, in __init__
(p2cread, p2cwrite,
File "C:\Python24\lib\subprocess.py", line 593, in _get_handles
p2cread = self._make_inheritable(p2cread)
File "C:\Python24\lib\subprocess.py", line 634, in _make_inheritable
DUPLICATE_SAME_ACCESS)
TypeError: an integer is required
>>>

I thank you all for your kindness and attention;I really want to make progress with this;

As a last question,I want to ask:getservbyname simply returns the usual service for a port or does it scan the machine it's running on?If the latter is thecase,then is it possible to scan remote ports using this?I found the manual somewhat ambiguous....Thanks in advance.
Feb 6 '07 #8

Motoma
Expert 2.5K+
P: 3,235
Dear dudes, I did as you advised,but to no success.I type this:
Code:
import os
DNS=raw_input("DNS")
CommandOutput=os.popen('tracert %s').read()%(DNS)
print CommandOutput

And this is what I get:
"Impossible to resolve to destination system"

And I don't really understand how to implement this:
import subprocess
DNS=raw_input("Enter DNS")
line=subprocess.Popen("tracert %s", stdout=subprocess.PIPE).communicate()[0] %(DNS)
I should place an integer somewhere,for I receive this message:

line=subprocess.Popen("tracert %s", stdout=subprocess.PIPE).communicate()[0] %(DNS)
Traceback (most recent call last):
File "<input>", line 1, in ?
File "C:\Python24\lib\subprocess.py", line 533, in __init__
(p2cread, p2cwrite,
File "C:\Python24\lib\subprocess.py", line 593, in _get_handles
p2cread = self._make_inheritable(p2cread)
File "C:\Python24\lib\subprocess.py", line 634, in _make_inheritable
DUPLICATE_SAME_ACCESS)
TypeError: an integer is required
>>>

I thank you all for your kindness and attention;I really want to make progress with this;

As a last question,I want to ask:getservbyname simply returns the usual service for a port or does it scan the machine it's running on?If the latter is thecase,then is it possible to scan remote ports using this?I found the manual somewhat ambiguous....Thanks in advance.

Try this:

Expand|Select|Wrap|Line Numbers
  1. import os
  2. DNS=raw_input("DNS")
  3. CommandOutput=os.popen('tracert %s' % (DNS)).read()
  4. print CommandOutput
However, I believe you will need to provide a host for tracert to work.
Feb 6 '07 #9

P: 6
Thank you very much,Motoma;I've put the dns outside the scope for string formatting.It works perfectly now.Thank you so much!!As for the other question....Is there any enlightened ones who could clarify the use of getservbyname() for me??I would appreciate very much.Thank you.
Feb 6 '07 #10

P: 6
So it actually verifies the availability of the service by port on a given hostname;in case I give no args,will it default to localhost??The manual does not specify this,if I can remember correctly.Thank you again,Motoma,my journey into the programming world was made smooth by your kindness.
Feb 6 '07 #11

Motoma
Expert 2.5K+
P: 3,235
So it actually verifies the availability of the service by port on a given hostname;in case I give no args,will it default to localhost??The manual does not specify this,if I can remember correctly.Thank you again,Motoma,my journey into the programming world was made smooth by your kindness.
My appologies, I was mistaken.
getservbyname() returns the port number for a given service and protocol.
An example:

Expand|Select|Wrap|Line Numbers
  1. >>> socket.getservbyname("http","tcp")
  2. 80
  3.  
This shows that the tcp:http protocol should be located on port 80.
You can also specify without the second argument.

Hope this helps, and welcome to The Scripts.
Feb 6 '07 #12

P: 6
This is what the manual says:

"getservbyname( servicename[, protocolname])

Translate an Internet service name and protocol name to a port number for that service. The optional protocol name, if given, should be 'tcp' or 'udp', otherwise any protocol will match. "
I see no place for the host/port args....Please be patient!:)


I found an interesting page:
http://gapz.tuxfamily.org/repos/Pyth...rogramming.pdf

Doing some research....Be back soon!
Feb 6 '07 #13

Motoma
Expert 2.5K+
P: 3,235
This is what the manual says:

"getservbyname( servicename[, protocolname])

Translate an Internet service name and protocol name to a port number for that service. The optional protocol name, if given, should be 'tcp' or 'udp', otherwise any protocol will match. "
I see no place for the host/port args....Please be patient!:)


I found an interesting page:
http://gapz.tuxfamily.org/repos/Python/Python_network_programming.pdf

Doing some research....Be back soon!
Yes, my apologies, my previous post was incorrect about it's usage.
Feb 6 '07 #14

dshimer
Expert 100+
P: 136
As a last question,I want to ask:getservbyname simply returns the usual service for a port or does it scan the machine it's running on?If the latter is thecase,then is it possible to scan remote ports using this?I found the manual somewhat ambiguous....Thanks in advance.
It seems to me to just scan the machine it is running on. Though getaddrinfo seems to return some interesting info if you know what you are looking for. for example for a local network storage device if I get the host ip by name (in this case nas) then send it to getaddrinfo looking for http
Expand|Select|Wrap|Line Numbers
  1. socket.getaddrinfo(socket.gethostbyname('nas'),'http')
  2. returns
  3. [(2, 1, 0, '', ('192.168.1.250', 80))]
Thouth I'm not really sure what you are looking for
Feb 6 '07 #15

Post your reply

Sign in to post your reply or Sign up for a free account.