Close Application directly after disconnect (not in command mode)

Advertisement

Niko Nik
Joined:
Posts:
6

Close Application directly after disconnect (not in command mode)

Hello,
I'm looking for a way to disable/hide the "WinSCP Login" dialog box. Background: I'm an administrator of several windows terminal servers from which we connect to a lot of destinations. We start WinSCP from a list with the following command:
WinSCP.exe {UserName}:{Password}@{VPN_ADDRESS} /log="{LOGPATH}\{USERNAME}.log"
After "Disconnect" shows up the "WinSCP Login" dialog box, but I want to close the application directly after disconnect(to guide the users more directly).
I'm seacherd through the forum but I could not find a helpful information but it could be I overlooked something... sorry then.

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
31,405
Location:
Prague, Czechia

Re: Close Application directly after disconnect (not in command mode)

There's no such option.
Why don't you close WinSCP straight away, instead of disconnecting only?

Reply with quote

Niko Nik
Joined:
Posts:
6

Close Application directly after disconnect (not in command mode)

Thanks for the answer.
The reason for the question is because we use a wrapper application from which the users start WinSCP and which logged this start in database. If the user can connect by itselef there is no log entry and so I would prefer if the programn will be terminated.

Reply with quote

Niko Nik
Joined:
Posts:
6

Close Application directly after disconnect (not in command mode)

Which brings me to the next question: how can I close the program after a disconnect as you suggested?

Reply with quote

A.E.
Guest

Re: Close Application directly after disconnect (not in command mode)

Niko Nik wrote:

Which brings me to the next question: how can I close the program after a disconnect as you suggested?

One way to do this is to run the following windows command:
IF EXIST %SystemRoot%\System32\wbem\wmic.exe IF EXIST %SystemRoot%\System32\taskkill.exe IF EXIST %SystemRoot%\System32\findstr.exe %SystemRoot%\System32\wbem\wmic.exe process get name, processid|for /F "tokens=2" %i in ('%SystemRoot%\System32\findstr.exe /B /I "WinSCP"') do @%SystemRoot%\System32\taskkill.exe /F /PID %i

Or if you put it in an batch file, and call the batch file, then in the batch file double the percent signs in the for command:
IF EXIST %SystemRoot%\System32\wbem\wmic.exe IF EXIST %SystemRoot%\System32\taskkill.exe IF EXIST %SystemRoot%\System32\findstr.exe %SystemRoot%\System32\wbem\wmic.exe process get name, processid|for /F "tokens=2" %%i in ('%SystemRoot%\System32\findstr.exe /B /I "WinSCP"') do @%SystemRoot%\System32\taskkill.exe /F /PID %%i

This will:
- check to make sure wmic, taskkill, and findstr commands are available, and if they are
-- then to use wmic to get the process name and process id of all processes, and
-- then it passes the results from wmic to the FOR command to get the second token which in this case is the process id
--- it bases the FOR results on the findstr command that makes sure FOR only works on the lines that have WinSCP in them (ie. only the process id for WinSCP)
---- it then passes the process id to taskkill and forces it to kill the program (as long as the user has rights to force the kill on the process id).
---- Note: This will close all open instances of WinSCP.

That should work.

Reply with quote

Advertisement

A.E.
Guest

Re: Close Application directly after disconnect (not in command mode)

Niko Nik wrote:

Which brings me to the next question: how can I close the program after a disconnect as you suggested?

My above suggestion made the following assumption: That it was an application with a wrapper that could call a program before or after the application run, and that it would be able to check the database to make sure the machine in question has actually disconnected first before running the script (ie. such as a backup program, or active directory, running a script before or after a certain condition).

However, if you are calling WinSCP from a custom developed application, you could adjust the code to do pretty much the same thing. Have the code check the database for when the disconnect happens for the machine - since you mentioned that you are recording this in a database, and then use the kill or process close function that you used to call the process for WinSCP. For instance, something like this could work (although I am sure there are more elegant solutions out there):
C#: https://stackoverflow.com/questions/3345363/kill-some-processes-by-exe-file-name

PHP: use $proc=proc_open() to get the process you are calling; then use $proc_info=proc_status($proc) to get the process id on $proc_info['pid']; and the use either $exit_code=proc_close($proc) or $terminate_code=proc_terminate($proc) to exit the program after you make sure the disconnect has happened as logged in your database
- https://www.php.net/manual/en/function.proc-open.php
- https://www.php.net/manual/en/function.proc-get-status.php
- https://www.php.net/manual/en/function.proc-close.php
- https://www.php.net/manual/en/function.proc-terminate.php

