Create new file after `scp` in remote script [closed]

Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
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.
#!/usr/bin/expect
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 "$passwordn"
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 "$passwordn"
#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 "exitr"
#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.
shell ssh scp expect
closed as unclear what you're asking by Goro, Jeff Schaller, G-Man, muru, RalfFriedl Sep 25 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.
add a comment |Â
up vote
1
down vote
favorite
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.
#!/usr/bin/expect
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 "$passwordn"
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 "$passwordn"
#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 "exitr"
#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.
shell ssh scp expect
closed as unclear what you're asking by Goro, Jeff Schaller, G-Man, muru, RalfFriedl Sep 25 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 at 1:00
1
(1)â¯You might want to consider setting up password-lessssh.â (2)â¯Why are you using bothnandr?â Are you sure that they both work?â (3)â¯Why are you doing twospawns?â (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 at 1:49
take a look at sexpect with which you can write Expect scripts with shell code only.
â pynexj
Sep 25 at 2:04
(1)â¯I asked you why you are doing twospawns (you are spawning ansshand anscp), and you havenâÂÂt answered.âÂÂI really donâÂÂt understand why youâÂÂre spawningssh.âÂÂ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 at 18:04
(ContâÂÂd) â¦â (3)â¯Can you explain in more detail what youâÂÂre trying to do?â For example, are you trying to useexpectto invokescpto uploadexpectâÂÂs log file to the remote machine?â Why are you doingexpectâ¯"$user@"(for a second time) after you run thescp?â Why are you doingexpectâ¯"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 at 18:05
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
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.
#!/usr/bin/expect
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 "$passwordn"
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 "$passwordn"
#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 "exitr"
#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.
shell ssh scp expect
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.
#!/usr/bin/expect
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 "$passwordn"
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 "$passwordn"
#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 "exitr"
#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.
shell ssh scp expect
shell ssh scp expect
edited Sep 25 at 22:37
peterh
4,03292755
4,03292755
asked Sep 25 at 0:33
coolhand
1092
1092
closed as unclear what you're asking by Goro, Jeff Schaller, G-Man, muru, RalfFriedl Sep 25 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.
closed as unclear what you're asking by Goro, Jeff Schaller, G-Man, muru, RalfFriedl Sep 25 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 at 1:00
1
(1)â¯You might want to consider setting up password-lessssh.â (2)â¯Why are you using bothnandr?â Are you sure that they both work?â (3)â¯Why are you doing twospawns?â (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 at 1:49
take a look at sexpect with which you can write Expect scripts with shell code only.
â pynexj
Sep 25 at 2:04
(1)â¯I asked you why you are doing twospawns (you are spawning ansshand anscp), and you havenâÂÂt answered.âÂÂI really donâÂÂt understand why youâÂÂre spawningssh.âÂÂ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 at 18:04
(ContâÂÂd) â¦â (3)â¯Can you explain in more detail what youâÂÂre trying to do?â For example, are you trying to useexpectto invokescpto uploadexpectâÂÂs log file to the remote machine?â Why are you doingexpectâ¯"$user@"(for a second time) after you run thescp?â Why are you doingexpectâ¯"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 at 18:05
add a comment |Â
Do yourself a favor and use keys to reduce your script to one line; scp file.txt $S:~/
â user1133275
Sep 25 at 1:00
1
(1)â¯You might want to consider setting up password-lessssh.â (2)â¯Why are you using bothnandr?â Are you sure that they both work?â (3)â¯Why are you doing twospawns?â (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 at 1:49
take a look at sexpect with which you can write Expect scripts with shell code only.
â pynexj
Sep 25 at 2:04
(1)â¯I asked you why you are doing twospawns (you are spawning ansshand anscp), and you havenâÂÂt answered.âÂÂI really donâÂÂt understand why youâÂÂre spawningssh.âÂÂ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 at 18:04
(ContâÂÂd) â¦â (3)â¯Can you explain in more detail what youâÂÂre trying to do?â For example, are you trying to useexpectto invokescpto uploadexpectâÂÂs log file to the remote machine?â Why are you doingexpectâ¯"$user@"(for a second time) after you run thescp?â Why are you doingexpectâ¯"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 at 18:05
Do yourself a favor and use keys to reduce your script to one line; scp file.txt $S:~/
â user1133275
Sep 25 at 1:00
Do yourself a favor and use keys to reduce your script to one line; scp file.txt $S:~/
â user1133275
Sep 25 at 1:00
1
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 at 1:49
(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 at 1:49
take a look at sexpect with which you can write Expect scripts with shell code only.
â pynexj
Sep 25 at 2:04
take a look at sexpect with which you can write Expect scripts with shell code only.
â pynexj
Sep 25 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 at 18: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 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 at 18:05
(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 at 18:05
add a comment |Â
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Do yourself a favor and use keys to reduce your script to one line; scp file.txt $S:~/
â user1133275
Sep 25 at 1:00
1
(1)â¯You might want to consider setting up password-less
ssh.â (2)â¯Why are you using bothnandr?â Are you sure that they both work?â (3)â¯Why are you doing twospawns?â (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 at 1:49
take a look at sexpect with which you can write Expect scripts with shell code only.
â pynexj
Sep 25 at 2:04
(1)â¯I asked you why you are doing two
spawns (you are spawning ansshand anscp), and you havenâÂÂt answered.âÂÂI really donâÂÂt understand why youâÂÂre spawningssh.âÂÂ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 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
expectto invokescpto uploadexpectâÂÂs log file to the remote machine?â Why are you doingexpectâ¯"$user@"(for a second time) after you run thescp?â Why are you doingexpectâ¯"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 at 18:05