check server port
来源:互联网 发布:淘宝违禁品有哪些 编辑:程序博客网 时间:2024/05/18 00:51
#!/bin/env python#coding:utf-8#Author:wankun603@pingan.com.cnimport socketimport reimport sys def check_server(address, port): socket.setdefaulttimeout(3) s = socket.socket() res = s.connect_ex((address, port)) print "connect "+address+":"+ str(port)+ " status : "+ ("success" if res==0 else "error") return res if __name__ == '__main__': if len(sys.argv)!=2: print "Usage :[m"+sys.argv[0]+" [port] exit() port = int(sys.argv[1]) servers = [] try: ipsfile = open('ips','r') ips = ipsfile.readlines() for ip in ips: ip = ip.strip() res = check_server(ip,port) if res==0: servers.append(ip) print "find alive servers" for ip in servers: print ip except IOError: pass
stool.py
#!/bin/env python#coding:utf-8#Author:wankun603@pingan.com.cnimport osimport sysimport difflibimport paramikoimport threadingimport tracebackimport socketimport Queuedef check_port(ip,port): s = socket.socket() res = s.connect_ex((ip,port)) print "connect "+address+":"+ str(port)+ " status : "+ ("success" if res==0 else "error") q.put((res,ip)) return resdef sput(ip,username,password,localpath, remotepath,port=22): print "************* NODE SYNC : %s:%s******************" %(ip,localpath) t = paramiko.Transport((ip,port)) try: t.connect(hostkey=None, username=username, password=password) sftp = paramiko.SFTPClient.from_transport(t) sftp.put(localpath,remotepath) q.put((0,ip)) except: q.put((1,ip)) finally: t.close()def sget(ip,username,password,remotepath, localpath,port=22): t = paramiko.Transport((ip,port)) try: t.connect(hostkey=None, username=username, password=password) sftp = paramiko.SFTPClient.from_transport(t) sftp.get(remotepath,localpath) finally: t.close()def diff_file(ip,username,password,filepath): filepath = os.path.abspath(filepath) dir,filename = os.path.split(filepath) tmppath = os.path.join('/tmp',filename) print "************* NODE : %s ******************" %(ip) try: sget(ip,username,password,filepath,tmppath) # ndiff context_diff unified_diff diff = difflib.unified_diff( open(filepath, 'rU').readlines(), open(tmppath, 'rU').readlines() ) sys.stdout.writelines(diff) q.put((0,ip)) except IOError,e: traceback.print_exc() q.put((1,ip)) print '\n'def run_cmd(ip,username,password,cmd,port=22): out = ["************* NODE COMMAND : %s %s\n" %(ip,cmd)] ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) try: ssh.connect(ip,port,username,password,timeout=5) for m in cmd: stdin, stdout, stderr = ssh.exec_command(m) # stdin.write("Y") #简单交互,输入 ‘Y’ out.extend(stdout.readlines()) out_stderr = stderr.readlines() if len(out_stderr): out.extend("STDERR:\n") out.extend(out_stderr) #for o in out: # print o, out.append("************* NODE : %s STATUS OK******************\n" %(ip)) q.put((0,ip)) except Exception,ex: out.append("************* NODE : %s STATUS ERROR******************\n" %(ip)) traceback.print_exc() q.put((1,ip)) finally: ssh.close() sys.stdout.writelines(out)q = Queue.Queue()if __name__=='__main__': if len(sys.argv)<4: print "Usage :"+sys.argv[0]+"[diff|sync|cmd] [nodesfile] [filepath|command]" exit() optype = sys.argv[1] nodesfile = sys.argv[2] if optype not in ['diff','sync','cmd']: print 'op type does not match' exit() username='hadoop' #password='P@ssw0rd' password='Paic1234' try: nodes = open(nodesfile,'r').readlines() threads = list() for node in nodes: node = node.strip() if optype == 'diff': filepath = sys.argv[3] diff_file(node,username,password,filepath) elif optype == 'sync': filepath = sys.argv[3] a=threading.Thread(target=sput,args=(node,username,password,filepath,filepath)) a.start() threads.append(a) elif optype == 'cmd': cmd = [ ' '.join(sys.argv[3:]) ] a=threading.Thread(target=run_cmd,args=(node,username,password,cmd)) a.start() threads.append(a) for a in threads: a.join() allres0 = list() allres1 = list() while not q.empty(): res = q.get() if res[0]==0: allres0.append(res[1]) else: allres1.append(res[1]) print "***********************************************" print "SUCCESS : \n","\n".join(allres0) print "ERROR : \n","\n".join(allres1) except Exception,ex: #print 'Exception:',ex traceback.print_exc() pass
0 0
- check server port
- How to check port number is open for a windows server
- check localhost port in use
- Bash check IP and port
- linux check process and port relation
- linux Server check
- how to check which process occupied the port
- how to check port number and kill process with the port number
- check smpt server mail queue
- Powershell check server patch status
- Mac port 安装postgres92 + postgres92-server
- sql server port 1433 vs 1434
- 测试server是否block port或者网站
- port
- port
- port
- port
- [sql server] Automating SQL Server Health Check (SQL Server 2005)
- op2-Install ArchLinux
- 12306验证码太难,试着用php自己做一个验证码吧!
- 利用Servlet实现表单回填
- Python程序员最常犯的十个错误
- 重要数据的加密处理,比如密码的加密,需要用盐多次加密
- check server port
- java 初始化过程
- sql trim()函数去掉两头空格
- 杨辉三角
- 背包问题
- 个人学习网站的记录
- 5.1.3. Functional Programming Tools(函数式编程工具)
- makefile教程
- Flipper