Python 如何搜索带有subprocess.check\u输出的行?
我有一个套接字连接,客户端在其中提交其Windows用户名。然后,服务器运行“网络用户用户名/域”以查找其过期日期。 我试图找到字符串“passwordexpires”并打印该行Python 如何搜索带有subprocess.check\u输出的行?,python,python-3.x,Python,Python 3.x,我有一个套接字连接,客户端在其中提交其Windows用户名。然后,服务器运行“网络用户用户名/域”以查找其过期日期。 我试图找到字符串“passwordexpires”并打印该行 print(line) 什么也没给我。 如果我使用 print(result) 我确实得到了命令的全部结果,但看起来有点乱七八糟。 如何获取“密码过期”的行?我是否需要以某种方式格式化它,这样“结果”就不会看起来一团糟 import subprocess import os from so
print(line)
什么也没给我。
如果我使用
print(result)
我确实得到了命令的全部结果,但看起来有点乱七八糟。
如何获取“密码过期”的行?我是否需要以某种方式格式化它,这样“结果”就不会看起来一团糟
import subprocess
import os
from socket import socket, gethostbyname, AF_INET, SOCK_DGRAM
import sys
PORT_NUMBER = 3555
SIZE = 1024
hostName = gethostbyname( '0.0.0.0' )
mySocket = socket( AF_INET, SOCK_DGRAM )
mySocket.bind( (hostName, PORT_NUMBER) )
print ("Test server listening on port {0}\n".format(PORT_NUMBER))
# Receive no more than 1024 bytes
while True:
(data,addr) = mySocket.recvfrom(SIZE)
try:
#print >>sys.stderr, 'Received data from', addr
while True:
print (data.decode())
batcmd=('net user ' + data.decode() + ' /domain')
result = subprocess.check_output(batcmd, shell=True)
# print (result) shows details but it is messy looking
for line in result:
if str(line).startswith("Password expires"):
print (line)
# if data:
# print >>sys.stderr, 'sending data back to the client'
# mySocket.sendto({ },addr).format(line)
# # TypeError: sendto() argument 1 must be string or buffer, not dict
# else:
# print >>sys.stderr, 'no more data from', client_address
# break
break
# send result back to client ?
#mySocket.send(result.encode('ascii'))
sys.exit()
finally:
print('closing socket')
mySocket.close()
如果我打印(结果),所有内容如下所示:
.local.\r\n\r\nUser Name js83838\r\nFull Name
John Doe \r\n\Country Code Comment Here.
Account active Locked\r\nAccount expires
more data like this
and this
更新
我正在尝试这个,它显示每一行,而不是1。每一条线前面都有一个b',看起来很整齐
while True:
(data,addr) = mySocket.recvfrom(SIZE)
#print >>sys.stderr, 'Received data from', addr
while True:
print (data.decode())
batcmd=('net user ' + data.decode() + ' /domain')
result = subprocess.check_output(batcmd, shell=True)
# print (result) # shows details but it is messy looking
for line in result.splitlines():
if str(line).find("Password expires"):
print (line)
子进程。check\u输出不返回行,而是返回字节缓冲区
将其解码为字符串后(Python 3中需要):
您需要根据行进行拆分,否则您的循环将逐字符生成,并且startswith
不会崩溃,但什么也找不到
for line in result.splitlines():
这就是诀窍。因此有两个问题:使用startswith
检查整个缓冲区,以及不解码到str
,这使得搜索结果一无所获
或者,如果line.startswith(b“Password expires”):
,则不要解码。这使得代码与Python2兼容,这是一个额外的好处
旁白:解析net user的输出时要小心,因为字符串本地化可能会使您的程序丢失信息。这里有一个解决方案,允许您:逐行获取输出,仅获取带有“密码过期”的行,而不使用shell=True
batcmd=['net', 'user', data.decode(), '/domain')
net_process = subprocess.Popen(batcmd, stdout=subprocess.PIPE)
grep_process= subprocess.Popen(["findstr", "Password expires"], stdin=net_process.stdout, stdout=subprocess.PIPE) # or grep depending on your system
for line in grep_process.stdout.readlines():
# do what you want
我在初始化行1224的\u execute\u child startupinfo)FileNotFoundError:[WinError 2]中得到一个错误\lib\subprocess.py行947,系统找不到指定的文件dok,这可能是因为grep在windows中不存在。谷歌告诉我一个等价物是findstr。当我添加.splitlines()时,它仍然不显示任何内容。尝试打印line
并查看打印的内容。我在上面的代码中添加了“for line in result.splitlines():”,当我使用print(line)运行它时,它会打印用户名并直接转到“closing socket”,还尝试使用更改startswith(“Password”)改为包含(“Password”),这给了我一个错误“str”对象没有属性“contains”,我取出try/except,但它仍然没有显示任何内容。我将startswith(“Password expires”)改为find(“Password expires”)它显示每一行的格式正确。它显示结果,但它们与在cmd提示符下运行命令时不完全一样。几行一起运行,有些行在下一行的基础上添加了选项卡。因此,看起来所有内容都是以一定角度打印的。
batcmd=['net', 'user', data.decode(), '/domain')
net_process = subprocess.Popen(batcmd, stdout=subprocess.PIPE)
grep_process= subprocess.Popen(["findstr", "Password expires"], stdin=net_process.stdout, stdout=subprocess.PIPE) # or grep depending on your system
for line in grep_process.stdout.readlines():
# do what you want