Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I am planning to run a bash script on a remote Windows system from my local Linux machine. For this purpose, I will have to log in to Windows machine from the local one and then execute the bash Script.

First of all, is this possible? If yes, then how can it be done?

share|improve this question
    
Have you heard of Cygwin? –  1_CR Mar 22 at 5:09
    
Yes. But, I would prefer to do it using command line. –  Mandar Shinde Mar 22 at 5:12
1  
I would consider doing it with wmiexe, for eg.: winexe -U "<user>%<pwd>" //192.168.1.2 'wmic bios get serialnumber' –  slm Mar 22 at 5:39
1  
@edvinas.me - cygwin is a bad way to go here IMO. If you have X number of machines to do this on, it's just another thing to install/support. Better to use the systems native capabilities then crutch Cygwin and Bash into this. –  slm Mar 22 at 18:29
1  
@MandarShinde - what's the love affair w/ doing this with Bash anyway? Can you share this script? That seems to be the big unknown here and would really help everyone understand why this has to be done using Bash when there are ample other ways to do this already (native scripting) on Windows platforms, with a simple winexe ...some remote server + script call... would easily do the job and be easier to maintain. –  slm Mar 22 at 18:31

4 Answers 4

up vote 7 down vote accepted

This can be done by installing Cygwin and an openssh server on your Windows machine. Cygwin will come with bash, which can run your script, and openssh can be installed under Cygwin, and will allow you to login to the Windows machine remotely. Before logging in, you can transfer your script to the Windows machine using scp, and then run it directly with ssh.

openssh can be installed using the cygwin setup program. For more detailed instructions, see http://www.howtogeek.com/howto/41560/how-to-get-ssh-command-line-access-to-windows-7-using-cygwin/

share|improve this answer

I agree with @slm that this is something of a borderline question. The truth is, Windows incorporates its own remote access protocols as your Unix system incorporates its own. Microsoft Powershell has matured a great deal since it was introduced with Vista, and it can do most if not all of the things you'd expect a shell script to do for you on your Windows system. For instance, according to technet:

The Windows PowerShell remoting features are supported by the WS-Management protocol and the Windows Remote Management (WinRM) service that implements WS-Management in Windows. Computers running Windows 7 and later include WinRM 2.0 or later. On computers running earlier versions of Windows, you need to install WinRM 2.0 or later as appropriate and if supported. Currently, remoting is supported on Windows Vista with Service Pack 1 or later, Windows 7, Windows Server 2008, and Windows Server 2008 Release 2. 

Of course making use of this from a Linux client presents some problems, but I expect a virtual machine would solve most of them, or even just a netcat or ssh proxy. Contributing 5 minutes and the use of a search engine to my mild curiosity and I now know of the Pash project, though I will make no claims on its usefulness.

UPDATE:

In the course of once again entertaining that mild-curiosity I revisited the Pash sourceforge page and browsed just a little more. While doing so, I noticed that the associated RECOMMENDED PROJECTS included not only the winexe that @slm recommends, but another I'd never heard of called win-bash. From the description:

Unlike other bash ports for Windows (e.g. the cygwin bash ), the win-bash needs no special environment or DLLs. There is just one binary and that's it.

The goal of the win-bash project is to finish the port to Windows and provide a fully-functional bash.exe binary for Windows NT and derived systems. win-bash can be used as an input shell, as well as an interpreter to run UN*X shell scripts.

What I find significant about the above regarding this question is its standalone nature. If it has no library or registry dependencies, a Windows executable can be remotely executed - for instance from a file share - without installation. So it is conceivable that you could remote into a machine using native Windows protocols (possibly also with or including winexe ), run a powershell or net use command to map a file share, execute your shell script via win-bash, then unmap the share and terminate the connection without having to copy even a single file to the remote host.

I say it is conceivable, but does it work? I don't know, though I might just find out.

I like to keep a current win pe image on my system's EFI partition so it is available to rEFInd (and bootable thanks to an iPXE hack or two). So far I've only customized it to block the default Windows install starting and set it to open a cmd prompt proxied by netcat, because cygwin seemed to me to be an awful lot of chore for little return, but now my previously mild curiosity is growing... I'll likely return with a report on my experience afters adding it to my pe image.

END UPDATE

@slm mentions winexe, of which I had never heard before, though I am now interested. He also mentions wmic as a tool you can use, and that is a tool which I have often used myself - it grants you pretty powerful access to the Registry backend using SQL-like syntax.

I do know that if you wish to execute .cmd scripts based on specific rules there is the Windows Task Manager and Windows Group Policy, both of which, as I believe, you can influence at least to some degree via wmic.

Last I will say that porting code from one operating system to another is not done as often as we might like for a reason: it is problematic by its very nature. Different operating systems behave differently, if nothing else, at the least because they are different. ssh for instance, at least in my experience, was incapable of handling Windows' graduated permissions and required either system level access or simply did not work, though netcat seemed to accept the permissions level of the user account under which I ran it. Powershell, as I mentioned above, can be an extremely tedious tool if used for parsing strings, as you may be accustomed to do in a Unix environment, because it treats everything as an object in its own right. So good luck.

share|improve this answer

You would use ssh to access a remote ssh server and run a command.

For example

ssh user@remote.server.address command

If you want to know how to set up a windows machine as a ssh server, that is a question for the windows forum. However, everyone's suggestion to use cygwin and open ssh is a good idea.

share|improve this answer
1  
How would this work for running a bash script on the Windows machine? You'd need to have bash set up on it first right? –  terdon Mar 22 at 17:49
    
@terdon Well yes, I gave the generic format to login using ssh from unix, which I thought was on topic for this forum. And confirmed that cygwin should be installed which I thought was off-topic, but suggested it anyway. –  X Tian Mar 22 at 18:41
    
I know, my point was that this won't work unless there is a bash shell and an ssh server on the other side so the command alone is not very useful. That's why my comment was a question, I thought perhaps you had a trick in mind. As it stands though, this does not answer the question since the only relevant piece of information is installing cygwin and you don't explain how to do so (nevertheless, just so you know, it was not I who downvoted). –  terdon Mar 22 at 18:44

I would probably dissuade you from using Cygwin just to crutch in a Bash script which will likely need to be highly customized anyway so that it can run on top of Cygwin + Windows. This seems very much like an XY Problem.

Instead you'd be better served in directing the running of a script thats been written in a scripting language that's native to windows instead. There are a wealth of options on Windows using anything from a simple .cmd file up to something in VB script.

Any of these solutions can then be executed from your Linux system using winexe, requiring you to not have to install anything on the Windows system beyond making sure they have access to your script.

Examples

$ winexe -U "<user>%<pwd>" //192.168.1.2 'wmic bios get serialnumber'
$ winexe -U "<user>%<pwd>" //192.168.1.2 'C:\path\to\script.cmd'

References

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.