Execute remote commands from Debian to Windows via

2019-04-17 09:13发布

问题:

I am working on remotely executing a command line in Windows from Debian. For that, I tried to use the bash script below. Using the expect tool, it consists in connecting via telnet to the remote server, entering username and password values and sending the command line desired.

#!/usr/bin/expect

set timeout 20
set name 192.168.1.46
set user Administrateur
set password MSapp/*2013
set cmd "TASKKILL /F /IM Tomcat6.exe"

spawn telnet 192.168.1.46
expect "login:"
send "$user\r"
expect "password:"
send "$password\r"
expect "C:\Users\Administrateur>"
send "$cmd\r"

The telnet connection is well established. But, the command line is not executed.

Could someone tell me what is wrong with my script?

回答1:

Just add one more expect statement at the end, like as follows,

send "$cmd\r"
expect "C:\Users\Administrateur>"

Basically, expect will work with two feasible commands such as send and expect. If send is used, then it is mandatory to have expect (in most of the cases) afterwards. (while the vice-versa is not required to be mandatory)

This is because without that we will be missing out what is happening in the spawned process as expect will assume that you simply need to send one string value and not expecting anything else from the session.