473,699 Members | 2,711 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to use subprocess.Pope n execute "find" in windows

In cmd, I can use find like this.

C:\>netstat -an | find "445"
TCP 0.0.0.0:445 0.0.0.0:0 LISTENING
UDP 0.0.0.0:445 *:*

C:\>

And os.system is OK.
>>import os
os.system('ne tstat -an | find "445"')
TCP 0.0.0.0:445 0.0.0.0:0 LISTENING
UDP 0.0.0.0:445 *:*
0
>>>
But I don't know how to use subprocess.Pope n to do this.

from subprocess import Popen, PIPE

p1 = Popen(['netstat', '-an'], stdout = PIPE)
p2 = Popen(['find', '"445"'], stdin = p1.stdout, stdout = PIPE)
print p2.stdout.read( )

It doesn't work.
Because subprocess.Pope n execute "find" like this.

C:\>find \"445\"
拒绝访问 - \

C:\>

It adds a '\' before each '"'.
How to remove the '\'?
Thank you.
Jun 27 '08 #1
8 4201
cl*****@gmail.c om wrote:
In cmd, I can use find like this.

C:\>netstat -an | find "445"
TCP 0.0.0.0:445 0.0.0.0:0 LISTENING
UDP 0.0.0.0:445 *:*

C:\>

And os.system is OK.
>>>import os
os.system('n etstat -an | find "445"')
TCP 0.0.0.0:445 0.0.0.0:0 LISTENING
UDP 0.0.0.0:445 *:*
0
>>>>

But I don't know how to use subprocess.Pope n to do this.
While there certainly are valid usecases for piping with subprocess in
Python - this ain't one I'd say. Just read the output of netstat yourself,
and filter for lines that contain the desired pattern.

Diez
Jun 27 '08 #2
On May 6, 11:19 am, clyf...@gmail.c om wrote:
In cmd, I can use find like this.

C:\>netstat -an | find "445"
TCP 0.0.0.0:445 0.0.0.0:0 LISTENING
UDP 0.0.0.0:445 *:*

C:\>

And os.system is OK.>>import os
>os.system('net stat -an | find "445"')

TCP 0.0.0.0:445 0.0.0.0:0 LISTENING
UDP 0.0.0.0:445 *:*
0

But I don't know how to use subprocess.Pope n to do this.

from subprocess import Popen, PIPE

p1 = Popen(['netstat', '-an'], stdout = PIPE)
p2 = Popen(['find', '"445"'], stdin = p1.stdout, stdout = PIPE)
print p2.stdout.read( )
I would say that, according to documentation, the following should
work:
print p2.communicate( )[0]
Philippe
Jun 27 '08 #3
On May 6, 5:19 pm, clyf...@gmail.c om wrote:
In cmd, I can use find like this.

C:\>netstat -an | find "445"
TCP 0.0.0.0:445 0.0.0.0:0 LISTENING
UDP 0.0.0.0:445 *:*

C:\>

And os.system is OK.>>import os
>os.system('net stat -an | find "445"')

TCP 0.0.0.0:445 0.0.0.0:0 LISTENING
UDP 0.0.0.0:445 *:*
0

But I don't know how to use subprocess.Pope n to do this.

from subprocess import Popen, PIPE

p1 = Popen(['netstat', '-an'], stdout = PIPE)
p2 = Popen(['find', '"445"'], stdin = p1.stdout, stdout = PIPE)
print p2.stdout.read( )

It doesn't work.
Because subprocess.Pope n execute "find" like this.

C:\>find \"445\"
拒绝访问 - \

C:\>

It adds a '\' before each '"'.
How to remove the '\'?
Thank you.
cannot help with the backslashes but try findstr instead of find
Jun 27 '08 #4
On May 6, 7:19*pm, clyf...@gmail.c om wrote:
In cmd, I can use find like this.

C:\>netstat -an | find "445"
* TCP * *0.0.0.0:445 * * * * * *0.0.0.0:0 * * * * * * *LISTENING
* UDP * *0.0.0.0:445 * * * * * **:*

C:\>

And os.system is OK.>>import os
>os.system('net stat -an | find "445"')

* TCP * *0.0.0.0:445 * * * * * *0.0.0.0:0 * * * * * * *LISTENING
* UDP * *0.0.0.0:445 * * * * * **:*
0

But I don't know how to use subprocess.Pope n to do this.

from subprocess import Popen, PIPE

