Monday, August 20, 2012

IP and Port Scanning From Windows Command-line

Ever need an IP or port scanner but didn't have one installed or the permission to install one?  Here's a quick trick I came up with using the little used built-in functions of the Windows Command-line:

for /L %A in (1,1,254) do ping -n 1 192.168.1.%A
FOR /L %A IN (1,1,254) DO FOR /L %B IN (1,1,1024) DO telnet 192.168.1.%A %B

Windows has a built-in for loop function and when used with the /L switch, it will act like a traditional counting for loop as in C and other program languages.

The code loops through values from 1 to 254, incrementing by 1 and pings 192.168.1.%A, where %A is the value of the loop variable.

The second line of code uses nested loops to telnet to IP addresses in the same range as above and port numbers from 1 to 1024 inclusive.  There is a hitch where if it connects it just hangs there, but you can only work with what you got.

Here's a sample run:

IP Scanner

Port Scanner

5 comments:

  1. Nice, thanks.
    One remark: the A variable need two %

    for /L %%A in (1,1,254) do ping -n 1 192.168.1.%%A

    ReplyDelete
  2. I tried your code and got the following error:

    %%A was unexpected at this time.

    ReplyDelete
  3. Two percent symbols are used inside of batch files. If you're directly typing the command into the command prompt, you only use one.

    % = command prompt

    %% = batch file.

    ReplyDelete