Operating System - Linux
1820095 Members
3488 Online
109608 Solutions
New Discussion юеВ

How to execute a command on Windows machine from Linux?

 
SOLVED
Go to solution
Roderick Derks
Frequent Advisor

How to execute a command on Windows machine from Linux?

Is it possible to execute a bat or cmd file on a Windows machine from a Linux machine?

For Windows you can use "pstools" to do this.

What I want to do is to stop and start a Windows service from a Linux server.

Hope someone can help me!
23 REPLIES 23
Florian Heigl (new acc)
Honored Contributor

Re: How to execute a command on Windows machine from Linux?

The best means for doing this would by using a ssh server, preferably a commercial one that supports proper authentication to a specific account using public keys.

Then simply issue
ssh windows_system "net stop iisadmin"
sleep 30
ssh windows_system "net start iisadmin"

If security is no issue at all, there also are free remsh implementations for windows.
yesterday I stood at the edge. Today I'm one step ahead.
Roderick Derks
Frequent Advisor

Re: How to execute a command on Windows machine from Linux?

Sounds promissing!

Do you know any free tools that I have to install on the Windows machine to use SSH or REMSH?
Florian Heigl (new acc)
Honored Contributor

Re: How to execute a command on Windows machine from Linux?

www.google.com? *cough*

http://pigtail.net/LRP/printsrv/cygwin-sshd.html

rsh appears to be readily available from microsoft

http://www.microsoft.com/resources/documentation/WindowsServ/2003/standard/proddocs/en-us/Default.asp?url=/resources/documentation/WindowsServ/2003/standard/proddocs/en-us/sag_TCPIP_ovr_utilities.asp

I have tried neither of them, I don't do much Windows any more.
At the time, I think we used the SecureCRT suite with great success.
yesterday I stood at the edge. Today I'm one step ahead.
Florian Heigl (new acc)
Honored Contributor

Re: How to execute a command on Windows machine from Linux?

Ignore the second link, I'm not in the best shape today, it was about a rsh client for windows.

I searched my homedir here, but only found a rsh server for OS/2. While both Windows and OS/2 share their POSIX layer I will NOT attempt to get it work on windows :>

I'd recommend You to get the SSH way, in the worst case there is the microsoft telnet service which could be abused with Expect scripts. (just kidding)

