Invoking a batch script on a remote machine using a script on the client

I have been doing software development for 17 years. I started on Unix platform with C++ and then moved to C++ on Microsoft platform and eventually transitioned to .NET and C# programming. In the meantime scripting has always been a fun thing to do on the side. Scripting is just means of accomplishing something quickly in order to simplify the development for the team and the deployments for the team. For example, some of the scripts that I will eventually share with you are:

  • Quick scripts that deploy code to a server and start/stop services
  • Scripts that monitor the changes on the server throughout the day
  • Scripts that notify you when there is a change on the server and what change it was
  • Scripts that monitor the logs for your application and send you the daily summary on the type of errrors
  • etc.....


The following example is how you can start scripts on a server by sending a signal from the client computer. There are many different ways to implement. This is one way of doing.

You basically have ScriptA.bat and ScriptB.bat  that need to do specific things on the server. Let's say you don't want to give access to everybody to remote-desktop into the server to execute these scripts, but you want to give a mechanism to your team to execute these scripts on the server.

The way these scripts are set up is the following way:

  • CLIENT script (on the machine of your teammate who does not have access to the server) writes a file onto a shared network location.
  • SIGNAL SERVER PROXY script is contentiously listening for that specific file in the shared network location and when it detects the file, it sends a signal (using WaitFor DOS command) to the appropriate server script.
  • SERVER  ScriptA.bat waits for the signal and as soon as it receives the signal, it continue running whatever it needs to do. When it finishes running, it goes back to the waiting mode.

How to demo this:
- On the server, start your ScriptA.bat and ScriptB.bat
- On the server, start the ServerSignalReceiver.bat (proxy script)
- On the client side, run ClinetScriptA.bat if you wish to invoke ScriptA.bat on the server side.








ClientScriptA.bat
____________________________________________________________

@echo off

REM Set up the location where the signal file will be created
set SIGNAL_LOCATION="\\Server01\SignalSharedLocation"

:STARTSCRIPT

touch %SIGNAL_LOCATION%\SignalA.txt

:ENDOFSCRIPT
____________________________________________________________




SignalServerProxy.bat
____________________________________________________________
@echo off

REM Set up the location where the signal file will be created
set SIGNAL_LOCATION="\\Server01\SignalSharedLocation"

:STARTSCRIPT
sleeper 5
echo %date% %time% : let me check if there are signals...

:STEP1
if not exist %SIGNAL_LOCATION%\SignalA.txt goto STEP2 
waitfor /si SIGNALA
del /F %SIGNAL_LOCATION%\SignalA.txt
echo SignalA received and propogated

:STEP2
if not exist %SIGNAL_LOCATION%\SignalB.txt goto STEP3
waitfor /si SIGNALB
del /F %SIGNAL_LOCATION%\SignalB.txt
echo SignalB received and propogated

:ENDOFSCRIPT
goto STARTSCRIPT
____________________________________________________________





ScriptA.bat
____________________________________________________________
@echo off

:STARTSCRIPT

waitfor SIGNALA

time /t  >> ScriptAoutput.txt
goto ENDOFSCRIPT


:ENDOFSCRIPT
sleeper 1
goto STARTSCRIPT
____________________________________________________________




ScriptB.bat
____________________________________________________________
@echo off

:STARTSCRIPT

waitfor SIGNALB

time /t  >> ScriptBoutput.txt
goto ENDOFSCRIPT


:ENDOFSCRIPT
sleeper 1
goto STARTSCRIPT
____________________________________________________________





- almirsCorner.com -

#batch #batchscript #scripting #doscommand #commandline #programming #devOps #automation


...

No comments:

Post a Comment