Please see this thread for reference

How can I scan using nmap and Zenmap all hostnames that begin with a particular string?

One of the answers in the thread above uses the following query (I take no credit at all for the command):

nmap -sL -oG - | awk '$3~/^(org/{print $2}' | nmap -iL -`

It scans all nodes for hostnames starting with org and returns a list of matching nodes.

It works great in a Unix/Linux environment, but I need an equivalent for Windows. I'd prefer not to use awk, sed, or grep packages for Windows. I'd like to maintain a standard and use PowerShell and/or Perl.

Is there an equivalent way to construct this query using PowerShell and/or Perl, along with nmap?

  • 4
    You've not accepted any answer on the original question. Did none of the answers work for that one? – jscott Mar 22 '13 at 19:36
  • Thank you. I just accepted it a moment ago, since it does work (very well) in linux/unix environments. But I need an equivalent for Windows-based systems. – user717236 Mar 22 '13 at 19:56

The Nmap parts of that command should work the same. To convert the awk command to Perl, use:

perl -lane "print $F[1] if $F[2]=~/^\(org/"

(Because of quoting differences between Windows cmd.exe and most *nix shells, *nix shells should use single quotes instead of double)

To do the same in PowerShell, this should work (not tested):

%{ if ($_.Split()[2] -match "^\(org") { $_.Split()[1]; } }
| improve this answer | |
  • Thank you very much. I appreciate it. The Perl version works great. But I'm getting an error in PowerShell: Bad argument to operator '-match': parsing "^(r" - Not Enough )'s.. – user717236 Mar 22 '13 at 20:49
  • 1
    I had to change the powershell version slightly to clear the error: %{ if ($_.Split()[2] -match "^\(org") { $_.Split()[1]; } }. I just added a backslash before the parenthesis in the regex and changed the array index in the output. Thank you for your help! – user717236 Mar 22 '13 at 20:56
  • 1
    @user717236 Thanks for the corrections. I've applied them in the answer above. – bonsaiviking Mar 22 '13 at 21:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.