Question

I am using cron to run a script that connects to another server via sftp and uploads files. Once in a while, I will receive the error: Spawn-id-exp7-not-open while executing send \"ls\r\" and the script will stop executing (files will not be sent). However, most of the time, the script will run with no errors.

Below is my script I use to connect :

#!/bin/bash
HOST="removed.com:\API"
USER="user"
PASS="removed"

VAR=$(expect -c "
  spawn sftp -o \"BatchMode no\" -b /var/www/prep/cmd -P 13266 $USER@$HOST

  expect \"Password:\"
  send \"$PASS\r\"

  expect \"\\\\$\"
  send \"ls\r\"

  expect -re \"$USER.*\"
  send \"exit\"
")


echo "==============="
echo "$VAR"

Below is the script that runs after I connect (cmd) :

put "/var/www/prep/1.xml"
put "/var/www/prep/2.xml"

I took a look at a similar question found at send: spawn id exp7 not open , but it has not helped me. Perhaps the connection ends before the cmd script can run?

Thanks for your help!

Was it helpful?

Solution

You're using sftp with -b. Therefore, once you provide the password, there will be no interaction: sftp will read commands from the batch file and exit.

From the man page:

-b batchfile

Batch mode reads a series of commands from an input batchfile instead of stdin. Since it lacks user interaction it should be used in conjunction with non-interactive authentication. A batchfile of ‘-’ may be used to indicate standard input. sftp will abort if any of the following commands fail: get, put, rename, ln, rm, mkdir, chdir, ls, lchdir, chmod, chown, chgrp, lpwd, df, symlink, and lmkdir. Termination on error can be suppressed on a command by command basis by prefixing the command with a ‘-’ character (for example, -rm /tmp/blah*).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top