I'm just thinking... maybe there is a much easier way by means of samba3's net command, but I have no idea. :(
yesterday I stood at the edge. Today I'm one step ahead.
Florian Heigl (new acc)
Honored Contributor

Re: How to execute a command on Windows machine from Linux?

Bingo!

the microsoft / interix services for unix contain a rsh server.
by version 3.5 they're available at no cost.
(but be warned: You need a passport account)
yesterday I stood at the edge. Today I'm one step ahead.
Paul Cross_1
Respected Contributor

Re: How to execute a command on Windows machine from Linux?

I'm not entirely sure why this has to be a "commercial" product, as openssh will do everything you need. If this were me I would install cygwin on the windows side and install the sshd as a service.

http://pigtail.net/LRP/printsrv/cygwin-sshd.html

That way you can ssh to the windows machine from linux.
Roderick Derks
Frequent Advisor

Re: How to execute a command on Windows machine from Linux?

Thanks!
This is what I tried just now:
I installed CGYWIN and tried to connect. It asked for my password and then gave this error after entering the correct password:

roderick@10.0.4.100's password:
Read from remote host 10.0.4.100: Connection reset by peer
Connection to 10.0.4.100 closed.

Googled and found nothing. Any idea?
Roderick Derks
Frequent Advisor

Re: How to execute a command on Windows machine from Linux?

Going to check microsoft / interix services later on. Also samba. Good ideas!
Florian Heigl (new acc)
Honored Contributor

Re: How to execute a command on Windows machine from Linux?

Paul - if You are able to show me one single non-commercial ssh server for windows that is able to run in the authenticated user's context, please do so?
yesterday I stood at the edge. Today I'm one step ahead.
Roderick Derks
Frequent Advisor

Re: How to execute a command on Windows machine from Linux?

Got me a passport... I downloaded the interix 3.5 (217Mb). Have to investigate if I can use it.

The thing is that I need to execute some commands on Windows systems that contain critical apps. I can not just install a complete Unix like environment on those machines. Maybe I can use just some parts of it.

Samba is not ready for stopping end starting services right now. This feature is added later on.

Doing more research now..
Telnet seems to be an option, though it is not as secure as SSH.
The only thing is that I can not get it to work. The script I use stops during login. It needs to give a carrage return but I don't know how. Someone got an idea?
B. Hulst
Trusted Contributor

Re: How to execute a command on Windows machine from Linux?

Hi,

Just a note when reading this thread:

Make sure both machines don't allow any other machine to give that remote command access.

If you compromise the windows machine easily then you also have the Linux machine under control...

Regards,
Bob
David Lee_10
Occasional Contributor
Solution

Re: How to execute a command on Windows machine from Linux?

I have had no problem using OpenSSH on Windows. Once you log in, send a simple net start.

For more tips on using OpenSSH, check out http://www.networksimplicity.com/
Peyman Javaheri
Frequent Advisor

Re: How to execute a command on Windows machine from Linux?

I never had any problem running Cygwin sshd either...
Roderick Derks
Frequent Advisor

Re: How to execute a command on Windows machine from Linux?

Thanks for the input!

This is where I am right now:
What I did was using Perl and Telnet. This works (see script at the bottom of this message). It would be better to use ssh though so I'm still going to try to run cygwin. I don't know why I'm having troubles with it. I still get this error:
roderick@10.0.4.100's password:
Read from remote host 10.0.4.100: Connection reset by peer
Connection to 10.0.4.100 closed.

Anyone any ideas? If I got the solution I'll write it down here.

Regards,
Roderick

#!/usr/bin/perl

use Net::Telnet;
$telnet = new Net::Telnet ( Timeout=>10,
Errmode=>'die');
$telnet->open('10.0.4.100');
$telnet->waitfor('/login: $/i');
$telnet->print('theusername');
$telnet->waitfor('/password: $/i');
$telnet->print('thepassword');
$telnet->waitfor('/system32/');

# I want to get some output but this
# is not working at the moment (errors)
# so I disabled it
# @output = $telnet->cmd('dir');

$telnet->print('net start alerter');
$telnet->waitfor('/system32/');
$telnet->print('quit');

# not printng anything right now:
print $output;

exit;
Paul Cross_1
Respected Contributor

Re: How to execute a command on Windows machine from Linux?

I'm not sure why your ssh attempt didn't work. You should make sure that the user roderick exists on the PC. What happens when you are prompted for your passwd? Are you able to input your password or does it give the error right away?
Other than that, ssh should be fine. If you are just going to run commands in plain text, as you showed in the above example (Net::Telnet) why not just use rsh, or expect if you want to script it?

Florian: I'm not sure what you mean by "authenticated user's context". But If you mean authenticating with keys, yes I do this all the time using openssh. If that's not what you mean, please explain.
Roderick Derks
Frequent Advisor

Re: How to execute a command on Windows machine from Linux?

Had trouble logging in in this site, took a while for me to respond.
I installed the openssh that David Lee referred to and this works.

Now the next phase: logging in using a script.

Yesterday I tried to copy a key that I created on a the Linux box (ssh-keygen -t rsa) to the Windows machine in the .rss\authorized_keys file. This didn't work. It worked on other Linux machines so this seamms to be a Windows issue.
I'm going to try how 'expect' works. Never used it before. Maybe this works with ssh.
Paul Cross_1
Respected Contributor

Re: How to execute a command on Windows machine from Linux?

Expect is just a chat-type expect and send scripting language. It will work with anything that you can type on the command line, including ssh. In fact, I used to have an expect script recorder that would just create the script based on the sequence that you were recording. Can't seem to find that now though. I think it came with the expect source.

Here is an example of a simple expect script:

#!/usr/local/bin/expect
spawn telnet
set timeout 300

expect *telnet>* {send open hostname\n}

set timeout 20
expect *login* {send userid\n}
expect *Password* {send password\n}

interact {send quit\n;exit 0}

Roderick Derks
Frequent Advisor

Re: How to execute a command on Windows machine from Linux?

Expect works.

I wrote this script:
#!/usr/bin/expect -f
#
# Roderick Derks r.derks@elisabeth.nl
# use two argumenten:
#
#
# Servicename:
# use: servicename not display name
# (zie eigenschappen van de service)#!/usr/bin/expect -f
#
# Roderick Derks r.derks@elisabeth.nl
#
# Dit script kan worden gebruikt om op een Windows
# server een service te herstarten.
# Er wordt gebruik gemnaakt van SSH. Dat betekent dat er
# op de Windows server een SSH service moet draaien.
# Gebruik hiervoor bv OpenSSH (Win32 install package).
#
# Gebruik wordt gemaakt van twee argumenten:
#
# Let wel: gebruik de servicenaam ("servicename") van
# de service, niet de "weergegeven naam" ("display name")


if {$argc!=2} {
send_user "usage: $argv0 \n\n"
exit
}
if {$argc==2} {

set HOSTNAME [lindex $argv 0]
set SERVICE [lindex $argv 1]

send_user "HOSTNAME: $HOSTNAME SERVICE: $SERVICE\n\n"
}


set force_conservative 1 ;# set to 1 to force conservative mode even if
;# script wasn't run conservatively originally
if {$force_conservative} {
set send_slow {1 .1}
proc send {ignore arg} {
sleep .1
exp_send -s -- $arg
}
}


set timeout -1
spawn $env(SHELL)
match_max 100000
expect "]# "
send -- "ssh windowsusername@$HOSTNAME\r"
expect " password: "
send -- "Passwordddddd\r"
expect ">"
send -- "net stop \"$SERVICE\"\r"
expect ">"
send -- "net start \"$SERVICE\"\r"
expect -exact ">"
send -- "exit\r"
expect -exact "\]# "
send -- "exit\r"
expect eof


if {$argc!=2} {
send_user "usage: $argv0 \n\n"
exit
}
if {$argc==2} {

set HOSTNAME [lindex $argv 0]
set SERVICE [lindex $argv 1]

send_user "HOSTNAME: $HOSTNAME SERVICE: $SERVICE\n\n"
}


set force_conservative 1 ;# set to 1 to force conservative mode even if
;# script wasn't run conservatively originally
if {$force_conservative} {
set send_slow {1 .1}
proc send {ignore arg} {
sleep .1
exp_send -s -- $arg
}
}


set timeout -1
spawn $env(SHELL)
match_max 100000
expect "]# "
send -- "ssh beheerder@$HOSTNAME\r"
expect " password: "
send -- "auckland\r"
expect ">"
send -- "net stop \"$SERVICE\"\r"
expect ">"
send -- "net start \"$SERVICE\"\r"
expect -exact ">"
send -- "exit\r"
expect -exact "\]# "
send -- "exit\r"
expect eof
Roderick Derks
Frequent Advisor

Re: How to execute a command on Windows machine from Linux?

Thanks all for the input!

I got ONE question left:

Has anyone started a script via SSH of which the login is automized from WINDOWS to LINUX/UNIX?
B. Hulst
Trusted Contributor

Re: How to execute a command on Windows machine from Linux?

Hi,

Such a construction does not pass the security audit. :-)

Regards,
Bob
Roderick Derks
Frequent Advisor

Re: How to execute a command on Windows machine from Linux?

Because there is a password in a file on the client side? Or is there more?
Paul Cross_1
Respected Contributor

Re: How to execute a command on Windows machine from Linux?

Yes, if you put your passwords in the script, yes it is insecure. That's why we use keys for ssh.

-p
Roderick Derks
Frequent Advisor

Re: How to execute a command on Windows machine from Linux?

Ok, thanks to all for contibuting!