Remotely Start/Stop TeamViewer service using a Web trigger

Remotely Start/Stop TeamViewer service using a Web trigger

There are some days where I screw things up pretty badly... And yes, the last resort would have been that remote control services I, of course, had left disabled or in manual start mode. Hence, you're pretty much so said; fried. After having lost access to one of my extending Labs remote location due to me being too bad and perhaps rushing things out, I've actually cobbled out a decent way to remotely start/stop mostly any services you'd want to.

The idea here is that in some part of the remote network (where it makes sense should I say) you'll setup some Remote Control services. My choice landed on TeamViewer (thanks guys!!) but there's plenty of such tools available.

Here is what I've done:

  1. Setup TeamViewer on a remote host, installing it with the default's settings
  2. Configure a remote connectivity password of our choice within TeamViewer (different than the session password automatically created/updated upon starting TeamViewer, both will work)
  3. Configure the TeamViewer service to start manually (it defaults to Automatic)

Once we have that, the next item we'll need is a trigger repository of some kind. A web server in our control will do just fine. On there, we will host a simple text file on which we will be able to shift values in order to either Start/Stop our TeamViewer services. For example:

λ curl http://fqdn/hardcorecobbling/justdoit.txt

Quick summary:
- We've installed TeamViewer on some remote hosts of interest in case of poor SysOps poo storms.
- We've set a 2nd password which we will always know.
- We've kept TeamViewer from running automatically.
- We've hosted a trigger file on an internet reachable destination.

Now comes the real hardcore cobbling. Below you'll find a slightly edited version of the Windows batch script I've left my box of interests running every two minutes (scheduled using the common Windows Task Scheduler (more on that later)):


:: script global variables
FOR /f %%a IN ('WMIC OS GET LocalDateTime ^| FIND "."') DO SET DTS=%%a
SET DateTime=%DTS:~0,8%-%DTS:~8,6%
SET me=%~n0
SET parent=%~dp0
SET home=C:\cobbling\teamviewer
SET log=C:\cobbling\teamviewer\logs\%me%_%DateTime%_log.txt

:: Getting the pill
c:\cobbling\wget.exe --quiet -O %home%\justdoit.txt http://fqdn/cobbling/justdoit.txt
SET /p tv_status=<%home%\justdoit.txt

:: Doing our cobbling
if "%tv_status%"=="1" (
    CALL :tee "[=== %me% @ %DateTime%% ---> Trigger file is set to ONE		===]"
    CALL :tee "[=== %me% @ %DateTime%% ---> Starting TeamViewer...		===]"
    net start TeamViewer
    CALL :tee "[=== %me% @ %DateTime%% ---> Started TeamViewer successfully...	===]"		
) else (
    CALL :tee "[=== %me% @ %DateTime%% ---> Trigger file is set to ZERO	===]"
    CALL :tee "[=== %me% @ %DateTime%% ---> Stopping TeamViewer...		===]"
    net stop TeamViewer
    CALL :tee "[=== %me% @ %DateTime%% ---> Stopped TeamViewer successfully...	===]"	

:: Cleaning up a bit
CALL :cleanup "[=== %me% @ %DateTime%% ---> Cleaning up older log files...	===]"
DEL %home%\justdoit.txt

:: kissgoodnight & force execution to quit
CALL :tee "[=== %me% @ %DateTime%% ---> ALL JOB DONE!			===]"

:: a function to write to the log file and stdout
ECHO %* >> "%log%"

:: a function to clean the log files older than 16 minutes (task is scheduled ti run every 2 minutes)
ECHO %* >> "%log%"
ECHO >%temp%\temp.vbs  for each File In CreateObject("XXXXXXXXXXXXXXXXXXXXXXXXXX").GetFolder("C:\cobbling\teamviewer\logs\").Files
ECHO >>%temp%\temp.vbs If DateDiff("n",File.DateCreated,Now) ^> 16 Then File.Delete
ECHO >>%temp%\temp.vbs next
cscript /nologo %temp%\temp.vbs 
DEL %temp%\temp.vbs

And now we just need to schedule a Window Task Scheduler task running this script at the recurrence of our choice, I've opted for 2 minutes (task XML export below):

    <Principal id="Author">
  <Actions Context="Author">

Should I mess things up, I've got now a triggerable remote access possibility. I can go and edit my http://fqdn/cobbling/justdoit.txt file with either 0 to stop TeamViewer or 1 to start it, wait for the task to run, connect via TeamViewer, sort my own mess out not forgetting to reset the trigger file to 0 afterwards.

Security wise, I would avoid the use any fqdn linking back to you. You basically just need a repository somewhere at your control. Furthermore, a WAF like FortiWeb would allow you to restrict access to your trigger file(s) and this from your remote locations edge public IPs uniquely, for example. Finally, the start trigger value can be any value of your choice, 1 being what anyone gifted with half a brain having RW access to the trigger file would probably try next...

One last neat thing with such cobbling is that different trigger file values could wake up different systems in different locations. Something like ATV98733 would start TeamViewer on a host seated in location A while BTV23799 would start TeamViewer on a host seated in location B and so on.

Old school yet ass savin'. Hope you found this post useful.


Image Credits: The Project 941 or Akula, Russian "Акула" ("Shark") class submarine (NATO reporting name: Typhoon) is a type of nuclear-powered ballistic missile submarine deployed by the Soviet Navy in the 1980s.

Show Comments