tcl - Expect: SSH into a remote host, run a command, and save its output to a variable -
i'm trying ssh server, run command, , save output variable, no success.
spawn $env(shell) expect "\$ " send "ls\r" expect "\$ " send "ssh myserver1\r" expect "\$ " send "cd /tmp/remotedir1\r" expect "\$ " send "ls\r" expect "\$ " set myvar1 [exec ls] puts "the value of \$myvar1 is: " puts $myvar1 send "exit\r" expect "\$ " send "exit\r" expect eof when run it, get:
spawn /bin/bash $ ls localfile1 localfile2 localfile3 $ ssh myserver1 last login: tue sep 10 15:45:07 2017 192.168.0.100 myserver1$ cd /tmp/remotedir1 myserver1$ ls remotefile1 myserver1$ value of $myvar1 is: localfile1 localfile2 localfile3 exit logout connection myserver1 closed. bash-3.2$ exit exit apparently, instead of setting $myvar1 "remotefile1", sets 3 files in $cwd on local host.
thank in advance!
using exec execute command locally.
upon sending ls command, have make use of expect_out array response.
set prompt "(.*)(#|%|>|\\\$) $" send "ls\r" expect -re $prompt puts $expect_out(1,string)
Comments
Post a Comment