python粘结shell监听端口设置

Posted VictorTiper

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python粘结shell监听端口设置相关的知识,希望对你有一定的参考价值。

通过python粘接,替代了shell判断状态是否ok的步骤。简单方便

#encoding=utf8
import os
import time
import socket
import commands
def checktcp(ip,port,ser):
        	
    cmdstra="netstat -atn|grep "+port
    (status, output) = commands.getstatusoutput(cmdstra)
    if output:
	print "service %s==port=%s is up true" %(ser,port)
        return True
    else:
        print '%s tcp is down but now we will restart it !!!' % ser
        if ser=="alarmdata ser":
	    	startcmd="cd /root/test/testalarm;./start.sh&"
	    try:
	        os.system(startcmd)
            print "%s restarted succeed!!"%ser
            return True
	    except:
            return False
def checkudp(ip ,port ,ser):
    cmdstra="netstat -aun|grep "+port
    (status, output) = commands.getstatusoutput(cmdstra)
    if  output:
        print "service %s==port=%s is up true" %(ser,port)
    else:
        print "service %s==port=%s is down false restarting service" %(ser,port)
	if ser=="config ser":
	    startcmd="cd /root/test/testConfigSvr/bin;./start.sh>>localcongif.log"
	    try:
	        os.system(startcmd)
			print "%s restarted succeed!!"%ser
	    except:
			pass
if __name__ == '__main__':
    for i in range(30):

        
        checktcp('127.0.0.1', "11002",u"alarmdata ser")
        checkudp("127.0.0.1","11003",u"config ser")
	time.sleep(2)

以上是关于python粘结shell监听端口设置的主要内容,如果未能解决你的问题,请参考以下文章

msf派生shell到cs

shell-插件服务监听端口的状态

shell-插件服务监听端口的状态

shell脚本杀死监听端口3000的进程? [复制]

shell脚本杀死监听端口3000的进程? [复制]

shell 脚本检测某网段中监听80端口 有多少