p1 = Popen(['netstat', '-an'], stdout = PIPE)
p2 = Popen(['find', *'"445"'], stdin = p1.stdout, stdout = PIPE)
print p2.stdout.read( )
Get rid of the extra quotes. ie:
p2 = Popen(['find', '445'], stdin = p1.stdout, stdout = PIPE)

The quotes on the command line and on the os.system call are consumed
by the shell. The program doesn't see them.
Jun 27 '08 #5
On 5月7日, 上午9时45分, Justin Ezequiel <justin.mailing li...@gmail.com >
wrote:
On May 6, 5:19 pm, clyf...@gmail.c om wrote:
In cmd, I can use find like this.
C:\>netstat -an | find "445"
TCP 0.0.0.0:445 0.0.0.0:0 LISTENING
UDP 0.0.0.0:445 *:*
C:\>
And os.system is OK.>>import os
>>os.system('ne tstat -an | find "445"')
TCP 0.0.0.0:445 0.0.0.0:0 LISTENING
UDP 0.0.0.0:445 *:*
0
But I don't know how to use subprocess.Pope n to do this.
from subprocess import Popen, PIPE
p1 = Popen(['netstat', '-an'], stdout = PIPE)
p2 = Popen(['find', '"445"'], stdin = p1.stdout, stdout = PIPE)
print p2.stdout.read( )
It doesn't work.
Because subprocess.Pope n execute "find" like this.
C:\>find \"445\"
拒绝访问 - \
C:\>
It adds a '\' before each '"'.
How to remove the '\'?
Thank you.

cannot help with the backslashes but try findstr instead of find
Thank you.
findstr doesn't need quotes, so it works.
Jun 27 '08 #6
On 5月7日, 下午2时41分, alito <alito...@gmail .comwrote:
On May 6, 7:19 pm, clyf...@gmail.c om wrote:
In cmd, I can use find like this.
C:\>netstat -an | find "445"
TCP 0.0.0.0:445 0.0.0.0:0 LISTENING
UDP 0.0.0.0:445 *:*
C:\>
And os.system is OK.>>import os
>>os.system('ne tstat -an | find "445"')
TCP 0.0.0.0:445 0.0.0.0:0 LISTENING
UDP 0.0.0.0:445 *:*
0
But I don't know how to use subprocess.Pope n to do this.
from subprocess import Popen, PIPE
p1 = Popen(['netstat', '-an'], stdout = PIPE)
p2 = Popen(['find', '"445"'], stdin = p1.stdout, stdout = PIPE)
print p2.stdout.read( )

Get rid of the extra quotes. ie:
p2 = Popen(['find', '445'], stdin = p1.stdout, stdout = PIPE)

The quotes on the command line and on the os.system call are consumed
by the shell. The program doesn't see them.
You must be a linux user:)

I guess, in windows, the quotes are consumed by the c runtime library.
Mayby the "find" in windows doesn't use the argc/argv but the windows
API GetCommandLine( ).

I wrote a c program to prove it.

#include <windows.h>
#include <stdio.h>

int main(int argc, char **argv) {
int i;

printf("%s\n", GetCommandLine( ));
for (i = 0; i < argc; ++i)
printf("%d: %s\n", i, argv[i]);

return 0;
}

The output is:
C:\>test 1 2 "3"
test 1 2 "3"
0: test
1: 1
2: 2
3: 3

C:\>

Notice that, GetCommandLine( ) does not consume the quotes, but the
(char **argv) does.
Jun 27 '08 #7
En Wed, 07 May 2008 23:29:58 -0300, <cl*****@gmail. comescribi贸:
On 5鏈7鏃, 涓婂崍9鏃45鍒 , Justin Ezequiel <justin.mailing li...@gmail.com >
wrote:
>On May 6, 5:19 pm, clyf...@gmail.c om wrote:
p1 = Popen(['netstat', '-an'], stdout = PIPE)
p2 = Popen(['find', '"445"'], stdin = p1.stdout, stdout = PIPE)
print p2.stdout.read( )
It doesn't work.
Because subprocess.Pope n execute "find" like this.
C:\>find \"445\"
It adds a '\' before each '"'.
How to remove the '\'?
Thank you.

cannot help with the backslashes but try findstr instead of find

Thank you.
findstr doesn't need quotes, so it works.
Build the command line yourself -instead of using a list of arguments-. Popen doesn't play with the quotes in that case:

p1 = Popen(['netstat', '-an'], stdout = PIPE) # using list
p2 = Popen('find "445"', stdin = p1.stdout, stdout = PIPE) # using str
print p2.communicate( )[0]

--
Gabriel Genellina

