PDA

View Full Version : Does anybody know a "Single-EXE" command line port scanner?



Rudolf Thilo
27-Nov-2012, 11:58
Hi.

For scripting purposes (using autoit, to check the host up/ down and
port open/closed status for TCP ports, 3389 e.g.) I'm looking for a
command line tool, similar to nmap, but it should offer the following
options:

- Exit Code represents the scan result,
e.g. Exit code 0=up and port is open, 1=up + closed, 3=host down

- full functionality in *ONE SINGLE* EXE file (nmap needs a bunch of
files and pcap)

the 2nd one is the more important one, if I can pipe the output to a
file and analyse that one later (like possible for NMAP) it's fine.



The reason is, that all tools using the Win Net APIs seem to always
wait, until a timeout occurres after several seconds, no matter, what's
the timeout setting of that tool was set, e.g. 50ms.


Any suggestions appreciated, regards, Rudi.

malcolmlewis
27-Nov-2012, 14:21
On Tue 27 Nov 2012 10:58:09 AM CST, Rudolf Thilo wrote:


Hi.

For scripting purposes (using autoit, to check the host up/ down and
port open/closed status for TCP ports, 3389 e.g.) I'm looking for a
command line tool, similar to nmap, but it should offer the following
options:

- Exit Code represents the scan result,
e.g. Exit code 0=up and port is open, 1=up + closed, 3=host down

- full functionality in *ONE SINGLE* EXE file (nmap needs a bunch of
files and pcap)

the 2nd one is the more important one, if I can pipe the output to a
file and analyse that one later (like possible for NMAP) it's fine.



The reason is, that all tools using the Win Net APIs seem to always
wait, until a timeout occurres after several seconds, no matter, what's
the timeout setting of that tool was set, e.g. 50ms.


Any suggestions appreciated, regards, Rudi.


Hi
Portqry.exe command-line
utility: http://support.microsoft.com/default.aspx?scid=kb;en-us;310099
Version 2.0 is the current version by the looks...
http://www.softpedia.com/get/Network-Tools/Network-IP-Scanner/PortQry-Command-Line-Port-Scanner.shtml

