保护原有进程,杀死新起来的进程
#-*- coding:utf-8 -*- #2018-01-17 #by-duwentao #version:python 2.7.9 #python2.6.6也可以使用 import os import signal pid_list=[] #设置最大进程 def max_pid(): f = open("/proc/sys/kernel/pid_max","r") pid = int(f.read().strip("\n")) f.close() if pid != 1024: print pid #设置最大进程数1024 os.system("echo \"1024\" >> /proc/sys/kernel/pid_max") else: print "max_pid : 1024" #获取需要保护的pid进程 def get_pid(): #判断要保护的进程是否存在,不存在则获取 if os.path.exists("pid.txt"): pass else: os.system("ps aux |awk \'{print $2}\' |sed \'s/PID/0/g\' >> pid.txt") #把要保护的进行加到列表里 f = open("pid.txt","r") for pid in f.readlines(): pid = int(pid.strip("\n")) pid_list.append(pid) f.close() return pid_list def kill_pid(pid_list): self_pid = os.getpid() #获取自己程序的pid while True: for i in range(1,1025): if i in pid_list: pass elif (i == self_pid): pass else: try: a = os.kill(i, signal.SIGKILL) print '已杀死pid为%s的进程, 返回值是:%s' % (i, a) except: pass def get_ip(): ip5 = int(raw_input("请输入要循环ip的第几位(3或者4):").strip("\n")) if ip5 == 3 : ip1 = int(raw_input("请输入ip第一位:").strip("\n")) ip2 = int(raw_input("请输入ip第二位:").strip("\n")) ip3 = int(raw_input("请输入ip第三位:").strip("\n")) ip4 = int(raw_input("请输入ip第四位:").strip("\n")) username = raw_input("请输入用户名:").strip("\n") passwd = raw_input("请输入默认密码:").strip("\n") for ip3 in range(105,107): ip = str(ip1) + "." + str(ip2) + "." + str(ip3) + "." + str(ip4) ssh_connect(ip,username,passwd) #调用连接函数 elif ip5 == 4: ip1 = int(raw_input("请输入ip第一位:").strip("\n")) ip2 = int(raw_input("请输入ip第二位:").strip("\n")) ip3 = int(raw_input("请输入ip第三位:").strip("\n")) #ip4 = int(raw_input("请输入ip第四位:").strip("\n")) username = raw_input("请输入用户名:").strip("\n") passwd = raw_input("请输入默认密码:").strip("\n") for ip4 in range(105,107): ip = str(ip1) + "." + str(ip2) + "." + str(ip3) + "." + str(ip4) ssh_connect(ip,username,passwd) else: print ("输入有误,正在退出") exit() def ssh_connect(ip,username,passwd): try: #python2.6.6自带的模块 import paramiko except: print "请使用python2.6.6运行或者安装paramiko模块" try: ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(ip,username=username,password=passwd,timeout= 1 ) #os.system(" echo centos | passwd --stdin root") # 修改root密码为centos f=open('ssh.txt','a') f.write("ip:%s,username:%s,password:%s\n" %(ip,username,passwd)) f.close() ssh.close() except: ·pass def menu(): while True: print "\033[32m -------请选择------\033[0m" print "\033[32m 1.获取和修改最大pid\033[0m" print "\033[32m 2.获取受保护的pid值\033[0m" print "\033[32m 3.获取ssh弱口令密码\033[0m" print "\033[32m 4.杀死不受保护的pid\033[0m" print "\033[32m 5.退出本程序的运行\033[0m" try: c =int(raw_input("请输入你要选择的序号:")) except: print "\033[31m your input is wrong \033[0m" continue if c == 1: max_pid() elif (c == 2): get_pid() elif ( c == 3 ): get_ip() elif (c == 4): kill_pid(pid_list) elif ( c == 5 ): exit() else: print "\033[31m your input is wrong \033[0m" if __name__ == '__main__': print("用的时候最好把汉语换成英语,避免出现乱码") menu()
原文地址:http://blog.51cto.com/tdcqvip/2121139