Using plink.exe or similar in a scheduled task (to be run continuously)

3

I've been trying to get plink.exe (from the maker of PuTTY) to establish a local SOCKS proxy to a remote site, as well as some tunnels.

The action is simply to start plink.exe with the right parameters. No questions there.

The conditions tie the start to an available network connection, which I thought to be sensible.

Then I also set it to:

  • Allow task to be run on demand
  • Run task as soon as possible after a scheduled start is missed
  • If the task fails, restart every: [1 minute]
  • If the running task does not end when requested, force it to stop

Now all of this works perfectly fine at system start. However, when I put the machine to sleep and wake it up again, this fails.

In fact right after waking up, the plink.exe console window is still visible, but disappears (likely because the SSH session from before the sleep long timed out). However, the status of the task is then still listed as Running although it's clearly gone (i.e. if I were to use CreateProcess, I'd be notified of the process having gone away).

The status means I have to explicitly End it and then explicitly Run it again.

Does anybody know a method by which I can essentially teach the scheduled tasks that it should restart the job when the process goes away? I have no idea what the exact metric is, but clearly the Running status I am observing is bogus.

The idea is to restart plink.exe (thus reestablishing the SOCKS proxy) whenever it goes down. Be that right after the startup and a failed connection attempt or be that after waking the machine up from sleep.

NB: I am looking for a solution that can work on both Windows 7 and Windows 10.


As per the suggestions in the comments, the following two settings have been changed:

Never turn off hard drive Don't allow computer to turn off NIC

0xC0000022L

Posted 2016-03-29T08:05:16.640

Reputation: 5 091

Is your Network Adapter set to turn off to save power when it sleeps? open devmgmt.msc and double click on your network adapter and go to power management and make sure to uncheck Allow the computer to turn off the device to saver power if it is not greyed out. – NetworkKingPin – 2016-03-29T08:13:40.687

@NetworkKingPin: I just turned it off, it was not greyed out. However, after putting the machine to sleep and waking it again, the problem is exactly the same as before. – 0xC0000022L – 2016-03-29T08:29:48.390

I wonder if its possible that the Reason the Program stops is when its asleep it stops the HDD from moving. try this In the Power Options box that opens, click the + sign next to the Hard Disk option. Here you will see the required settings under Turn off hard disk after heading. Change the value to 0. – NetworkKingPin – 2016-03-29T08:34:19.503

@NetworkKingPin: I'm not sure, but you may have misunderstood the question. Being a driver writer, I can tell you that oftentimes a user mode program won't have the slightest clue about certain resources being unavailable during sleep. Network is a special case, since the uplink takes some time to renegotiate. That said: I'll change the value and report back. – 0xC0000022L – 2016-03-29T08:53:24.873

@NetworkKingPin: no change after adjusting that setting, either. Just tested it. – 0xC0000022L – 2016-03-29T09:07:00.587

Answers

1

I'm a heavy user of plink/ssh and it's SOCKS5 proxy and port-forwarding features. Here are my ideas:

It will be difficult to rely only on the status of the plink.exe process to determine the connectivity of your SOCKS5 proxy or any SSH port-forward. Sometimes, your SSH connection might be dead for any random reason, but plink.exe will be unable to detect it and just keeps "running".

To make sure your SOCKS5 port-forward is still working, I'm going as far as monitoring the local SOCKS5 proxy port. That's the port you connect to with your browser in order to use the proxy (plink CLI -D 8008). Once plink.exe has finished the SSH handshake, it will do port-forwarding and listen on your chosen local port (8008 from my prior example). If the SSH connection is not successful, crashed, has been rejected, closed or denied, the SOCKS5 port will NOT be opened and listening. In this case the port 8008 will NOT be available.

I have a small PHP script that will try to connect to that local port (8008):

function check_open_port($host, $port, $timeout)
{
    $connection = @fsockopen($host, $port, $errno, $errstr, $timeout);

    if (is_resource($connection))
    {
        fclose($connection);
        return true;
    }
    else return false;
}

This should create almost no traffic. And it's easy to code in any other language.

You could also just run a curl and see if you can get a return from a popular site through your SOCKS5 proxy (creates a bit more traffic):

curl --socks5 127.0.0.1:8008 http://checkip.amazonaws.com/

So you could setup a 2nd scheduled task to run this monitor script every X minutes and on error, kill plink.exe process and restart (use pskill).

You could also do one single script to launch plink.exe, monitor it and restart if needed. To launch plink.exe into background (in PHP) use psexec exec("psexec -d plink.exe"). Now immediately run a monitoring loop and make sure the port-forwarded local port stays opened.

All this might sound complicated, but it's a very reliable solution. It will work with any kind of ssh port-forwards (not just SOCKS5). I have been going through many set-ups to make all that work reliable in the last 5 years... I don't think you can do it with Scheduled Tasks and plink alone.

I'm also using klink.exe (KiTTY) instead of plink.exe, which is a fork from PuTTY. It gives you a few more options to automate the process.

Eugen

Posted 2016-03-29T08:05:16.640

Reputation: 111