--
Cheers Malcolm °¿° (Linux Counter #276890)
openSUSE 12.2 (x86_64) Kernel 3.4.11-2.16-desktop
up 3 days 13:28, 7 users, load average: 0.11, 0.09, 0.07
CPU Intel i5 CPU M520@2.40GHz | Intel Arrandale GPU

craig wilson
27-Nov-2012, 14:23
Why not just use AutoIt for everything?

The TCPConnect and PING should tell you if the port device is up.
For Up Status, you likely will want to use PING connect to other known
unblocked ports.

On 11/27/2012 5:58 AM, Rudolf Thilo wrote:
>
> Hi.
>
> For scripting purposes (using autoit, to check the host up/ down and
> port open/closed status for TCP ports, 3389 e.g.) I'm looking for a
> command line tool, similar to nmap, but it should offer the following
> options:
>
> - Exit Code represents the scan result,
> e.g. Exit code 0=up and port is open, 1=up + closed, 3=host down
>
> - full functionality in *ONE SINGLE* EXE file (nmap needs a bunch of
> files and pcap)
>
> the 2nd one is the more important one, if I can pipe the output to a
> file and analyse that one later (like possible for NMAP) it's fine.
>
>
>
> The reason is, that all tools using the Win Net APIs seem to always
> wait, until a timeout occurres after several seconds, no matter, what's
> the timeout setting of that tool was set, e.g. 50ms.
>
>
> Any suggestions appreciated, regards, Rudi.
>


--
Craig Wilson - MCNE, MCSE, CCNA
Novell Knowledge Partner

Novell does not officially monitor these forums.

Suggestions/Opinions/Statements made by me are solely my own.
These thoughts may not be shared by either Novell or any rational human.

Rudolf Thilo
27-Nov-2012, 14:42
Hi.

>Why not just use AutoIt for everything?

As I wrote, the tcptimeout() function doesn't help. It takes seconds
(per device) to get a "down". As long as everything is up, no issue,
but as soon as one, or worse, several devices / ports are down, the
script will render to be "un-usable".

same thing is fact for Microsoft's PortQry.exe. So I suspect, that this
is a constraint of the Windows Network APIs.



Regards, Rudi.

Rudolf Thilo
27-Nov-2012, 14:44
Hi.

>Portqry.exe command-line

same behaviour as the autoit buildin functions: when the port is closed
/ host is down, it takes several seconds, until the result is returned.

Thanks for your answer, regards, Rudi.

ab
27-Nov-2012, 15:10
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Out of curiosity, why does the NMAP option (with multiple files on
there) NOT work? I know you said it doesn't, but that seems like a
pretty arbitrary requirement and I'm sure there's something more
substantial than "just 'cuz" behind that, so perhaps mentioning it would
help find other alternatives.

Worst case nmap and pcap are both OSS... compile them as one big static
binary.

Good luck.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.19 (GNU/Linux)
Comment: Using GnuPG with undefined - http://www.enigmail.net/

iQIcBAEBAgAGBQJQtMnaAAoJEF+XTK08PnB5v30P/RcU0Wni18n6sJsxdZKS0hyS
VbzgZUY8s8qURyKmB+WeXKL1urW1Pw2auwiEWK1TpPT/rtVgaLXxeecu2xexLJrX
RSrKmlvb4UzLSVlGciI+m9pTKrwhE509OhgfqRCwmtAAdgY/QXDIq22B1qhoOrgZ
eoARKffhLAWD6E1h1yu+fmbU++XxvjlFOYdOl1gZk44hQdyFle hOoXY4flkOgBKO
K8u17muC0AJcwsWC9k5cpTulWmMB6bI0gsmyaXDo+Me3fY6RVe CpC/XTQKHnYLX7
l1moVsrSrM+ziR9KNeDSvOKm29VNYx1+s3C84CPddH2RgzNvvz DblyzyJOlZPJZd
wb1LOZK/nVAUfZPBUioK5Y3RechaPydtwPPKFwA85nbe50+o0bAiOXVmOj/PD7OC
DIasPBSLe/f7PsnIqCNYM8EWsKwwuUQ2sbF0DopRC3VJLSOhTofZxc8bCPzm mwVK
P+XLqqeLjPpCUaNK4oq8bagDu+Vfum7XtWDRz4TfyeVTvhQASp H+Rkav8PXGjvWI
CJP83312AqiIxGC9yBBsLy2ZD2Gx7NvH0NivUWQ5zW6thZLGfi AMoa4GHfcEF5kh
38yX0bbM9PKCXUtrgBF1mFNWD5KAuV+MJvAXu1LH8b5fCSp6TA fPgltbt2lqAwUC
xhQaCm/TSAIRo50WINBs
=johZ
-----END PGP SIGNATURE-----

craig wilson
27-Nov-2012, 15:13
While AutoIt is not really Multi-Threaded you could achieve that.

Create One AutoIt app that does all the testing and has the "Timeout"
issue and pipes its own results to a file or set of files.

Create a 2nd App that Calls the 1st App and tells it which IP(s) to scan
using the Run feature so as to not wait, but then loops and creates "n"
instances of the 1st app.

The 2nd app will also monitor the total number of copies of the 1st app
are running so that only X number are running since I suspect you do not
want hundreds but perhaps even 10 or 20 would do.

Not Elegant but would likely work.


On 11/27/2012 8:42 AM, Rudolf Thilo wrote:
> Hi.
>
>> Why not just use AutoIt for everything?
>
> As I wrote, the tcptimeout() function doesn't help. It takes seconds
> (per device) to get a "down". As long as everything is up, no issue,
> but as soon as one, or worse, several devices / ports are down, the
> script will render to be "un-usable".
>
> same thing is fact for Microsoft's PortQry.exe. So I suspect, that this
> is a constraint of the Windows Network APIs.
>
>
>
> Regards, Rudi.
>


--
Craig Wilson - MCNE, MCSE, CCNA
Novell Knowledge Partner

Novell does not officially monitor these forums.

Suggestions/Opinions/Statements made by me are solely my own.
These thoughts may not be shared by either Novell or any rational human.

Rudolf Thilo
27-Nov-2012, 16:02
Hi ab.

thanks for your reply.

>Out of curiosity, why does the NMAP option (with multiple files on
>there) NOT work?

OK, I'll describe my approach more detailed:

Situation:
===========
This is a company, that has ~20 retail outlet in several cities in
Germany, Austria, Swizerland.

Due to budget limitations a "real" load balancing solutions is not an
oportunity. (Citrix is on the list for years...)


So they work with several standard Terminal Servers (Win2008-R2-x64).
Currently the clients "outside" have an RDP definition file, that
arranges a "fix" combination "this PC" <-> "TS1/2/../n". Login name for
the TS session = hostname of the client PC. Host names are <city>01,
<city>02,...., <city>16


My idea:
=========
a default mapping, according to the PC numbers:
set1: PC1, PC5, PC9, PC13 -> TS1
set2: PC2, PC6, PC10, PC14 -> TS2
and so on.


Then check, if TS for "my set" is up and 3389 is open: Then connect.
Otherwise try the next set, with a wrap from the last set to set1.


Autoit, as well as portqry.exe and other tools, I've tried all have the
issue, that in case the host is down / port is closed, it takes several
seconds, until (the Win Net API?) timeout occures. Some of the tools
(including Autoit: Opt("TCPTimeout",50)) allow to define a timeout in
ms, but it doesn't seem to have any effect.

nmap would be fine, but it needs WinPcap, and several DLLs. I do not
want to install it on all the client PCs. Just using the Autoit
function "fileinstall("C:\...\nmap.exe",@tempdir & "\nmap.exe",1)
doesn't help, as nmap.exe is *NOT* a standalone EXE program. Beside
this, nmap doesn't provide exit codes, that reflect the result of a
single host + single port query (that's not the purpose, nmap was
written for)


My question:
=============
Does anybody know a way to do a *FAST* check for host / port status,
that can be (ab)used for this purpose?

- FAST! Not like Autoit functions, WMI calls,
- single exe or or DLL that could be called
- no local installation (like nmap winpcap)
- nice to have: Exit code, reflecting status. outtext to be analyzed,
like NMAP is doing, is acceptable.


I also tried to search a powershell solution, with no luck so far.
http://poshcode.org/2455 was promising, but here it returns for each
and every ip / port parameter pair "false".



Regards, Rudi.

Rudolf Thilo
27-Nov-2012, 16:13
Hi.

Thanks for your reply.

>Create One AutoIt app that does all the testing and has the "Timeout"
>issue and pipes its own results to a file or set of files.

[snip]

>The 2nd app will also monitor the total number of copies of the 1st
>app are running so that only X number are running since I suspect you
>do not want hundreds but perhaps even 10 or 20 would do.
>
>Not Elegant but would likely work.


that's quite close to the interim solution, I'm not happy with.

it's just one app, calling itself with "IP:TCP:PORT" of the TS server
to be checked next. When called with a param1 matching that syntax, the
up/down open/close status is checked and written as an entry in an INI
file.

--------- INI -----------
[TS-status]
TS1=3389
TS2=up
TS3=down
TS4=not checked so far
....
--------------------------


But I dislike this approach.


regards, Rudi.

malcolmlewis
27-Nov-2012, 16:19
On Tue 27 Nov 2012 03:02:48 PM CST, Rudolf Thilo wrote:

Hi ab.

thanks for your reply.

>Out of curiosity, why does the NMAP option (with multiple files on
>there) NOT work?

OK, I'll describe my approach more detailed:

Situation:
===========
This is a company, that has ~20 retail outlet in several cities in
Germany, Austria, Swizerland.

Due to budget limitations a "real" load balancing solutions is not an
oportunity. (Citrix is on the list for years...)


So they work with several standard Terminal Servers (Win2008-R2-x64).
Currently the clients "outside" have an RDP definition file, that
arranges a "fix" combination "this PC" <-> "TS1/2/../n". Login name for
the TS session = hostname of the client PC. Host names are <city>01,
<city>02,...., <city>16


My idea:
=========
a default mapping, according to the PC numbers:
set1: PC1, PC5, PC9, PC13 -> TS1
set2: PC2, PC6, PC10, PC14 -> TS2
and so on.


Then check, if TS for "my set" is up and 3389 is open: Then connect.
Otherwise try the next set, with a wrap from the last set to set1.


Autoit, as well as portqry.exe and other tools, I've tried all have the
issue, that in case the host is down / port is closed, it takes several
seconds, until (the Win Net API?) timeout occures. Some of the tools
(including Autoit: Opt("TCPTimeout",50)) allow to define a timeout in
ms, but it doesn't seem to have any effect.

nmap would be fine, but it needs WinPcap, and several DLLs. I do not
want to install it on all the client PCs. Just using the Autoit
function "fileinstall("C:\...\nmap.exe",@tempdir & "\nmap.exe",1)
doesn't help, as nmap.exe is *NOT* a standalone EXE program. Beside
this, nmap doesn't provide exit codes, that reflect the result of a
single host + single port query (that's not the purpose, nmap was
written for)


My question:
=============
Does anybody know a way to do a *FAST* check for host / port status,
that can be (ab)used for this purpose?

- FAST! Not like Autoit functions, WMI calls,
- single exe or or DLL that could be called
- no local installation (like nmap winpcap)
- nice to have: Exit code, reflecting status. outtext to be analyzed,
like NMAP is doing, is acceptable.


I also tried to search a powershell solution, with no luck so far.
http://poshcode.org/2455 was promising, but here it returns for each
and every ip / port parameter pair "false".



Regards, Rudi.


Hi
If it's just one port the just use telnet to check.....?

--
Cheers Malcolm °¿° (Linux Counter #276890)
openSUSE 12.2 (x86_64) Kernel 3.4.11-2.16-desktop
up 3 days 15:30, 7 users, load average: 0.08, 0.13, 0.10
CPU Intel i5 CPU M520@2.40GHz | Intel Arrandale GPU

ab
27-Nov-2012, 16:32
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

[resend]

Wildly guessing:

1. telnet still has the timeout issue like everything else.

2. Recent versions of the inferior OS on which he is stuck doesn't even
have telnet. Of course, maybe he's using XP which does have it, or
vista (hahahaha) which might, but seven didn't ship it and for that I
must give the evil empire credit.

Good luck.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.19 (GNU/Linux)
Comment: Using GnuPG with undefined - http://www.enigmail.net/

iQIcBAEBAgAGBQJQtNz/AAoJEF+XTK08PnB5rb8P/Ar90Hz0tk+aVDRbo2q9O7Uy
T6FO7lmZwx1JiUBryydEC6ZZLUAuZqZlkFfrqH79gDzwkm5b3y uohWsugg1fqUKT
SEwR6aq9R8LJbaObGC33f/Nzas+aeVFtOrG+bwEbbIORw5s5+QpREK+pTl4UQkNM
F1gWNDeSeh+PQg4gc3RzKxOu4LVVndxHEqlEapZLk2OCZHJUYe M+xQyNL0ARD7je
q81qU28+H3JVxpCrjQKKp9JUGYBenNlZwVOQNcBWs1dRbNXmlS tL8VurULJBRH2n
7pZRvn3F02NSVIKqHy/KGRIhz3R2+ovIft/xIW0PdobaZRgG5/nFWNvPpniiVtYb
O9y/G3kTqL0H8LuYmUmRPMOw/oPQ4hOGcUFIhQiwDl7R4LIRgvEpc0ql4LQrDKGX
VDeN9pb+8P0YJOdgstP3Nq6pYQDQwYa7RnQzEb+5GZ/sTrrOlH15KWO3+2Cp16GP
hSpxK976+1ua4lrwPpXDb6AB0p4UfdzlvqmRJPyHDS4pIp1WXA u3mfWZD079if0f
i97kO6PvLRQbgmn25vqNlYElmCqp5ahs5CxXPTeS3oLHdqfZ4B ZY/ia+M9xcqEzh
7YnG7yduzi01moTnkGvapsMykdfBuT/yOILlqlcXfX8k75Z9q3nTnwWAWjD4w+sI
y2yEdH2QVvvmnbvrxg47
=SFBS
-----END PGP SIGNATURE-----

malcolmlewis
27-Nov-2012, 17:06
On Tue 27 Nov 2012 03:32:15 PM CST, ab wrote:

Wildly guessing:

1. telnet still has the timeout issue like everything else.

2. Recent versions of the inferior OS on which he is stuck doesn't even
have telnet. Of course, maybe he's using XP which does have it, or
vista (hahahaha) which might, but seven didn't ship it and for that I
must give the evil empire credit.

Hi
Java and just modify the script for the port in question?
http://www.coderanch.com/t/573479/sockets/java/check-open-ports-remote-computer
or VB?
http://bytes.com/topic/visual-basic/answers/736956-how-check-ports-status-remote-server-using-vb-script

@ab PS, telnet is still there.... just not turned on ;)
http://thumbnails106.imagebam.com/22279/62641e222789379.jpg (http://www.imagebam.com/image/62641e222789379)


--
Cheers Malcolm °¿° (Linux Counter #276890)
openSUSE 12.2 (x86_64) Kernel 3.4.11-2.16-desktop
up 0:25, 3 users, load average: 0.01, 0.04, 0.09
CPU Intel i5 CPU M520@2.40GHz | Intel Arrandale GPU

Massimo Rosen
27-Nov-2012, 17:08
Aaron,

On 27.11.2012 16:32, ab wrote:
> 2. Recent versions of the inferior OS on which he is stuck doesn't even
> have telnet. Of course, maybe he's using XP which does have it, or
> vista (hahahaha) which might, but seven didn't ship it and for that I
> must give the evil empire credit.

Of course does Windows 7 ship with telnet, it's merely not installed by
default.

CU,
--
Massimo Rosen
Novell Knowledge Partner
No emails please!
http://www.cfc-it.de

n0m
28-Nov-2012, 14:26
Hi,

I just took a fast look at your problem. Tehre was a aproach with
telnet. You can modify the timing sessions in the registry of the
machine.
Take a look here
http://technet.microsoft.com/en-us/library/cc771020(v=ws.10).aspx

Bye
n0m

Rudolf Thilo
30-Nov-2012, 15:39
Hi

>If it's just one port the just use telnet to check.....?

Hm, that would be the opposite approach. If the port is closed,
Telnet's PID will die within one second. If it's PID is still "alive"
after one second, the port is open, and the remaining job will simply
be to kill that PID.

I'll give it a try,


thanks, Rudi.

Rudolf Thilo
30-Nov-2012, 15:44
Hi Massimo

>Of course does Windows 7 ship with telnet, it's merely not installed
>by default.

Both is unimportant: Telnet.exe is a standalone program, that can be
made available using Autoit's "Fileinstall()" function.

But the disadvantage is the fact, that Telnet will *ONLY* die within a
second, when

1.) the host ist up
2.) the port in question is closed (RST)


