bukashka101 bukashka101 - 4 months ago 22
Linux Question

How to continue entering commands on SSH-server, using expect

I'm using

expect
to connect on my SSH-server:

#!/bin/expect

set command [lindex $argv 0]
set command_connect "connect"

proc connect {} {
spawn ssh username@192.168.1.101
expect "password"
send "password\r"
interact
}

if {$command == $command_connect} {
connect
}


All good working and I'm can connect to server, but I can't enter commands! Somehow all commands entering on my computer! If I try to clear screen:

proc connect {} {
spawn ssh username@192.168.1.101
expect "password"
send "password\r"
spawn clear
interact
}


Then I clear screen on my computer and SSH-connection disconnect (I returned to my console)!




Why is this happening and how continue enter commands on SSH-server?

Answer

If you want to send commands to the remote server, then you need to send them through the spawned SSH process:

expect "$"
send "clear\r"

The first line is to ensure the prompt on the remote server appeared.

Comments