473,326 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Running Windows commands with Python

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
14 24015
bartonc
6,596 Expert 4TB
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
3,237 Expert 2GB
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
6,596 Expert 4TB
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
horace1
1,510 Expert 1GB
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
136 Expert 100+
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
horace1
1,510 Expert 1GB
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
miago
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
3,237 Expert 2GB
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
miago
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
miago
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
3,237 Expert 2GB
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
miago
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
3,237 Expert 2GB
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
136 Expert 100+
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

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

Similar topics

0
by: Will Seay | last post by:
At the end of this message I've pasted a script we're trying to modify slightly. I don't believe it is VBscript or javascript but these are the closest groups I could find with my limited...
1
by: Darren Jensen | last post by:
Hi, I have a machine which is running Windows Server 2003, IIS and .NET V1.1. I have been using this machine very well for about 6 months now for running ASP.NET on IIS and the occasional .NET...
9
by: kapsi | last post by:
Hey all, I was just wondering as to how to implement dos commands and windows commands using the C language? any ideas?
6
by: AP | last post by:
Hello, I am having trouble trying to assign an object to a running program in windows in VB.NET A COM component class with the same name as the windows program is avaliable to use in VB.NET. The...
4
by: BSman | last post by:
Hello all, I am a fairly new user of python and was given an assignment to try and execute windows commands in python. I've done a fair amount of searching on the internet and haven't come up...
6
by: Mike | last post by:
I'm having trouble running TSQL commands when SQL2005 is in single- user mode. I've restarted SQL with -m, -c, -T3608 set in the startup options. I can get into Config manager OK, but as soon...
0
by: =?Utf-8?B?Q2hyaXN0aW5h?= | last post by:
Whoever can figure this problem out will be a genius. I have a home network that includes: 2 desktop computers both running on Windows XP and 1 laptop running Windows Vista. The desktop computers...
4
by: fang | last post by:
Dear all: The mouse cannot be responded in the windows of python(command line) and cut and paste cannot be done. ctrl c and ctrl v do not work. But they do work in IDLE. please teach me about...
1
by: Cambridge10 | last post by:
Hi, I'm using perl Tk and I have to call a couple of Windows commands but they are executed in a dosbox. I want to prevent a dosbox is opened or at least hidden for the user. Is there any way...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.