Regards, Rudi.

Rudolf Thilo
30-Nov-2012, 15:57
Hi.

Thanks for your reply.

these values seem to admin the telnet *SERVER*, not client?


But the approach, to admin the timeout value might be a good one:
Propably the default time out of the Win Network APIs is adjustable
somehow. I'll try to search in that direction.


Regards, Rudi.

Rudolf Thilo
30-Nov-2012, 16:34
Hi.

http://technet.microsoft.com/en-us/library/cc962453.aspx

looks like a good start, well, didn't find a value that would do the
trick to reduce the general timout period.

http://technet.microsoft.com/en-us/library/cc938217.aspx is the "Reuse
closed connection delay".


TcpMaxConnectRetransmissions and TcpInitialRTT might help, I'll check
that.



Hm.


Regards, Rudi.

malcolmlewis
30-Nov-2012, 16:38
On Fri 30 Nov 2012 03:34:56 PM CST, Rudolf Thilo wrote:

Hi.

http://technet.microsoft.com/en-us/library/cc962453.aspx

looks like a good start, well, didn't find a value that would do the
trick to reduce the general timout period.

http://technet.microsoft.com/en-us/library/cc938217.aspx is the "Reuse
closed connection delay".


TcpMaxConnectRetransmissions and TcpInitialRTT might help, I'll check
that.



