Today, I saw a group of friends sent a demand, as follows:
"How to batch detect the port of a number of hosts, whether there is, the port is external", it is not difficult to write a small script with py, the problem of the place, but also hope you point out, thank you!
#!/usr/bin/env pythonimport socketfile = "C:\Users\Administrator\py_demo\ip.txt" port = 80a = open (file, ' R ') B = a.readlines () A.close () For i in b: ip = i.strip () try: sk = socket.socket (Socket.af_inet, socket. SOCK_STREAM) sk.settimeout (2) sk.connect ((ip,port)) sk.close () print "Connect", ip,port, "Success." except socket.error: print "Connect",ip, port, "faild!"
The following effects are performed:
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/45/A8/wKioL1Ppc0jAKN7YAAEx4aVe7mw151.jpg "title=" 1.jpg " alt= "Wkiol1ppc0jakn7yaaex4ave7mw151.jpg"/>
The script mainly uses the Python socket module and the text reading and so on knowledge content, what needs to revise the place, everybody exchanges together, thanks.
This article comes from "My future is not a dream." "Blog, be sure to keep this provenance http://song49.blog.51cto.com/4480450/1538855