I'm not really proud of this but it's the first thing that came to mind and it
might work for you. Some of the elements (the %computername%, in particular)
may not work on Window 9x; some of the elements ({for /F}, in particular)
won't work in Windows 9x or NT. I'm sure you can make some simplifying
assumptions and consequent simplifications that will meet your needs and if
you're using Windows 2000, I think this will work unmodified.

The command {netstat -a} shows all IP connections and listening ports, e.g.,
if run on a VNC server that DOES NOT have a session established with a viewer,
it produces:

<snip>
 TCP    JRE-SRVR:1035           JRE-SRVR:0             LISTENING
 TCP    JRE-SRVR:5800           JRE-SRVR:0             LISTENING
 TCP    JRE-SRVR:5900           JRE-SRVR:0             LISTENING
 TCP    JRE-SRVR:11965          JRE-SRVR:0             LISTENING
<snip>

If run on a VNC server that DOES have a session established with a viewer,
{netstat -a} produces:

<snip>
 TCP    JRE-SRVR:1035          JRE-SRVR:0             LISTENING
 TCP    JRE-SRVR:5800          JRE-SRVR:0             LISTENING
 TCP    JRE-SRVR:5900          JRE-SRVR:0             LISTENING
 TCP    JRE-SRVR:11965         JRE-SRVR:0             LISTENING
<snip>
 TCP    JRE-SRVR:netbios-ssn   JRE-SRVR:0             LISTENING
 TCP    JRE-SRVR:5900          JRE-VIEW:1202          ESTABLISHED
 UDP    JRE-SRVR:epmap         *:*
<snip>

Notice the word "ESTABLISHED" appears when you have a session in progress.

If you pipe {netstat -a}'s output to a find command looking for the VNC port
(:5900) you'll get only the lines relevant to VNC, e.g., on a VNC server that
has a client connected to it, {netstat -a | find ":5900"} on a VNC server that
has a connection established produces:

 TCP    JRE-SRVR:5900          JRE-SRVR:0             LISTENING
 TCP    JRE-SRVR:5900          JRE-VIEW:1202          ESTABLISHED

If the VNC server were also acting as a viewer to another VNC server, you'd
get a line you are not really interested in:

 TCP    JRE-SRVR:5900          JRE-SRVR:0             LISTENING
 TCP    JRE-SRVR:1050          JRE-ANOTHER:5900       ESTABLISHED
 TCP    JRE-SRVR:5900          JRE-VIEW:1202          ESTABLISHED

To avoid that, you can change the command to {netstat -a | find /i
"%computername%:5900"} giving:

 TCP    JRE-SRVR:5900          JRE-SRVR:0             LISTENING
 TCP    JRE-SRVR:5900          JRE-VIEW:1202          ESTABLISHED

The {/i} is needed on the find command to make it case-insensitive because
{%computername%} and {netstat} aren't case consistent.

{find} sets %errorlevel% to "0" if it finds what it's looking for and to
something greater than "0" if it doesn't, so piping all that to {find
"ESTABLISHED"} will give a way to determine if the word "ESTABLISHED" appears.
{netstat -a | find /i "%computername%:5900" | find "ESTABLISHED"} gives

 TCP    JRE-SRVR:5900          JRE-VIEW:1202          ESTABLISHED

and sets %errorlevel% to "0" so a reasonable thing to do might be to follow
{netstat -a | find /i "%computername%:5900" | find "ESTABLISHED"} with

if not %errorlevel% 1 call what-to-do-when-connected.bat
if     %errorlevel% 1 call what-to-do-when-not-connected.bat

Remember that {if %errorlevel% n}  is true when %errorlevel% is equal to OR
GREATER THAN n.

There is one condition that I can think of that will befuddle this scheme ---
if the word "ESTABLISHED" appears somewhere in the name of one of the VNC
machines involved. Accounting for that is pretty straightforward. The {for}
command of Windows 2000 (but not NT or 9x) can parse a line and pick out the
token in the fourth column alone, ignoring the second and third where
"ESTABLISHED" would appear if it were part of a computer name. These commands
account for this possibility:

netstat -a | find /i "%computername%:5900" | find "ESTABLISHED" >
%temp%\delete.me
for /F "tokens=4" %s in (%temp%\delete.me) do echo %s | find "ESTABLISHED"
if not %errorlevel% 1 call what-to-do-when-connected.bat
if     %errorlevel% 1 call what-to-do-when-not-connected.bat
del %temp%\delete.me

You may or may not know that, in order to put the above into a batch file, you
need to double up the "%" in "%s" on the {for} command, i.e., the {for} line
needs to read

for /F "tokens=4" %%s in (%temp%\delete.me) do echo %%s | find "ESTABLISHED"

Cheers,
JRE

----- Original Message -----
From: "edlogic" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Saturday, December 15, 2001 1:57 PM
Subject: detecting a current connect


> winvnc -
>
> running server behind firewall at work to connect to viewer at home .
>
> from command prompt i can start the server and connect but i have no way to
> detect if a connection is still alive at the command prompt .
>
> also the windows modal dialogs get in the way when i get messages about the
> server already running etc .
>
> ----------
>
> i want my work computer to periodically attempt to connect to my home
> computer any time there is not an active connection .  that way any time i
> want control of my work computer - all i gotta do is start the listening
> viewer on my home machine .
>
> right now i have a batch file looping with a delay time and it stops and
> starts the server - this breaks and remakes the current connection ( if
> present ) .
>
> it works but is cumbersome .
>
> since i can't tell if there is a current connect i have to stop the server
> and restart .
> ---------------------------------------------------------------------
> To unsubscribe, mail [EMAIL PROTECTED] with the line:
> 'unsubscribe vnc-list' in the message BODY
> See also: http://www.uk.research.att.com/vnc/intouch.html
> ---------------------------------------------------------------------
---------------------------------------------------------------------
To unsubscribe, mail [EMAIL PROTECTED] with the line:
'unsubscribe vnc-list' in the message BODY
See also: http://www.uk.research.att.com/vnc/intouch.html
---------------------------------------------------------------------

Reply via email to