Hm.


Regards, Rudi.


Hi
Not sure if you would want to deploy perl?

This works for sure (reduce the timeout to 1 second on line 54);
http://exchange.nagios.org/directory/Plugins/Network-Protocols/%2A-TCP-and-UDP-%28Generic%29/check_port-2Epl/details

--
Cheers Malcolm °¿° (Linux Counter #276890)
openSUSE 12.2 (x86_64) Kernel 3.4.11-2.16-desktop
up 3 days 0:15, 6 users, load average: 0.05, 0.09, 0.06
CPU Intel i5 CPU M520@2.40GHz | Intel Arrandale GPU

Hamish
03-Dec-2012, 20:03
On 11/27/2012 4:58 AM, Rudolf Thilo wrote:
>
> Hi.
>
> For scripting purposes (using autoit, to check the host up/ down and
> port open/closed status for TCP ports, 3389 e.g.) I'm looking for a
> command line tool, similar to nmap, but it should offer the following
> options:
>
> - Exit Code represents the scan result,
> e.g. Exit code 0=up and port is open, 1=up + closed, 3=host down
>
> - full functionality in *ONE SINGLE* EXE file (nmap needs a bunch of
> files and pcap)
>
> the 2nd one is the more important one, if I can pipe the output to a
> file and analyse that one later (like possible for NMAP) it's fine.
>
>
>
> The reason is, that all tools using the Win Net APIs seem to always
> wait, until a timeout occurres after several seconds, no matter, what's
> the timeout setting of that tool was set, e.g. 50ms.

I have a utility to check if a port is open, but it waits for the
timeout that you've run into. I'll see if I can re-do it as a
non-blocking socket - that should allow for user defined timneout.

H.

Rudolf Thilo
12-Dec-2012, 11:07
Hi.


>Not sure if you would want to deploy perl?

Nope, if you mean, if I want to deploy it on each and every client box.


>This works for sure (reduce the timeout to 1 second on line 54);
>http://exchange.nagios.org/directory/Plugins/Network-Protocols/%2A-TCP-and-UDP-%28Generic%29/check_port-2Epl/details


Is it possible to *COMPILE* perl scripts to a single EXE?

Regards, Rudi.

Rudolf Thilo
12-Dec-2012, 11:09
Hi Hamish,


>I have a utility to check if a port is open, but it waits for the
>timeout that you've run into. I'll see if I can re-do it as a
>non-blocking socket - that should allow for user defined timneout.


How are the chances for a user defined timeout?




Thanks for your reply, Rudi.

Haitch
13-Dec-2012, 02:43
On 12/12/2012 4:09 AM, Rudolf Thilo wrote:
> Hi Hamish,
>
>
>> I have a utility to check if a port is open, but it waits for the
>> timeout that you've run into. I'll see if I can re-do it as a
>> non-blocking socket - that should allow for user defined timneout.
>
>
> How are the chances for a user defined timeout?


Rudi,

user defined timeout was the whole idea :-) And it is working.

