大淘客网站上的推广怎么做,开发公司办出项目不动产证纪实,一站式做网站设计,哪类网站流量大使用Python方法比用各种命令方便#xff0c;可以设置超时时间#xff0c;到底通不通#xff0c;端口是否开放一眼能看出来。命令和返回完整权限#xff0c;可以ping通#xff0c;端口开放#xff0c;结果如下#xff1a;无root权限(省略了ping)#xff0c;端口开放可以设置超时时间到底通不通端口是否开放一眼能看出来。命令和返回完整权限可以ping通端口开放结果如下无root权限(省略了ping)端口开放结果如下完整权限可以ping通远端端口关闭结果如下完整权限可以ping通本地端口关闭结果如下完整权限不能ping通(端口自然也无法访问)结果如下pnp.py代码#!/usr/bin/python#name pnp.py#ping and port#coding:utf-8import os, sys, socket, struct, select, timeICMP_ECHO_REQUEST 8 # Seems to be the same on Solaris.socket.setdefaulttimeout(4)#first argumenthostsys.argv[1]#second argumentportint(sys.argv[2])#socket try connectdef PortOpen(ip,port):print( 33[1m*Port 33[0m %s:%d %(ip,port)),ssocket.socket(socket.AF_INET,socket.SOCK_STREAM)try:s.connect((ip,port))s.shutdown(2)print( 33[1;32m.... is OK. 33[0m )return Trueexcept socket.timeout:print( 33[1;33m.... is down or network time out!!! 33[0m )return Falseexcept:print( 33[1;31m.... is down!!! 33[0m )return Falsedef checksum(source_string):Im not too confident that this is right but testing seemsto suggest that it gives the same answers as in_cksum in ping.csum 0countTo (len(source_string)/2)*2count 0while countthisVal ord(source_string[count 1])*256 ord(source_string[count])sum sum thisValsum sum 0xffffffff # Necessary?count count 2if countTosum sum ord(source_string[len(source_string) - 1])sum sum 0xffffffff # Necessary?sum (sum 16) (sum 0xffff)sum sum (sum 16)answer ~sumanswer answer 0xffff# Swap bytes. Bugger me if I know why.answer answer 8 | (answer 8 0xff00)return answerdef receive_one_ping(my_socket, ID, timeout):receive the ping from the socket.timeLeft timeoutwhile True:startedSelect time.time()whatReady select.select([my_socket], [], [], timeLeft)howLongInSelect (time.time() - startedSelect)if whatReady[0] []: # TimeoutreturntimeReceived time.time()recPacket, addr my_socket.recvfrom(1024)icmpHeader recPacket[20:28]type, code, checksum, packetID, sequence struct.unpack(bbHHh, icmpHeader)if packetID ID:bytesInDouble struct.calcsize(d)timeSent struct.unpack(d, recPacket[28:28 bytesInDouble])[0]return timeReceived - timeSenttimeLeft timeLeft - howLongInSelectif timeLeft 0:returndef send_one_ping(my_socket, dest_addr, ID):Send one ping to the given dest_addr.dest_addr socket.gethostbyname(dest_addr)# Header is type (8), code (8), checksum (16), id (16), sequence (16)my_checksum 0# Make a dummy heder with a 0 checksum.header struct.pack(bbHHh, ICMP_ECHO_REQUEST, 0, my_checksum, ID, 1)#a1 struct.unpack(bbHHh,header) #my testbytesInDouble struct.calcsize(d)data (192 - bytesInDouble) * Qdata struct.pack(d, time.time()) data# Calculate the checksum on the data and the dummy header.my_checksum checksum(header data)# Now that we have the right checksum, we put that in. Its just easier# to make up a new header than to stuff it into the dummy.header struct.pack(bbHHh, ICMP_ECHO_REQUEST, 0, socket.htons(my_checksum), ID, 1)packet header datamy_socket.sendto(packet, (dest_addr, 1)) # Dont know about the 1def do_one(dest_addr, timeout):Returns either the delay (in seconds) or none on timeout.delayNoneicmp socket.getprotobyname(icmp)try:my_socket socket.socket(socket.AF_INET, socket.SOCK_RAW, icmp)my_ID os.getpid() 0xFFFFsend_one_ping(my_socket, dest_addr, my_ID)delay receive_one_ping(my_socket, my_ID, timeout)my_socket.close()except socket.error, (errno, msg):if errno 1:# Operation not permittedmsg msg ( - not root.)raise socket.error(msg)#raise # raise the original errorreturn delaydef verbose_ping(dest_addr, timeout 2, count 100):Send count ping to dest_addr with the given timeout and displaythe result.for i in xrange(count):print 33[1m*Ping 33[0m %s ... % dest_addr,try:delay do_one(dest_addr, timeout)except socket.error, e:print 33[1;31m... failed. (%s) % ebreakif delay None:print 33[1;31m... failed. (timeout within %ssec.) 33[0m % timeoutelse:delay delay * 1000print 33[1;32m... get ping in %0.4fms 33[0m % delayif __name__ __main__:if os.geteuid() 0:verbose_ping(host,2,3)else:print 33[1m*Ping 33[0m test must be sudo or root...PortOpen(host,port)print( Job finished.)使用命令方法使用命令ping就不说了端口可以用下面的命令。当时目前telnet基本不用可能没有telnet客户端了。测试通常连接不上会等很久端口连上了也需要通过反馈内容自行判断。telnettelnet ip port$telnet 192.168.234.1Trying 192.168.234.1...Connected to 192.168.234.1.Escape character is ^].......wgetwget ip:port$wget 192.168.234.1:21--2019-03-22 15:42:27-- http://192.168.234.1:21/正在连接 192.168.234.1:21... 已连接。已发出 HTTP 请求正在等待回应... 200 没有 HTTP 头尝试 HTTP/0.9长度未指定正在保存至: “index.html”......SSHssh -v ip -p port$ssh -v 192.168.234.1 -p 21OpenSSH_7.4p1, OpenSSL 1.0.2k-fips 26 Jan 2017debug1: Reading configuration data /etc/ssh/ssh_configdebug1: /etc/ssh/ssh_config line 58: Applying options for *debug1: Connecting to 192.168.234.1 [192.168.234.1] port 21.debug1: Connection established.......curlculr ip:port$curl 192.168.234.1:21220 Serv-U FTP Server v15.1 ready...530 Not logged in.......以上这篇使用Python测试Ping主机IP和某端口是否开放的实例就是小编分享给大家的全部内容了希望能给大家一个参考也希望大家多多支持云海天教程。