I know that you are actually asking for an option to be added, if it does not already exist, in WinSCP to close on disconnect, but this could get you by in the mean time (although you may already have thought of this and are miles ahead in regards to an actual solution code wise but would rather see it implemented in WinSCP itself). Just a thought if helpful, otherwise just disregard.

Reply with quote

Niko Nik
Joined:
Posts:
6

Close Application directly after disconnect (not in command mode)

Hello A.E.Guest,

many thanks for your answers and for your effort, but unfortunately there is no application which is able to monitor the connection to detect a disconnect.
Is there a possibility to use a functionality built in in WinSCP?

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
31,405
Location:
Prague, Czechia

Re: Close Application directly after disconnect (not in command mode)

Niko Nik wrote:

Which brings me to the next question: how can I close the program after a disconnect as you suggested?
I was referring to you as a user. I didn't know that you use "wrapper" application.

Niko Nik wrote:

many thanks for your answers and for your effort, but unfortunately there is no application which is able to monitor the connection to detect a disconnect.
What about your "wrapper" application?

Is there a possibility to use a functionality built in in WinSCP?
No.
But I've added this request to the tracker:
https://winscp.net/tracker/1719

Reply with quote

Niko Nik
Joined:
Posts:
6

Close Application directly after disconnect (not in command mode)

Thanks Martin for your response and: no, our wrapper can only start programs with paramters, to watch an IP conncetion to trigger a TCP disconect it is not able to.
Anyway, so I have to deal with it for now. You added my problem to your tasklist, so I'm looking forward to get a solution hopefully soon.
Thanks again!

Reply with quote

Advertisement

A.E.
Guest

Re: Close Application directly after disconnect (not in command mode)

Niko Nik wrote:

Hello A.E.Guest,

many thanks for your answers and for your effort, but unfortunately there is no application which is able to monitor the connection to detect a disconnect.
Is there a possibility to use a functionality built in in WinSCP?

You could try this script, it uses netstat to monitor the connection with a 5 second wait time between netstat pulls and will continue to monitor until all instances of WinSCP are disconnected:
@ECHO OFF

:BEGIN
REM File to store process ID for WinSCP and Connection associated with WinSCP 
IF EXIST %temp% SET filename_for_winscp_pid=%temp%\%random%
IF EXIST %temp% SET filename_for_winscp_connection_pid=%temp%\%random%
IF EXIST %temp% SET filename_for_loop=%temp%\%random%

REM Exit if temp files exist
IF EXIST %filename_for_winscp_pid% exit
IF EXIST %filename_for_winscp_connection_pid% exit
IF EXIST %filename_for_loop% exit

REM First FOR gets PID of WinSCP and Second FOR gets PID of Connection associated with WinSCP (if any)
IF EXIST %SystemRoot%\System32\wbem\wmic.exe IF EXIST %SystemRoot%\System32\netstat.exe IF EXIST %SystemRoot%\System32\findstr.exe %SystemRoot%\System32\wbem\wmic.exe process get name, processid|for /F "tokens=2" %%i in ('%SystemRoot%\System32\findstr.exe /B /I "WinSCP"') do @echo %%i >> %filename_for_winscp_pid% && %SystemRoot%\System32\netstat.exe -a -n -o|for /F "tokens=5" %%g in ('%SystemRoot%\System32\findstr.exe /E /I "%%i"') do @echo %%g >> %filename_for_winscp_connection_pid%

REM Determine if there exists a Connection associated with WinSCP, and if not then Kill the WinSCP process (second statement handles multiple WinSCP processes)
IF EXIST %SystemRoot%\System32\taskkill.exe IF EXIST %filename_for_winscp_pid% IF NOT EXIST %filename_for_winscp_connection_pid% for /F "tokens=1" %%a in (%filename_for_winscp_pid%) do @%SystemRoot%\System32\taskkill.exe /F /PID %%a
IF EXIST %SystemRoot%\System32\taskkill.exe IF EXIST %SystemRoot%\System32\find.exe IF EXIST %filename_for_winscp_pid% If EXIST %filename_for_winscp_connection_pid% for /F "tokens=1" %%b in (%filename_for_winscp_pid%) do for /F "tokens=3" %%c in ('%SystemRoot%\System32\find.exe /C "%%b" %filename_for_winscp_connection_pid%') do @IF "%%c"=="0" (%SystemRoot%\System32\taskkill.exe /F /PID %%b) 

REM Delete Temporary Files
IF EXIST %filename_for_winscp_pid% del %filename_for_winscp_pid%
IF EXIST %filename_for_winscp_connection_pid% del %filename_for_winscp_connection_pid%