usage is:

chkport-ip x.x.x.x port timeout

where:
- x.x.x.x is the target IP address
- port is port to connect to.
- timeout is the time out in millisecs to wait.

so: chkport-ip 8.8.8.8 53 100

checks to see if it can connect to Googles DNS service in 100 msecs.
(BTW - it can, but at 10 msec it times out).

If you want to send me an email at hamish at haitch dot net with an
email address that can accept executables, I'll send it to you.

H.

Haitch
13-Dec-2012, 23:19
> usage is:
>
> chkport-ip x.x.x.x port timeout
>
> where:
> - x.x.x.x is the target IP address
> - port is port to connect to.
> - timeout is the time out in millisecs to wait.
>
> so: chkport-ip 8.8.8.8 53 100
>
> checks to see if it can connect to Googles DNS service in 100 msecs.

Can be downloaded from:

https://www.dropbox.com/s/gpn0vxc36whxqpn/chkport-ip.exe

H.

Rudolf Thilo
14-Dec-2012, 10:13
Hi Haitch,

that's exactly what I was looking for:

>https://www.dropbox.com/s/gpn0vxc36whxqpn/chkport-ip.exe

- single exe file
- adjustable timeout
- exit code reflecting the result.


Did you write that tiny, nice tool?

PERFECT! Thanks a lot,

