3

I have to orchestrate some script tasks among few Linux machines and a Windows machine. ssh works fine for linux machines suing below:

ssh mysuser@targetLinuxmachine "python '/pathtomyscript/myscript.py' &"

But the same method doesn't work as background task when I'm sending the command to an Windows machine. On the Windows machine, I have ssh server running and the ssh connection itself is working. RSA public key has been inserted too. In fact below commands runs:

ssh -n -f mysuer@targetwinmachine "python 'C:\Windowspath\myscript.py -someswitch parameter' &"

however executing the command in bash will take my terminal to windows console and doesn't allow me to run it as a back ground task. I guess "&" is not working on windows to run the command as a background task.

How can I run the equivalent script on a Windows machine as a background task?

MRK
  • 33
  • 1
  • 5
  • To open an SSH session to a Windows host you need to run an SSH server on the target host. There are a number of commercial options which are easily googleable. Cygwin, although wonderful in many ways, is not fit for production use for a number of solid reasons, also googleable. – ErikE Feb 19 '14 at 17:19
  • I should have made the question a bit more clear, the ssh itself is working. I have ssh server running and below commands runs: 'ssh -n -f mysuer@targetwinmachine "python 'C:\Windowspath\myscript.py -someswitch parameter' &" ' just this takes my terminal to windows console and doesn't allow me to run it as back ground task. – MRK Feb 19 '14 at 18:02

4 Answers4

2

Yes, cygwin has a very good ssh server for windows. First you should download and install this.

After you can log into your windows machine with command line ssh, you can use ssh-keygen to make this passwordless and automatic.

The third thing were to install python on your windows machine, which you can get from http://www.python.org/getit/windows/ .

It is for me uncelar, which one of this steps don't work, but probably your main problem is with the ssh server install on your windows machine. Can you log in to your server with commandline ssh from your unix/linux box? If not, please upgrade your question and I will upgrade my answer.

peterh
  • 4,914
  • 13
  • 29
  • 44
  • Thanks but I already have ssh server running with rsa key. I'm sorry that my original post wasn't clear enough, the command it self runs but moves my linux terminal to windows terminal like an interactive mode. I need this command running in background so the other task under the line can continue without waiting for the windows task complete. – MRK Feb 20 '14 at 10:40
  • @MRK I can't understand this: "moves my linux terminal to windows terminal like an interactive mode." – peterh Feb 20 '14 at 10:45
  • When I run the line of ssh script, which I posted above, from a Linux machine, the Linux "user@lix:~$" prompt changes to "c:/" prompt and starts the script I listed after the ssh. Then, the C prompt remain as it is until the all intended python scripts complete. – MRK Feb 20 '14 at 10:50
  • @MRK This is the problem of your ssh server. I suggest to try the sshd from cygwin. – peterh Feb 20 '14 at 10:55
  • Really? that's a good news, I will try different types today. I'll update this soon. thanks. – MRK Feb 20 '14 at 11:00
1

You can use Putty from a command line:

plink mysuser@targetLinuxmachine "python '/pathtomyscript/myscript.py' &"

SSH keys can be managed via PuttyAgent.

peterh
  • 4,914
  • 13
  • 29
  • 44
DrGkill
  • 936
  • 6
  • 7
  • Actually I have putty on Windows server side, but I didn't know the putty works as ssh server. Therefore I installed SharpSSH. That's anyway good to know thanks! – MRK Feb 20 '14 at 10:42
  • @MRK, `plink` is just a `cli` tool for putty. Its not a ssh server for putty: https://www.ssh.com/ssh/putty/putty-manuals/0.68/Chapter7.html Did you find `sharpssh` to be reliable way to run the python code? I have tried mobassh and `openssh`, and both of them turned out to be unreliable to run the python script – alpha_989 Mar 10 '18 at 22:50
  • mobassh gave me a error saying "Fatal Python error: Py_Initialize: can't initialize sys standard streams LookupError: unknown encoding: cp28591 Current thread 0x00000874 (most recent call first):" OpenSSH didnt work at all.. and failed without warning. I am able to access and run powershell and cmd scripts from both – alpha_989 Mar 10 '18 at 22:51
1

So you just want to run the command in the background on linux? I think you're just putting the & in the wrong place. Put the & outside of the "":

ssh -n -f mysuer@targetwinmachine "python 'C:\Windowspath\myscript.py -someswitch parameter' " &
peterh
  • 4,914
  • 13
  • 29
  • 44
jmp242
  • 668
  • 3
  • 13
  • This is the tricky part of my problem. I'd like to send this code from Linux machine to run on Windows machine as a back ground task. Basically I'm forking 26 tasks in same time, then happen one have to run on Windows.... – MRK Feb 20 '14 at 10:37
1

Via the cgi scripts from an apache server is a nifty way to execute code on another machine.

peterh
  • 4,914
  • 13
  • 29
  • 44
dooode
  • 121
  • 4
  • Actually I tried with Python proxy to do the same, the problem is that my environment require to keep low in the memory footprint and keep running the waiting call through http wasn't really good method, that's how I'm ending up using this ssh and rsync method. – MRK Feb 20 '14 at 10:45