ssh works manually, but not through script

883 views
Skip to first unread message

Peter Drake

unread,
Jun 11, 2015, 3:03:23 PM6/11/15
to gce-dis...@googlegroups.com

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 --quiet


My 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$


Peter Drake

unread,
Jun 11, 2015, 11:57:13 PM6/11/15
to gce-dis...@googlegroups.com
Curiouser and curiouser. The problems seems to be that I can't ssh into an instance from within a script until I've done so with exactly the same command from outside of a script. In the example below, test.bash contains:

#!/bin/bash


gcloud compute ssh broadcast ls


Help! 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

Kamran (Google Cloud Support)

unread,
Jun 12, 2015, 10:13:59 AM6/12/15
to gce-dis...@googlegroups.com, dr...@lclark.edu

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 up and running including SSH. To verify this, I suggest putting several seconds delay (e.g. 30 seconds) before SSH command in your script to see if it connects.

If this is not the case, add --ssh-flag="-vvv" to your SSH command in the script to print debugging messages about errors.

Sincerely,

Kamran

Peter Drake

unread,
Jun 12, 2015, 10:42:40 AM6/12/15
to Kamran (Google Cloud Support), gce-dis...@googlegroups.com
Excellent -- adding

sleep 30s

in the script between creating the instance and sshing into it did the trick. It hadn't occurred to me that the creation command would return before all services were up and running.


Thanks!

On Fri, Jun 12, 2015 at 7:13 AM, Kamran (Google Cloud Support) <khas...@google.com> wrote:

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. 

--
© 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.
To view this discussion on the web visit https://groups.google.com/d/msgid/gce-discussion/46cdfdd1-2133-4931-a153-4718744107c0%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--

Seth Thompson

unread,
Jun 12, 2015, 1:22:04 PM6/12/15
to gce-dis...@googlegroups.com, khas...@google.com
Hi Peter,

Sounds like you've figured it out!  A newly created VM needs time to initialize the sshd service.

Let us know if you run into any additional problems.

Cheers,
Seth
Reply all
Reply to author
Forward
0 new messages