regards, Rudi.

Haitch
14-Dec-2012, 13:30
Rudi,

> that's exactly what I was looking for:
>
>> https://www.dropbox.com/s/gpn0vxc36whxqpn/chkport-ip.exe
>
> - single exe file
> - adjustable timeout
> - exit code reflecting the result.

Glad you like it - if there are any tweaks you'd like, let me know.
>
> Did you write that tiny, nice tool?

Yep. In Pascal .... And one of these days I'll clean the code up and
port it over to the version that does name resolution rather than
requiring IP addresses.

>
> PERFECT! Thanks a lot,

You're welcome.

H.

Rudolf Thilo
18-Dec-2012, 15:15
Hi Hamish,

I'm wondering, whether I should drop a feature request at the
developers forum of Autoit, to implement "non-blocking sockets" as
well. As I have no clue, how you did that trick: Would you mind to
share the source, so that I could point them to your code to see, how
it's done / how much work this might be?

If that's OK for you...


One feature that might be nice would be the option to specify the
protocol as well, e.g. UDP:69, or icmpv4:8 [1 -> PING]

But this is just an idea, not something I currently need (and,
honestly, I have no clue at all, how much work these features might be)


Regards, Rudi.


[1]
for allowing incoming ICMP ECHO requests I found this netsh somewhere
in the web:

