0

When I am doing SSH to some machine inside the for loop it is doing the ssh but not able to execute further. Code is like:

string=c01.test.cloud.com,c02.test.cloud.com
for i in $(echo $string | sed "s/,/ /g")
do
    ssh -t -t AppAccount@$i
    cd a/b/c
    str2=x,y,z
done
gaganso
  • 2,914
  • 2
  • 26
  • 43
  • Do you have the rights to connect there? Or does it ask for a password perhaps? – Arndt Jonasson Jun 28 '18 at 17:21
  • yes .. I have... – Niket Sharma Jun 28 '18 at 17:24
  • What happens if you do the ssh command from the command line, outside a for loop? Is "cd a/b/c str2=x,y,z" what you want to do, for that matter? I think there is a semicolon missing. – Arndt Jonasson Jun 28 '18 at 17:26
  • It is successfully able to connect to the first machine of the machine list but not able to execute the script... Because there is change in shell and we have to give information somewhere else but then again will not be able to process it for machine two. – Niket Sharma Jun 28 '18 at 17:29
  • I don't understand your second sentence at all. – Arndt Jonasson Jun 28 '18 at 17:31
  • [sshexec] For more information about the application environment, run: [sshexec] /usr/local/bin/cloudops info [sshexec] [sshexec] [AppAccount@c01 ~]$ Build was aborted – Niket Sharma Jun 28 '18 at 17:32
  • There seems to be a lot about your environment you're not telling. What happens if you simply do `ssh -t -t AppAccount@c01.test.cloud.com date`? – Arndt Jonasson Jun 28 '18 at 17:35
  • We have n number of machines that we are going to connect. When I will pickup the first machine name and do the ssh we are successfull but not able to execute the script further. string=c01.test.cloud.com,c02.test.cloud.com for i in $(echo $string | sed "s/,/ /g") do ssh -t -t AppAccount@$i cd a/b/c str2=x,y,z echo $str2 done – Niket Sharma Jun 28 '18 at 17:35
  • Till this point it is fine: string=c01.test.cloud.com,c02.test.cloud.com for i in $(echo $string | sed "s/,/ /g") do ssh -t -t AppAccount@$i If will do echo or some other script then it is not working – Niket Sharma Jun 28 '18 at 17:37
  • What is `cd a/b/c str2=x,y,z` meant to achieve? At best, it does nothing. It probably gives an error. – Arndt Jonasson Jun 28 '18 at 17:38
  • What happens if you simply do ssh -t -t AppAccount@c01.test.cloud.com -> Yes we are able to connect but not able to execute anything further – Niket Sharma Jun 28 '18 at 17:39
  • This is just an example -> What is cd a/b/c str2=x,y,z meant to achieve? At best, it does nothing. It probably gives an error. – We are not able to print echo – Niket Sharma Jun 28 '18 at 17:40
  • We are executing this script through Jenkins – Niket Sharma Jun 28 '18 at 17:41

1 Answers1

1

I take it from your question that you expect cd a/b/c to run on a remote server? That's not what this script is doing. The call to ssh opens an SSH tunnel, and provides you an interactive terminal connection. It then waits for that connection to terminate. (I suspect if you pressed Control-D, the script would continue.) Your use of -t -t here is particularly strange. Why do you want to force a remote pty? This is making the problem worse (not that much, since it won't work anyway, but this seems the opposite of what you'd want).

I think this is the script you meant:

string=c01.test.cloud.com,c02.test.cloud.com
for i in $(echo $string | sed "s/,/ /g")
do
    ssh AppAccount@$i 'cd a/b/c; str2=x,y,z'
done

(This won't do anything of course, but I assume your real script has more to it than setting a shell variable and exiting.) The point is that you ned to pass the script you want to run as a parameter to ssh. Otherwise it's going to spawn an interactive shell and wait for you to close it.

Note that if your script is very complicated, it can be very inconvenient to stick it all in a single-quoted string. If your internal script is in its own file, a simple way to handle this is with bash -s which reads a script from stdin:

cat some_script | ssh server 'bash -s'

You can also use bash Here docs to achieve the same thing, but that is likely getting too fancy for this use.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610