portscan.pl | tiny nmap script

May 11, 2020 // echel0n

Lets say you have a connection and can run commands on the victim.

But the machine has no nmap or something to scan its network and it has perl installed, you can use this script


  1. #!/usr/bin/perl
  2. use IO::Socket;
  3. $| = 1;
  4. # change the target IP
  5. $target = "172.18.0.2";
  6. # least port to scan
  7. $start_port = "1";
  8. $end_port = "10000";
  9. foreach ($port = $start_port ; $port <= $end_port ; $port++)
  10. {
  11. #\r will refresh the line
  12. print "\rScanning port $port";
  13. #Connect to port number
  14. $socket = IO::Socket::INET->new(PeerAddr => $target , PeerPort => $port , Proto => 'tcp' , Timeout => 1);
  15. #Check connection
  16. if( $socket )
  17. {
  18. print "\r = Port $port is open.\n" ;
  19. }
  20. else
  21. {
  22. #Port is closed, nothing to print
  23. }
  24. }
  25. print "\n\nFinished Scanning $target\n";
  26. exit (0);