1
votes
#!/usr/bin/python

import socket
import argparse
import os.path

parser = argparse.ArgumentParser(description='Scan for open ports on given 
hostname or ip address')
parser.add_argument('-H','--HOST', help ="Enter a hostname or Ip Address")
args = parser.parse_args()

    def find_port(PORT,HOST):
        data = ('\r\nSuccessful Connections were made at host {} on port {}'.format(HOST, PORT))
        originalfile = ("Data Log.txt")

       s = socket.socket()
       socket.setdefaulttimeout(1)

        try:
           s.connect((HOST, PORT))
           print '[+] Successful connection on ',PORT

        except:
           print '[+] Connection failure on port ',PORT

       if os.path.exists(originalfile) is True:
           if s.connect((HOST, PORT)):
               with open (originalfile, "w") as currentfile:
                    currentfile.write(data)
                    currentfile.close()

       elif os.path.exists(originalfile) is False:
            if s.connect((HOST, PORT)):
               with open("Data Log.txt","w+") as newfile:
                    newfile.write(data)
                    newfile.close()


    if __name__=='__main__':
       for i in range(1024):
            find_port(i, args.HOST)

i keep getting these errors. All im trying to do is capture the instance of a successful connection on the port and for it to log it inside of a text file but i keep getting these errors.

[+] Connection failure on port 0 Traceback (most recent call last): File "./poo2.py", line 44, in find_port(i, args.HOST) File "./poo2.py", line 30, in find_port if s.connect((HOST, PORT)): File "/usr/lib/python2.7/socket.py", line 228, in meth return getattr(self._sock,name)(*args) socket.error: [Errno 99] Cannot assign requested address

1

1 Answers

0
votes

It is not clear what you are trying to achieve with your code. But, you are creating first a socket, then connect it to the destination and then you try to connect it again. This second connect makes no sense and will cause the error since a socket can only be connected once.