c0d3dsk1lls

port_scanner.py

Jun 27th, 2022 (edited)
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.35 KB | None | 0 0
  1. import socket
  2. from utils import timefunc
  3.  
  4. class Scanner:
  5.     def __init__(self, ip):
  6.         self.ip = ip    
  7.         self.open_ports = [];          
  8.  #------------------------
  9.     def __repr__(self):
  10.         return 'Scanner: {}'.format(self.ip)
  11.        
  12.        
  13.     def add_port(self, port):
  14.         self.open_ports.append(port)
  15.        
  16.     def scan(self, lowerport, upperport):
  17.         for port in range(lowerport, upperport + 1):
  18.             if self.is_open(port):
  19.                 #print(port)
  20.                 self.add_port(port)
  21.                
  22.     def is_open(self, port):
  23.         s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  24.         result = s.connect_ex((self.ip, port))
  25.         #print('Port {}:        {}'.format(port, result)) # --PRINTS VERTICAL LIST OF PORTS--
  26.         s.close()
  27.         return result == 0
  28.                              
  29.     def write(self, filepath):
  30.         openport = map(str, self.open_ports) #--CONVERTS TO STRING--
  31.         with open(filepath, 'w') as f:
  32.             f.write('\n'.join(openport)) #--WRITES RESULT TO A FILE--          
  33.  #------------------------
  34. @timefunc
  35. def main():
  36.     ip = '192.168.10.666'
  37.     scanner = Scanner(ip)
  38.     scanner.scan(1, 65535)
  39.     scanner.write('./open_ports')
  40.     print(scanner.open_ports)
  41.    
  42.  #------------------------
  43. if __name__ == '__main__':
  44.     main()
  45.  
  46.  
Add Comment
Please, Sign In to add comment