python实时获取cmd的输出

最近发现一个问题,一个小伙儿写的console程序不够健壮,监听socket的时候容易崩,造成程序的整体奔溃,无奈他没有找到问题的解决办法,一直解决不了,可是这又是一个监控程序,还是比较要紧的,又必须想办法解决。

(这是要搞死我的节奏啊….)由于个人不太懂他用的语言,只能在程序外围想办法。

环境描述:

1. 目标程序执行时会监听8080端口,tcp,并在每一次client连接后通过console输出client的ip地址。

2. 监听不是一次性完成的,而是一直监听,程序并不会退出

3. 为了监控需要,最好能对连接的ip进行排序,整理。

p.s. 系统基于windows平台。

想起来做监控程序,简单点比较好,于是想到了python。

我的预想逻辑是这样的,通过python检测目标程序是否崩了,如果中标就启动目标程序,并进行监控,每输出一次,python进行一次数据运算整理,然后循环。

第一步,先搞定输出的捕获问题。

# this method is used for monitoring
import time
import subprocess
import locale
import codecs
mylist = []
ps = subprocess.popen(‘netstat -a’, stdin=subprocess.pipe, stdout=subprocess.pipe, shell=true)
while true:
data = ps.stdout.readline()
if data == b”:
if ps.poll() is not none:
break
else:
mylist.append(data.decode(codecs.lookup(locale.getpreferredencoding()).name))
newlist = []
for i in mylist:
if i.find(‘192.168’) > 0:
newlist.append(i)
newlist.sort()
print(‘sum of requests from lan:’, len(newlist))

我用netstat -a替代那个需要持续输出的程序,执行程序,发现程序和想象的不太一样,确实是实时获得数据了,但是感觉总是有点不太和谐,不管了,继续。

第二步,解决监控程序的问题

程序或者还是死的,有一点非常关键,就是监听端口,那只要检测一下端口就行了。三个办法:

1. 找端口检测的api

2. 连接一次目标端口,通了就是活的

3. netstat

第一种方法需要去找找有没有相关的api,第二种方法容易对目标程序的正常运行造成问题,第三种我想都没想就用了吧。这里需要用到cmd的重定向功能

# this method is used for monitoring
import time
import subprocess
import locale
import codecs
def getstdout(p):
mylist = []
while true:
data = p.stdout.readline()
if data == b”:
if p.poll() is not none:
break
else:
mylist.append(data.decode(codecs.lookup(locale.getpreferredencoding()).name))
return mylist
while true:
ps = subprocess.popen(‘netstat -an | findstr “8080”‘, stdin=subprocess.pipe, stdout=subprocess.pipe, shell=true)
resultlist = getstdout(ps)
if len(resultlist) >= 1:
pass
else:
print(time.strftime(“%y-%m-%d %h:%m:%s”))
subprocess.popen(‘taskkill.exe /f /im node.exe’, shell=false)     # 防止动作过快,把新建的程序整死了
time.sleep(3)
subprocess.popen(‘start node d:\\app.js’, shell=true)
time.sleep(10)

netstat -an获得当前的端口监听情况,“|”将netstat的输出重定向到findstr函数

netstat -an | findstr “8080” 查找有8080端口的地址行,有就说明活着,否则就是挂了。

最后一步,整合

# this method is used for monitoring
import time
import subprocess
import locale
import codecs
def getstdout(p):
mylist = []
while true:
data = p.stdout.readline()
if data == b”:
if p.poll() is not none:
break
else:
mylist.append(data.decode(codecs.lookup(locale.getpreferredencoding()).name))
return mylist
while true:
ps = subprocess.popen(‘netstat -an | findstr “8080”‘, stdin=subprocess.pipe, stdout=subprocess.pipe, shell=true)
resultlist = getstdout(ps)
if len(resultlist) >= 1:
pass
else:
print(time.strftime(“%y-%m-%d %h:%m:%s”))
subprocess.popen(‘taskkill.exe /f /im node.exe’, shell=false)
time.sleep(3)
pss = subprocess.popen(‘start cmd.exe /k node app.js’, stdin=subprocess.pipe,
stdout=subprocess.pipe, shell=true)
alist = getstdout(pss)
newlist = []
for i in alist:
if i.find(‘192.168’) > 0:
newlist.append(i)
newlist.sort()
print(‘sum of requests from lan:’, len(newlist))
time.sleep(10)

然后发现有问题,程序完全不会定时检测,只会卡在readline()上。

各种找问题,发现那个process.stdout.readline()是个同步方法,没结果就不返回。有没有的能异步的方法?

有人用fnctl,windows不支持,pass

asyncio?看了半天没太明白…

折腾了半天,最后关头我还是用c#解决这个问题了….

参考代码见http://www.jiamaocode.com/cts/1031.html,打不开的话http://www.cnblogs.com/sode/archive/2012/07/10/2583941.html有转载

总算解决了这个问题,但是我心中还是不爽,思考了很久如何解决异步readline()的问题。忽然想起来多线程这个利器,干脆开

一个线程,不返回就等着,不就问题解决了。

# this method is used for monitoring
import time
import subprocess
import locale
import codecs
import threading
alist = []
def getstdout(p, asy):
if asy:
alist.clear()
mylist = []
while true:
data = p.stdout.readline()
if data == b”:
if p.poll() is not none:
break
else:
if asy:
alist.append(data.decode(codecs.lookup(locale.getpreferredencoding()).name))
else:
mylist.append(data.decode(codecs.lookup(locale.getpreferredencoding()).name))
return mylist
while true:
ps = subprocess.popen(‘netstat -an | findstr “”‘, stdin=subprocess.pipe, stdout=subprocess.pipe, shell=true)
resultlist = getstdout(ps, false)
if len(resultlist) >= :
newlist = []
for i in alist:
if i.find(‘.’) > :
newlist.append(i)
newlist.sort()
print(‘sum of requests from lan:’, len(newlist))
else:
print(time.strftime(“%y-%m-%d %h:%m:%s”))
subprocess.popen(‘taskkill.exe /f /im node.exe’, shell=false)
time.sleep()
pss = subprocess.popen(‘start cmd.exe /k node app.js’, stdin=subprocess.pipe,
stdout=subprocess.pipe, shell=true)
th = threading.thread(target=getstdout, args=[pss, true])
th.start()
time.sleep()

总结

有时候简单的解决方法也可以实现同样的功能,对比python的实现与c#的实现,c#更面向事件一点,python应该也有不错的解决方案,继续摸索…

以上内容是小编给大家分享的python实时获取cmd的输出的相关知识,希望大家喜欢。

Posted in 未分类

发表评论