Jun 27 '08 #8
On 5月8日, 下午5时39分, "Gabriel Genellina" <gagsl-....@yahoo.com. arwrote:
En Wed, 07 May 2008 23:29:58 -0300, <clyf...@gmail. comescribió:
On 5月7日, 上午9时45分, Justin Ezequiel <justin.mailing li...@gmail.com >
wrote:
On May 6, 5:19 pm, clyf...@gmail.c om wrote:
p1 = Popen(['netstat', '-an'], stdout = PIPE)
p2 = Popen(['find', '"445"'], stdin = p1.stdout, stdout = PIPE)
print p2.stdout.read( )
It doesn't work.
Because subprocess.Pope n execute "find" like this.
C:\>find \"445\"
It adds a '\' before each '"'.
How to remove the '\'?
Thank you.
cannot help with the backslashes but try findstr instead of find
Thank you.
findstr doesn't need quotes, so it works.

Build the command line yourself -instead of using a list of arguments-. Popen doesn't play with the quotes in that case:

p1 = Popen(['netstat', '-an'], stdout = PIPE) # using list
p2 = Popen('find "445"', stdin = p1.stdout, stdout = PIPE) # using str
print p2.communicate( )[0]

--
Gabriel Genellina
Thanks very much.
You solved my problem.
Jun 27 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2061
by: Galsaba | last post by:
I am trying to find a script to a "dealer locator" or "find a store near you". I will have two tables. One will include all US and Canada ZIPs including lat and longtitudes. The other one is a table of our dealers (or store) including thier ZIP codes. When the user enters his ZIP code, the application needs to check (based on the lat and long) if there is a dealer in a 30 miles radious. After the application finds the list of all the...
3
4139
by: Dmitry Jouravlev | last post by:
Hi, I have a number of C++ solutions in Visual Studio .NET and when i compile them using "Whole Program Optimization", certain projects report a LNK1171 error saying that c2.dll could not be loaded. The error contains the correct path to c2.dll (and it is definately there). This only happens on some projects and only when "whole program optimization" option is turned on. If i turn off this option, the problem goes away. I have other...
1
1815
by: Bart Masschelein | last post by:
Hello, I have a VC++ project which uses files located on my Unix account Browsing (eg. go to Definition etc) works fine, however, if I use "Fin in Files" it does not find any single instance of the string entered. guess? it is a Windows (find)/Unix (grep) problem, but is there also solution to it? That would be great. Thanks,
2
7657
by: Lenonardo | last post by:
Hi. I'm writing a VB.Net application to update multiple Excel Worksheets. I'm using late binding (i.e. all variables are objects + use createobject) I develop the application on an XP machine with Excel 2002 (version 10.0) installed - and the code works fine. I then test the code on a laptop running Excel 2000 (version 9.0) and it
2
1567
by: Bill nguyen | last post by:
How can I get a listbox to display the most closely matched items when user types in the first few letters/digits of displaymember value? Thanks a million Bill
0
2792
by: Rave | last post by:
This is a long shot, but I thought I'd try it. I am currently using excel as an inventory tool. I currently have a hand-held scanner plugged into a laptop for reading barcodes. Using the "Find and Replace" fuction, I scan the merchandise which then searches the spreadsheet for the matching inventory number. When it is found, I highlight that cell in yellow. After scanning and coloring all of my inventory I can then see exactly what is...
0
1235
by: zensunni | last post by:
The script I've made checks for identical fields in the database before it inserts a new row. However, there is one number that, when checked for, will not be found. It assumes there is no row, so it will insert it again....thus allowing for duplicate records. I've tried tonnes of data with no problems. But if I use this number, it skips it.Anyone have any clue? The number is "49.0350". Here's my code:...
1
2298
by: vps | last post by:
I have Apache 2.2 server running on Windows XP SP2 with PHP 5.2.5. I have run go-pear.bat in PHP directory (system-wide, all defaults). Running 損ear command lists additional commands. I cannot find 搈ail.php on local drives (performed a Windows search). Using 搈ail.php from separate PEAR package does not solve the problem, since "mail" class is not recognized then.
1
20758
by: sidd4one | last post by:
Hi Hi friends when ever i start my system i use get the one message as windows can not find "CSRCS.EXE".i tried everthing to delete but i can't.But friends i known its vires.please help in this and how to remove it. wating for replay sidd
0
8704
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we抣l explore What is ONU, What Is Router, ONU & Router抯 main usage, and What is the difference between ONU and Router. Let抯 take a closer look ! Part I. Meaning of...
0
9187
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8936
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8894
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6544
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4390
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4637
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3071
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2361
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.