-2

this is a security test in Python. Using telnet to connect to a public IP/Port but my program runs so slow it won't complete. I even set timeout to 1 second. How can I speed it up?

This is my code:


import telnetlib

# long list of IPs
ips = ["xx.81.56.xxx", "xx.81.56.xxx"]

# long list of ports
ports = ["22", "10800" , "4422"]

connections = {}

for ip in ips:
    for port in ports:
        try:
            connections[ip, port] = isinstance(telnetlib.Telnet(ip, port, 1), telnetlib.Telnet)
        except:
            connections[ip, port] = False

Thank you!

user1047260
  • 91
  • 1
  • 11
  • 2
    What do you mean by: "*my program runs so slow it won't complete*". what **exactly** is preventing it from completing? Can you provide the lengths of the 2 lists? and you want to test **each** *IP* with **each** port? How long does it take for **one** such connection to fail? One second? What would you expect? You could try parallelizing, but I'm not sure whether one attempt doesn't keep resources busy. – CristiFati Mar 23 '19 at 00:09
  • 1
    Trying things one at a time is just not the right approach if you have a large number of things to test that each might take a significant amount of time. How long is the list of IPs? How long is the list of ports? – David Schwartz Mar 23 '19 at 00:11
  • You could add *print* statements in the inner loop to see program's progress, or at least add some code so that the user is able to gracefully break out of the loop and check the partial results (without having to go through *KeyboardInterrupt*).. – CristiFati Mar 23 '19 at 00:15
  • 1
    I'm also a bit puzzled why you call this a "telnet program" and why you are using telnetlib. Is the purpose of this program just to see which IP/port combinations you can connect to? Because that doesn't have much to do with telnet and that's probably taking you afield. What's the actual purpose of this program? – David Schwartz Mar 23 '19 at 00:17
  • 1
    When would `telnetlib.Telnet` *not* return an instance of `Telnet`? – chepner Mar 23 '19 at 00:35
  • It sounds like you're trying to nmap the internet. Get a bigger pipe. – Pedro Rodrigues Mar 23 '19 at 01:10

1 Answers1

0

I got it to speed up with these steps:

  1. Using Nmap to reduce my list of ports. Mapped each IP to limited list of open ports.
  2. Shorten telnet timeont to .01 seconds => telnetlib.Telnet(ip, port, .01)

Thank you all for the advice and comments!

user1047260
  • 91
  • 1
  • 11