1

I'm writing an expect script that executes a command on a remote server using ssh.

Command syntax: ssh <classname> <command>

Code:

set ip "hive28.cs.berkeley.edu"
set class [lindex $argv 0]
// set $user and $ip according to class

set cmd [lindex $argv 1]
spawn ssh "$user\@$ip '$cmd'"
expect "assword"
send "$password\r";
interact

Unfortunately, I get this error:

~/foo> ssh2 162 'pwd'

spawn ssh ***@hive28.cs.berkeley.edu 'pwd'
ssh: Could not resolve hostname hive28.cs.berkeley.edu 'pwd': Name or service not known
send: spawn id exp6 not open
    while executing
"send "$password\r""
    invoked from within <...>

But when I run the generated command directly, it works (ignore the gdircolors warnings):

~/foo> ssh ***@hive28.cs.berkeley.edu 'pwd'
***@hive28.cs.berkeley.edu's password: 
/home/ff/cs162/adm/bashrc.d/70-gnu.sh: line 36: gdircolors: command not found
/home/cc/cs162/sp16/class/***
1
  • If you ssh-keygen and ssh-copy-id first, you don't need expect at all. Commented Jan 24, 2016 at 11:36

1 Answer 1

1

Try this instead:

set ip "hive28.cs.berkeley.edu"
set class [lindex $argv 0]

set cmd [lindex $argv 1]
spawn ssh "$user@$ip" "$cmd"
expect "assword"
send "$password\r";
interact

The problem seems to be with your quoting. "$user\@$ip" and "$cmd" are two separate arguments.

Sign up to request clarification or add additional context in comments.

3 Comments

2 minor points about Tcl(expect): 1. @ is not a special character and does not need to be escaped; 2. Tcl does not require quotes around variables, even if the variable value contains whitespace.
I'm fully aware; I was trying to make minimal changes to OP's script. Quotes are for clarity; they don't cause harm. But, good to point out in a comment! I'll remove the @ escape even though it was in the original code. Thanks!
Awesome, glad to help :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.