I want to connect to an instance via ssh and run one or more commands on it. I therefore wrote this script, test.bash:
#!/bin/bash
gcloud compute instances create broadcast --disk name=orego-instance,boot=yes
gcloud compute ssh broadcast <<REMOTE
ls
REMOTE
gcloud compute instances delete broadcast --quietMy problem is that these commands work manually, but not through the script, as demonstrated by the transcript below. Specifically, the instance appears to refuse the ssh connection.
Does anyone have any idea why this would happen?
Last login: Thu Jun 11 11:43:34 on ttys006
c989203:~ drake$ gcloud compute instances create broadcast --disk name=orego-instance,boot=yes
Created [https://www.googleapis.com/compute/v1/projects/orego-966/zones/us-central1-a/instances/broadcast].
NAME ZONE MACHINE_TYPE PREEMPTIBLE INTERNAL_IP EXTERNAL_IP STATUS
broadcast us-central1-a n1-standard-1 10.240.45.174 107.178.222.236 RUNNING
c989203:~ drake$ gcloud compute ssh broadcast ls
Warning: Permanently added '107.178.222.236' (RSA) to the list of known hosts.
broadcast.bash
collate.bash
collate.bash~
gnugo-3.8
Hello.class
Hello.java
kill-experiment.bash
Orego
results
things.txt
c989203:~ drake$ gcloud compute instances delete broadcast
The following instances will be deleted. Attached disks configured to
be auto-deleted will be deleted unless they are attached to any other
instances. Deleting a disk is irreversible and any data on the disk
will be lost.
- [broadcast] in [us-central1-a]
Do you want to continue (Y/n)? y
Deleted [https://www.googleapis.com/compute/v1/projects/orego-966/zones/us-central1-a/instances/broadcast].
c989203:~ drake$ ~/Documents/workspace/Orego/bash-scripts/test.bash
Created [https://www.googleapis.com/compute/v1/projects/orego-966/zones/us-central1-a/instances/broadcast].
NAME ZONE MACHINE_TYPE PREEMPTIBLE INTERNAL_IP EXTERNAL_IP STATUS
broadcast us-central1-a n1-standard-1 10.240.95.212 130.211.172.186 RUNNING
Pseudo-terminal will not be allocated because stdin is not a terminal.
ssh: connect to host 130.211.172.186 port 22: Connection refused
ERROR: (gcloud.compute.ssh) [/usr/bin/ssh] exited with return code [255].
Deleted [https://www.googleapis.com/compute/v1/projects/orego-966/zones/us-central1-a/instances/broadcast].
c989203:~ drake$ #!/bin/bash
gcloud compute ssh broadcast lsHelp! My bash and GCE skills are both thin, so I have no idea what to do here.
Here's the transcript:
$ gcloud compute instances create broadcast --disk name=orego-instance,boot=yes
Created [https://www.googleapis.com/compute/v1/projects/orego-966/zones/us-central1-a/instances/broadcast].
NAME ZONE MACHINE_TYPE PREEMPTIBLE INTERNAL_IP EXTERNAL_IP STATUS
broadcast us-central1-a n1-standard-1 10.240.133.171 130.211.146.246 RUNNING
$ ./test.bash
ssh: connect to host 130.211.146.246 port 22: Connection refused
ERROR: (gcloud.compute.ssh) [/usr/bin/ssh] exited with return code [255].
$ gcloud compute ssh broadcast ls
Warning: Permanently added '130.211.146.246' (RSA) to the list of known hosts.
broadcast.bash
collate.bash
collate.bash~
gnugo-3.8
Hello.class
Hello.java
kill-experiment.bash
Orego
results
things.txt
update.bash
$ ./test.bash
Warning: Permanently added '130.211.146.246' (RSA) to the list of known hosts.
broadcast.bash
collate.bash
collate.bash~
gnugo-3.8
Hello.class
Hello.java
kill-experiment.bash
Orego
results
things.txt
update.bash
I did test your script and it works well for a disk created from the CentOS image. However, as your disk may have customized with other services, and subsequently it could take longer all services started and running including SSH. To verify this, I suggest putting several seconds delay (e.g. 30 seconds) before SSH command in your script.
To view this discussion on the web visit https://groups.google.com/d/msgid/gce-discussion/46cdfdd1-2133-4931-a153-4718744107c0%40googlegroups.com.--
© 2014 Google Inc. 1600 Amphitheatre Parkway, Mountain View, CA 94043
Email preferences: You received this email because you signed up for the Google Compute Engine Discussion Google Group (gce-dis...@googlegroups.com) to participate in discussions with other members of the Google Compute Engine community and the Google Compute Engine Team.
---
You received this message because you are subscribed to a topic in the Google Groups "gce-discussion" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/gce-discussion/ydNw8qcsIL8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to gce-discussio...@googlegroups.com.
To post to this group, send email to gce-dis...@googlegroups.com.