views:

263

answers:

5

Does anyone know an elegant way to initiate a bash script (to run on a linux box) from a windows service written in C#?

I can only think of some combination of putty doing auto-login and automatically running a command upon login. But this seems clumsy and a bit insecure.

Security doesn't need to be very high as both boxes reside on the internal LAN inside the corporate firewall. And only sysadmins can login to the windows box.

A: 

If the linux box is running samba, you can use a magic script http://oreilly.com/catalog/samba/chapter/book/ch08%5F02.html

I would probably run a webserver on the linux box and run the script via an http request

gnibbler
+1  A: 

If they are behind the firewall you can create a mini server on the linux machine that waits for a command on the given port. Windows can telnet to that port and send the command.

grigy
A: 

I do this with Cygwin including the optional packages for ssh and cron. Almost as elegant as using a real Unix machine.

mobrule
+2  A: 

You can use SSH with key authentication to run a command without having to enter or store a password in your application. You have to enable key authentication in the Linux SSH server (follow instructions here to create and store the key in the appropriate location on the server) and then you can run

plink -i <key_location> user@machine "command"

in your application via Process.Start()

plink is the command line utility which comes bundled with PuTTY.

EDIT: If you don't want PKI (which may not be a good idea, but you know your environment), you can setup a rsh server in the linux box and send the command via rsh (Cygwin's rsh might work better with linux boxes)

Vinko Vrsalovic
Thanks for this idea Vinko. plink is basically what i was already thinking of. But it relies on accepted keys, which often becomes a pain on the windows side when you are running as a different user, etc. I think I'm looking for something that doesn't rely on using PKI. Sorry if I wasn't clear enough in my original question.
Stew-au
By the way, if the destination user on the Linux bos will always be the same, you can share the key and store it in a common location, that way there is no pain at all.
Vinko Vrsalovic
A: 

Since you asked for something easily usable from C#, the webserver method might be best: quick and easy, and no password would be needed. Set up the Linux script in /var/www/cgi-bin and start it with the C# magic to open an http socket and call the http://hostname/cgi-bin/programname.

Shannon Nelson