REM If WinSCP Process still running, wait for 5 seconds and then restart loop
IF EXIST %SystemRoot%\System32\wbem\wmic.exe IF EXIST %SystemRoot%\System32\findstr.exe %SystemRoot%\System32\wbem\wmic.exe process get name, processid|for /F "tokens=2" %%d in ('%SystemRoot%\System32\findstr.exe /B /I "WinSCP"') do @echo %%d > %filename_for_loop%
IF EXIST %SystemRoot%\System32\findstr.exe IF EXIST %filename_for_loop% for /F "tokens=1" %%e in ('%SystemRoot%\System32\findstr.exe /N "^" %filename_for_loop%') do @IF NOT "%%e"=="0" %SystemRoot%\System32\timeout.exe /T 5 > NUL && IF EXIST %filename_for_loop% del %filename_for_loop% && GOTO BEGIN

Hope that helps out

Reply with quote

Niko Nik
Joined:
Posts:
6

Close Application directly after disconnect (not in command mode)

Hello A.E.,

sorry for delay, but I was busy last time...
This is REALLY an interesting idea! Good work! The only thing which can prevent this solution could be - we use a terminal server - that netstat is not able to differ between the different sessions - but I will try it and will told you the result.
Many thanks for your ideas!

Best regards

Reply with quote

A.E. Guest
Guest

Re: Close Application directly after disconnect (not in command mode)

Niko Nik wrote:

Hello A.E.,

sorry for delay, but I was busy last time...
This is REALLY an interesting idea! Good work! The only thing which can prevent this solution could be - we use a terminal server - that netstat is not able to differ between the different sessions - but I will try it and will told you the result.
Many thanks for your ideas!

Best regards

I think it will work out still with multiple user sessions in a terminal server, since it is only looking for those instances of WinSCP that have no active connection (ie. disconnected). So, even though it may try to close the pid of other user sessions of WinSCP that have no active connection (if the user that called the script has rights to close a program of another user), it is still only closing WinSCP pid's that are disconnected. The connections from netstat are associated with the process name for WinSCP from wmic by the pid's from both programs - so it should not close any user session's use of WinSCP as long as the associated pid's still show a connection in netstat.

I think too if you were wanting to start WinSCP from the same script, you could call the executable in the batch file between @echo off and :Begin (I think that would work).

Let me know though how it works out, or does not work out, and maybe we can adjust it to be user session specific as well.

Reply with quote

A.E. Guest
Guest

Re: Close Application directly after disconnect (not in command mode)

Of course, you could also use the above batch file and run it as a service with an elevated user. If you did this, and did not call WinSCP from the batch file, you would have to make the batch file cont. loop, but you could do this by putting "GOTO BEGIN" in the last line of the batch file. The drawback with running it as one service is that it will constantly have a small resource pull, rather than just when it is called is association with a new execution of WinSCP.

If you wanted to go this route you could change the .bat file to service with these instructions:
· https://www.coretechnologies.com/products/AlwaysUp/Apps/RunBatchFileAsAService.html

Or, you could change your .bat to an .exe and then run it as a service using SC.EXE:
· https://superuser.com/questions/868340/how-can-i-convert-a-windows-batch-script-to-a-exe
· https://stackoverflow.com/questions/3582108/create-windows-service-from-executable

Just an additional thought of how you could use the above batch file - if you only wanted one copy of the batch file running under an elevated rights user as a service to monitor connections for all user sessions, and close down WinSCP for those users who have disconnected in WinSCP.

Reply with quote

Advertisement

A.E. Guest
Guest

Re: Close Application directly after disconnect (not in command mode)

Of course, you could also use the above batch file and run it as a service with an elevated user. If you did this, and did not call WinSCP from the batch file, you would have to make the batch file cont. loop, but you could do this by putting "GOTO BEGIN" in the last line of the batch file. The drawback with running it as one service is that it will constantly have a small resource pull, rather than just when it is called is association with a new execution of WinSCP.

If you wanted to go this route you could change the .bat file to service with these instructions:
· www (dot) coretechnologies (dot) com/products/AlwaysUp/Apps/RunBatchFileAsAService.html

Or, you could change your .bat to an .exe and then run it as a service using SC.EXE:
· superuser (dot) com/questions/868340/how-can-i-convert-a-windows-batch-script-to-a-exe
· stackoverflow (dot) com/questions/3582108/create-windows-service-from-executable

Just an additional thought of how you could use the above batch file - if you only wanted one copy of the batch file running under an elevated rights user as a service to monitor connections for all user sessions, and close down WinSCP for those users who have disconnected in WinSCP.

Reply with quote

Advertisement

You can post new topics in this forum