Execute remote commands from Debian to Windows via Telnet

社会主义新天地 提交于 2019-12-06 15:01:30

问题


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.



来源:https://stackoverflow.com/questions/28319804/execute-remote-commands-from-debian-to-windows-via-telnet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!