I'm trying to use expect
to execute some certain commands in remote server like this
- ssh_cmd.sh
#!/usr/bin/expect -f
set host [lindex $argv 0]
set user [lindex $argv 1]
set passwd [lindex $argv 2]
set cmd [lindex $argv 3]
set timeout 3
spawn ssh ${user}@${host} 'bash -s' < ${cmd}
expect "*password:"
send "${passwd}\r"
expect eof
- pwd.sh
#!/bin/bash
pwd
When I execute
ssh_cmd.sh xxx.xxx.xxx.xxx root 12345 pwd.sh
it returns bash: pwd.sh: No such file or directory
. However, directly executing the following command in terminal is okay and returns the path of the remote server successfully.
ssh [email protected] 'bash -s' < pwd.sh
I wonder why and how to execute the scripts successfully.
CodePudding user response:
The arguments to spawn
are not executed using the shell, so < pwd.sh
is passed as literal arguments to ssh
, and it sends them to the remote shell.
You can spawn a local bash
to execute the command.
spawn bash -c "ssh ${user}@${host} 'bash -s' < ${cmd}"