今天小编就为大家分享一篇使用Python测试Ping主机IP和某端口是否开放的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
使用Python方法
比用各种命令方便,可以设置超时时间,到底通不通,端口是否开放一眼能看出来。
命令和返回
完整权限,可以ping通,端口开放,结果如下:
无root权限(省略了ping),端口开放,结果如下:
完整权限,可以ping通,远端端口关闭,结果如下:
完整权限,可以ping通,本地端口关闭,结果如下:
完整权限,不能ping通(端口自然也无法访问),结果如下:
pnp.py代码
#!/usr/bin/python #name pnp.py #ping and port #coding:utf-8 import os, sys, socket, struct, select, time ICMP_ECHO_REQUEST = 8 # Seems to be the same on Solaris. socket.setdefaulttimeout(4) #first argument host=sys.argv[1] #second argument port=int(sys.argv[2]) #socket try connect def PortOpen(ip,port): print( '\033[1m*Port\033[0m %s:%d' %(ip,port)), s=socket.socket(socket.AF_INET,socket.SOCK_STREAM) try: s.connect((ip,port)) s.shutdown(2) print( '\033[1;32m.... is OK.\033[0m' ) return True except socket.timeout: print( '\033[1;33m.... is down or network time out!!!\033[0m' ) return False except: print( '\033[1;31m.... is down!!!\033[0m' ) return False def checksum(source_string): """ I'm not too confident that this is right but testing seems to suggest that it gives the same answers as in_cksum in ping.c """ sum = 0 countTo = (len(source_string)/2)*2 count = 0 while count<countto: thisval + 1])*256 ord(source_string[count]) sum & 0xffffffff # necessary? count=count 2 if countto> 16) + (sum & 0xffff) sum = sum + (sum >> 16) answer = ~sum answer = answer & 0xffff # Swap bytes. Bugger me if I know why. answer = answer >> 8 | (answer << 8 & 0xff00) return answer def receive_one_ping(my_socket, ID, timeout): """ receive the ping from the socket. """ timeLeft = timeout while True: startedSelect = time.time() whatReady = select.select([my_socket], [], [], timeLeft) howLongInSelect = (time.time() - startedSelect) if whatReady[0] == []: # Timeout return timeReceived = 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 - timeSent timeLeft = timeLeft - howLongInSelect if timeLeft dest_addrcou<span style="color:transparent">来源gaodai#ma#com搞*!代#%^码网</span>ntdest_addrtimeout</div><p><strong>使用命令方法</strong></p><p>使用命令ping就不说了,端口可以用下面的命令。</p><p>当时目前telnet基本不用,可能没有telnet客户端了。</p><p>测试通常连接不上会等很久,端口连上了也需要通过反馈内容自行判断。</p><p><strong>telnet</strong></p><div class="gaodaimacode"><pre class="prettyprint linenums"> telnet ip port
$telnet 192.168.234.1 Trying 192.168.234.1... Connected to 192.168.234.1. Escape character is '^]'. ......
wget
wget 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” ......
SSH
ssh -v ip -p port
$ssh -v 192.168.234.1 -p 21 OpenSSH_7.4p1, OpenSSL 1.0.2k-fips 26 Jan 2017 debug1: Reading configuration data /etc/ssh/ssh_config debug1: /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. ......
curl
culr ip:port
$curl 192.168.234.1:21 220 Serv-U FTP Server v15.1 ready... 530 Not logged in. ......
以上这篇使用Python测试Ping主机IP和某端口是否开放的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持gaodaima搞代码网。
以上就是使用Python测试Ping主机IP和某端口是否开放的实例的详细内容,更多请关注gaodaima搞代码网其它相关文章!