netsh advfirewall firewall add rule name="ICMP Allow incoming V4 echo
request" protocol=icmpv4:8,any dir=in action=allow

Haitch
19-Dec-2012, 01:33
On 12/18/2012 8:15 AM, Rudolf Thilo wrote:

> Hi Hamish,
>
> I'm wondering, whether I should drop a feature request at the
> developers forum of Autoit, to implement "non-blocking sockets" as
> well. As I have no clue, how you did that trick: Would you mind to
> share the source, so that I could point them to your code to see, how
> it's done / how much work this might be?

The code is quick and dirty, but you/they are welcome to it. It's below,
in it's entirety, at the end of this post.

> One feature that might be nice would be the option to specify the
> protocol as well, e.g. UDP:69, or icmpv4:8 [1 -> PING]

UDP I could add fairly easily, icmp would require using raw sockets
(which I've no idea on and no references to look at, but will see what I
can find), or possibly do-able using the icmp.dll - I'll look into it
when I have time.

> But this is just an idea, not something I currently need (and,
> honestly, I have no clue at all, how much work these features might be)

H.

program chkPort;
// Input: chkport-ip <ip address> <port> [timeout]
// Output: Text message + Errorlevel
// 0 if we connect,
// 1 if we timeout,
// 2 if we get a socket failure,
// 3 for an invalid address.

