Learning bash and trying to copy a file from a local machine to a remote machine. I would like to create a simple .txt file on the local machine that logs a success. This is my first foray into bash, so there's probably some bad practices on my part as I try to learn how everything works. I'm definitely in the "try to get it to work, first" phase of learning.

My goal is to create a remote script that copies a file from a local machine to a remote machine and then logs the success into a file on the local machine. It doesn't necessarily have to be my log file, that's just what I chose for the exercise. The lesson I'm following uses expect, ssh, and scp so I'd like to continue with these to contribute to the class forum and not have a stand-alone solution.


set user "redacted"
set password "redacted"
set success "Successfully Logged!"

log_file upload_me.txt

#I spawn ssh to connect to the remote server when I first run the remote script
spawn /usr/bin/ssh ${user}@example.com
expect "assword"
send "${password}\n"
expect "${user}@"

#honestly, I spawn scp here because this is the way I could get it to work
#my (maybe wrong) understanding was that spawn was needed to start a new process (in this case scp)
spawn scp upload_me.txt ${user}@example.com:~/
expect "assword"

send "${password}\n"

#the code seems to work until this point, when I'm am redirected to root of my local machine
#I put expect "${user}@ here because that's what shows when I do the operation manually (not with the remote script)
expect "${user}@"

#I try the exit command because I want to terminate the ssh and return to my local machine to log the success
send "exit\r"
#when I manually exit ssh, I would expect "root@" on my terminal to denote returning to my local machine. I am running locally as root
expect "root@"

#now write to a log file, except this doesn't work
#send "echo ${success} >> my_file.txt

I got the error "spawn id expX not open while executing" or some similar variant where X is an integer (e.g. exp4 or exp7).

The file gets uploaded, but I have not been able to create a file on the local machine afterwards that logs a successful attempt.

While I appreciate the tips on other methods, I am purposely using scp, expect, and ssh as it's part of the directed, non-credit course.

closed as unclear what you're asking by user88036, Jeff Schaller, G-Man, muru, RalfFriedl Sep 25 '18 at 5:48

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • Do yourself a favor and use keys to reduce your script to one line; scp file.txt $S:~/ – user1133275 Sep 25 '18 at 1:00
  • 1
    (1) You might want to consider setting up password-less ssh.  (2) Why are you using both \n and \r?  Are you sure that they both work?  (3) Why are you doing two spawns?  (4) Please don’t say things like “or some similar variant”.  Tell us the exact error message.  (5) Can you tell how far it gets before it fails?      Please do not respond in comments; edit your question to make it clearer and more complete. – G-Man Sep 25 '18 at 1:49
  • take a look at sexpect with which you can write Expect scripts with shell code only. – pynexj Sep 25 '18 at 2:04
  • (1) I asked you why you are doing two spawns (you are spawning an ssh and an scp), and you haven’t answered. I really don’t understand why you’re spawning ssh. If you are trying to complete an assignment that (arbitrarily) says that you must do X, Y, Z, Q, J and W, then it might help us if you told us exactly what the assignment says. (2) Thanks for quoting the error message and identifying the point of failure, but I still don’t fully understand. What does “I’m am redirected to root of my local machine” mean? … (Cont’d) – G-Man Sep 25 '18 at 18:04
  • (Cont’d) …  (3) Can you explain in more detail what you’re trying to do?  For example, are you trying to use expect to invoke scp to upload expect’s log file to the remote machine?  Why are you doing expect "${user}@" (for a second time) after you run the scp?  Why are you doing expect "root@"?  Are you running this as root?  (If so, why?)    Please do not respond in comments; edit your question to make it clearer and more complete. – G-Man Sep 25 '18 at 18:05

Browse other questions tagged or ask your own question.