uses sockets,inetaux,strutils,winsock2,sysutils;
var
ConSock : LongInt;
sAddr : TInetSockAddr;
timeout : dword;
result : boolean;

procedure setnonblockingsocket( s : integer );
var
nb : dword;
begin
nb := 1; // 1 = nonblocking, 0 = blocking
winsock2.ioctlsocket( s , FIONBIO , @nb );
end;

function is_writable_socket( sck : integer; timeout : dword ) : boolean;
var
fds : tfdset;
tv : timeval;
result : boolean;
begin
fd_zero( fds );
fd_set( sck , fds );
tv.tv_sec := timeout div 1000;
tv.tv_usec := timeout mod 1000;

// select (socket+1 , read , write , except , timeout) - wait
// "timeout" to see if data can be written to "socket" without
// blocking

result := select( sck + 1 , nil , @fds , nil , @tv ) > 0;
is_writable_socket := result;
end;

begin
// create a socket, die if we fail
ConSock := Socket(af_inet, sock_stream, 0);
if ConSock = -1 then begin
writeln('Could not open socket: ');
halt(2);
end
else begin

// make it non-blocking
SetNonBlockingSocket(ConSock);

// if timeout defined use it, otherwise default to 2 secs
If paramcount >= 3 then Timeout := numb2dec(paramstr(3),10)
else timeout := 2000;

// fill in the socket info - protocol, address, port
with sAddr do begin
Family := af_inet;
Port := htons(numb2dec(paramstr(2),10));
Addr := StrToAddr(paramstr(1));
end;

// die if we can't turn the address into something useable
If saddr.Addr = 0 then begin
writeln('Could not resolve ',paramstr(1));
halt(3);
end;

// otherwise try to connect
Result := (fpConnect(ConSock, @sAddr, sizeof(sAddr)) = 0);

// and see if it become writeable in the time allowed
result := result or is_writable_socket(consock, timeout);

// close the socket
Shutdown(ConSock, 2);

// and report results
If result then writeln('Port listening')
else begin
Writeln('Port not listening');
halt(1);
end;
end;
end.

mf_ncc
04-Jan-2013, 11:48
Microsoft

portqry.exe

rgds

On 27/11/12 10:58, Rudolf Thilo wrote:
>
> Hi.
>
> For scripting purposes (using autoit, to check the host up/ down and
> port open/closed status for TCP ports, 3389 e.g.) I'm looking for a
> command line tool, similar to nmap, but it should offer the following
> options:
>
> - Exit Code represents the scan result,
> e.g. Exit code 0=up and port is open, 1=up + closed, 3=host down
>
> - full functionality in *ONE SINGLE* EXE file (nmap needs a bunch of
> files and pcap)
>
> the 2nd one is the more important one, if I can pipe the output to a
> file and analyse that one later (like possible for NMAP) it's fine.
>
>
>
> The reason is, that all tools using the Win Net APIs seem to always
> wait, until a timeout occurres after several seconds, no matter, what's
> the timeout setting of that tool was set, e.g. 50ms.
>
>
> Any suggestions appreciated, regards, Rudi.
>

Haitch
06-Jan-2013, 06:12
On 1/4/2013 4:48 AM, mf_ncc wrote:
> Microsoft
>
> portqry.exe
>
> rgds

It has the same issue as the other tools - it waits on the windows
timeout - Rudi's requirements are for a non-blocking connect attempt
that can be timed out on a user definable basis.

H.