Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

grep me no patterns and I'll tell you no lines.


devel / comp.unix.shell / Struggling with this "until" loop

SubjectAuthor
* Struggling with this "until" loopOttavio Caruso
+* Re: Struggling with this "until" loopSpiros Bousbouras
|`* Re: Struggling with this "until" loopOttavio Caruso
| +* Re: Struggling with this "until" loopJanis Papanagnou
| |+- Re: Struggling with this "until" loopJanis Papanagnou
| |+- Re: Struggling with this "until" loopOttavio Caruso
| |`* 'ssh' exit status (Was: Struggling with this "until" loop)Kenny McCormack
| | `* Re: 'ssh' exit status (Was: Struggling with this "until" loop)Christian Weisgerber
| |  `* Re: 'ssh' exit status (Was: Struggling with this "until" loop)Kenny McCormack
| |   +* Re: 'ssh' exit status (Was: Struggling with this "until" loop)William Unruh
| |   |`* Re: 'ssh' exit status (Was: Struggling with this "until" loop)Kenny McCormack
| |   | +- Re: 'ssh' exit status (Was: Struggling with this "until" loop)William Unruh
| |   | `* Re: 'ssh' exit status (Was: Struggling with this "until" loop)Richard Harnden
| |   |  `- Re: 'ssh' exit status (Was: Struggling with this "until" loop)Kenny McCormack
| |   `* Re: 'ssh' exit status (Was: Struggling with this "until" loop)Christian Weisgerber
| |    +- Re: 'ssh' exit status (Was: Struggling with this "until" loop)Christian Weisgerber
| |    `* Re: 'ssh' exit status (Was: Struggling with this "until" loop)Kenny McCormack
| |     +- Re: 'ssh' exit status (Was: Struggling with this "until" loop)Christian Weisgerber
| |     `* Re: 'ssh' exit status (Was: Struggling with this "until" loop)Spiros Bousbouras
| |      `- Re: 'ssh' exit status (Was: Struggling with this "until" loop)Kenny McCormack
| `- Re: Struggling with this "until" loopSpiros Bousbouras
+- Re: Struggling with this "until" loopWilliam Unruh
`- Re: Struggling with this "until" loopWilliam Unruh

1
Struggling with this "until" loop

<s497dh$6t2$1@dont-email.me>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=3568&group=comp.unix.shell#3568

 copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: ottavio2...@yahoo.com (Ottavio Caruso)
Newsgroups: comp.unix.shell
Subject: Struggling with this "until" loop
Date: Sat, 3 Apr 2021 09:01:52 +0100
Organization: A noiseless patient Spider
Lines: 55
Message-ID: <s497dh$6t2$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 3 Apr 2021 08:01:53 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="3c148b738e7022ac4ab4610cc7dbb38b";
logging-data="7074"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+prShdej5sVif5hqbt0UQYyyXR66LJzoo="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.3.1
Cancel-Lock: sha1:MsWB3mb2OGw2PMxWJWEzRPfakHc=
X-No-Archive: Yes
Content-Language: en-GB
X-Mozilla-News-Host: snews://news.eternal-september.org:563
 by: Ottavio Caruso - Sat, 3 Apr 2021 08:01 UTC

Hi,

I am sure there is more than one way to skin a cat, but, for the purpose
of using a shell script, I am trying to troubleshoot a miscreant.

My goal is:

1) boot a qemu VM
2) connect to the VM using ssh;
a) if sshd in the guest is not listening, output an error message,
wait 10 seconds and retry, else:
b) exit the loop.

What happens instead is:

1) qemu VM boots fine (I know that because I can telnet to the qemu
monitor);
2) error message doesn't display;
3) eventually it will connect to VM instance, but:
4) upon shutdown of the VM the loop restarts, while I'd have expected
exiting from the script.

I am considering using a nested if ... fi statement, or maybe bash
instead of sh, but this should have worked with "until".

This is the script. Please note that "&" is not needed after qemu
because "-daemonize" will detach it from the terminal already; however,
adding it doesn't make much difference.

$ cat test/boot-netbsd-virtio
#!/bin/sh
qemu-system-x86_64 \
-drive if=virtio,file=/home/oc/VM/img/netbsd.image,index=0,media=disk \
-drive if=virtio,file=/home/oc/VM/img/netbsd.image.old,index=1,media=disk \
-M q35,accel=kvm -m 250M -cpu host -smp $(nproc) \
-nic user,hostfwd=tcp:127.0.0.1:5555-:22,model=virtio-net-pci,ipv6=off \
-daemonize -display none -vga none \
-monitor telnet:127.0.0.1:6665,server,nowait \
-pidfile /home/oc/VM/pid/netbsd-pid -nodefaults

until (ssh 127.0.0.1 -p 5555)
do
echo "ssh not available..."
sleep 10
done

--
Ottavio Caruso

Re: Struggling with this "until" loop

<91Y9I.257971$7Kb.228854@fx37.ams4>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=3572&group=comp.unix.shell#3572

 copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!aioe.org!news.uzoreto.com!news-out.netnews.com!news.alt.net!fdc2.netnews.com!peer04.ams1!peer.ams1.xlned.com!news.xlned.com!peer03.ams4!peer.am4.highwinds-media.com!news.highwinds-media.com!fx37.ams4.POSTED!not-for-mail
From: spi...@gmail.com (Spiros Bousbouras)
Newsgroups: comp.unix.shell
Subject: Re: Struggling with this "until" loop
References: <s497dh$6t2$1@dont-email.me>
In-Reply-To: <s497dh$6t2$1@dont-email.me>
X-Organisation: Weyland-Yutani
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
Lines: 36
Message-ID: <91Y9I.257971$7Kb.228854@fx37.ams4>
X-Complaints-To: http://netreport.virginmedia.com
NNTP-Posting-Date: Sat, 03 Apr 2021 11:08:53 UTC
Organization: virginmedia.com
Date: Sat, 03 Apr 2021 11:08:53 GMT
X-Received-Bytes: 2006
 by: Spiros Bousbouras - Sat, 3 Apr 2021 11:08 UTC

On Sat, 3 Apr 2021 09:01:52 +0100
Ottavio Caruso <ottavio2006-usenet2012@yahoo.com> wrote:
>
> $ cat test/boot-netbsd-virtio
> #!/bin/sh
> qemu-system-x86_64 \
> -drive if=virtio,file=/home/oc/VM/img/netbsd.image,index=0,media=disk \
> -drive if=virtio,file=/home/oc/VM/img/netbsd.image.old,index=1,media=disk \
> -M q35,accel=kvm -m 250M -cpu host -smp $(nproc) \
> -nic user,hostfwd=tcp:127.0.0.1:5555-:22,model=virtio-net-pci,ipv6=off \
> -daemonize -display none -vga none \
> -monitor telnet:127.0.0.1:6665,server,nowait \
> -pidfile /home/oc/VM/pid/netbsd-pid -nodefaults
>
>
> until (ssh 127.0.0.1 -p 5555)
> do
> echo "ssh not available..."
> sleep 10
> done

What happens if you start qemu and then simply type on some terminal
ssh 127.0.0.1 -p 5555

? When and how does ssh terminate ? What is the exit code ? Does the script
work if you use a different shell ?

I also don't see why you can't just write
until ssh 127.0.0.1 -p 5555

i.e. not execute ssh in a subshell. I'm not saying this is the cause for
the script not working as you expect , just a suggested simplification.

--
Hyperbole totally rips into this insane bar and absolutely destroys everything.
https://molivam42.wordpress.com/2018/04/22/30-language-bar-jokes/

Re: Struggling with this "until" loop

<s49qjl$g67$1@dont-email.me>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=3574&group=comp.unix.shell#3574

 copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: ottavio2...@yahoo.com (Ottavio Caruso)
Newsgroups: comp.unix.shell
Subject: Re: Struggling with this "until" loop
Date: Sat, 3 Apr 2021 14:29:25 +0100
Organization: A noiseless patient Spider
Lines: 51
Message-ID: <s49qjl$g67$1@dont-email.me>
References: <s497dh$6t2$1@dont-email.me> <91Y9I.257971$7Kb.228854@fx37.ams4>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 3 Apr 2021 13:29:26 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="3c148b738e7022ac4ab4610cc7dbb38b";
logging-data="16583"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18hr2dksZCp7uZbD1tB0PLV6hq53PNIryE="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.3.1
Cancel-Lock: sha1:fuTdjlV+53T/RXDtQ4LNAI5oHrA=
X-No-Archive: Yes
In-Reply-To: <91Y9I.257971$7Kb.228854@fx37.ams4>
Content-Language: en-GB
 by: Ottavio Caruso - Sat, 3 Apr 2021 13:29 UTC

On 03/04/2021 12:08, Spiros Bousbouras wrote:
> On Sat, 3 Apr 2021 09:01:52 +0100
> Ottavio Caruso <ottavio2006-usenet2012@yahoo.com> wrote:
>>
>> $ cat test/boot-netbsd-virtio
>> #!/bin/sh
>> qemu-system-x86_64 \
>> -drive if=virtio,file=/home/oc/VM/img/netbsd.image,index=0,media=disk \
>> -drive if=virtio,file=/home/oc/VM/img/netbsd.image.old,index=1,media=disk \
>> -M q35,accel=kvm -m 250M -cpu host -smp $(nproc) \
>> -nic user,hostfwd=tcp:127.0.0.1:5555-:22,model=virtio-net-pci,ipv6=off \
>> -daemonize -display none -vga none \
>> -monitor telnet:127.0.0.1:6665,server,nowait \
>> -pidfile /home/oc/VM/pid/netbsd-pid -nodefaults
>>
>>
>> until (ssh 127.0.0.1 -p 5555)
>> do
>> echo "ssh not available..."
>> sleep 10
>> done
>
> What happens if you start qemu and then simply type on some terminal
> ssh 127.0.0.1 -p 5555
>
> ? When and how does ssh terminate ?

If I just launch qemu, it will detach. If I ssh into the VM, it will
work as intended. Shutting down the VM will also detach from ssh as
expected.

> What is the exit code ?

How do I trap the exit code? But the problem is that the script doesn't
exit the loop.

> Does the script
> work if you use a different shell ?

I have tried launching it as bash but with no difference.

>
> I also don't see why you can't just write
> until ssh 127.0.0.1 -p 5555

Removing parentheses doesn't change the outcome.

--
Ottavio Caruso

Re: Struggling with this "until" loop

<s49to5$ihg$1@news-1.m-online.net>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=3576&group=comp.unix.shell#3576

 copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!news.mixmin.net!news2.arglkargh.de!news.karotte.org!news.space.net!news.m-online.net!.POSTED!not-for-mail
From: janis_pa...@hotmail.com (Janis Papanagnou)
Newsgroups: comp.unix.shell
Subject: Re: Struggling with this "until" loop
Date: Sat, 3 Apr 2021 16:23:01 +0200
Organization: (posted via) M-net Telekommunikations GmbH
Lines: 71
Message-ID: <s49to5$ihg$1@news-1.m-online.net>
References: <s497dh$6t2$1@dont-email.me> <91Y9I.257971$7Kb.228854@fx37.ams4>
<s49qjl$g67$1@dont-email.me>
NNTP-Posting-Host: aftr-62-216-206-167.dynamic.mnet-online.de
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
X-Trace: news-1.m-online.net 1617459781 18992 62.216.206.167 (3 Apr 2021 14:23:01 GMT)
X-Complaints-To: news@news-1.m-online.net
NNTP-Posting-Date: Sat, 3 Apr 2021 14:23:01 +0000 (UTC)
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
X-Enigmail-Draft-Status: N1110
In-Reply-To: <s49qjl$g67$1@dont-email.me>
 by: Janis Papanagnou - Sat, 3 Apr 2021 14:23 UTC

On 03.04.2021 15:29, Ottavio Caruso wrote:
> On 03/04/2021 12:08, Spiros Bousbouras wrote:
>> On Sat, 3 Apr 2021 09:01:52 +0100
>> Ottavio Caruso <ottavio2006-usenet2012@yahoo.com> wrote:
>>>
>>> $ cat test/boot-netbsd-virtio
>>> #!/bin/sh
>>> qemu-system-x86_64 \
>>> -drive if=virtio,file=/home/oc/VM/img/netbsd.image,index=0,media=disk \
>>> -drive
>>> if=virtio,file=/home/oc/VM/img/netbsd.image.old,index=1,media=disk \
>>> -M q35,accel=kvm -m 250M -cpu host -smp $(nproc) \
>>> -nic
>>> user,hostfwd=tcp:127.0.0.1:5555-:22,model=virtio-net-pci,ipv6=off \
>>> -daemonize -display none -vga none \
>>> -monitor telnet:127.0.0.1:6665,server,nowait \
>>> -pidfile /home/oc/VM/pid/netbsd-pid -nodefaults
>>>
>>>
>>> until (ssh 127.0.0.1 -p 5555)
>>> do
>>> echo "ssh not available..."
>>> sleep 10
>>> done
>>
>> What happens if you start qemu and then simply type on some terminal
>> ssh 127.0.0.1 -p 5555
>>
>> ? When and how does ssh terminate ?
>
> If I just launch qemu, it will detach. If I ssh into the VM, it will
> work as intended. Shutting down the VM will also detach from ssh as
> expected.
>
>> What is the exit code ?
>
> How do I trap the exit code? But the problem is that the script doesn't
> exit the loop.

The man page says:

"ssh exits with the exit status of the remote command or
with 255 if an error occurred."

Are you expecting an error from the ssh call after the connection has
been established?

If regular operation does not lead to an error the loop will continue.
Will the termination of qemu refuse subsequent ssh connections or will
the connection stay open? (A loop condition based on ssh might not be
effective as the logic is designed here. Just asking; I don't know how
qemu works and how exit codes are passed through qemu to ssh which in
the construct must create an error exit code to terminate the loop.)

Janis

>
>> Does the script
>> work if you use a different shell ?
>
> I have tried launching it as bash but with no difference.
>
>
>>
>> I also don't see why you can't just write
>> until ssh 127.0.0.1 -p 5555
>
> Removing parentheses doesn't change the outcome.
>
>

Re: Struggling with this "until" loop

<s4a10r$ji2$1@news-1.m-online.net>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=3580&group=comp.unix.shell#3580

 copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!news.mixmin.net!news2.arglkargh.de!news.karotte.org!news.space.net!news.m-online.net!.POSTED!not-for-mail
From: janis_pa...@hotmail.com (Janis Papanagnou)
Newsgroups: comp.unix.shell
Subject: Re: Struggling with this "until" loop
Date: Sat, 3 Apr 2021 17:18:50 +0200
Organization: (posted via) M-net Telekommunikations GmbH
Lines: 9
Message-ID: <s4a10r$ji2$1@news-1.m-online.net>
References: <s497dh$6t2$1@dont-email.me> <91Y9I.257971$7Kb.228854@fx37.ams4>
<s49qjl$g67$1@dont-email.me> <s49to5$ihg$1@news-1.m-online.net>
NNTP-Posting-Host: aftr-62-216-206-167.dynamic.mnet-online.de
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
X-Trace: news-1.m-online.net 1617463131 20034 62.216.206.167 (3 Apr 2021 15:18:51 GMT)
X-Complaints-To: news@news-1.m-online.net
NNTP-Posting-Date: Sat, 3 Apr 2021 15:18:51 +0000 (UTC)
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
In-Reply-To: <s49to5$ihg$1@news-1.m-online.net>
 by: Janis Papanagnou - Sat, 3 Apr 2021 15:18 UTC

On 03.04.2021 16:23, Janis Papanagnou wrote:
> [...]
> the construct must create an error exit code to terminate the loop.)

Please ignore that.

Janis

Re: Struggling with this "until" loop

<s4a1rv$g9a$1@dont-email.me>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=3581&group=comp.unix.shell#3581

 copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: unr...@invalid.ca (William Unruh)
Newsgroups: comp.unix.shell
Subject: Re: Struggling with this "until" loop
Date: Sat, 3 Apr 2021 15:33:20 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 70
Message-ID: <s4a1rv$g9a$1@dont-email.me>
References: <s497dh$6t2$1@dont-email.me>
Injection-Date: Sat, 3 Apr 2021 15:33:20 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="4d5eff4498dc25a43cd1a2ee7548eb37";
logging-data="16682"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18vy4hFeq1nM1JF9Yg/RjdM"
User-Agent: slrn/1.0.3 (Linux)
Cancel-Lock: sha1:NXuTcrSpw63o0FyaUM8LXwf+NB8=
 by: William Unruh - Sat, 3 Apr 2021 15:33 UTC

On 2021-04-03, Ottavio Caruso <ottavio2006-usenet2012@yahoo.com> wrote:
> Hi,
>
> I am sure there is more than one way to skin a cat, but, for the purpose
> of using a shell script, I am trying to troubleshoot a miscreant.
>
> My goal is:
>
> 1) boot a qemu VM
> 2) connect to the VM using ssh;
> a) if sshd in the guest is not listening, output an error message,
> wait 10 seconds and retry, else:
> b) exit the loop.
>
> What happens instead is:
>
> 1) qemu VM boots fine (I know that because I can telnet to the qemu
> monitor);
> 2) error message doesn't display;
> 3) eventually it will connect to VM instance, but:
> 4) upon shutdown of the VM the loop restarts, while I'd have expected
> exiting from the script.
>
> I am considering using a nested if ... fi statement, or maybe bash
> instead of sh, but this should have worked with "until".

It sounds to me like the ssh call is just hanging, waiting for a
connection that never occurs.
The port call never returns an error code, because no error has yet
occured.
This happens to me in the following sense. I use a autossh to open a
connection between a remote machine and mine. The remote machine goes to
sleep or something (I have not figured out what it does, because it
seems to have kept running. I try ssh into the opened port on the local
machine, and ssh just hangs for ever (^C will get me out). Sounds like
something similar has happened. Maybe a race. You should perhaps wait 10
sec before you try to start the process.

Ie, This has nothing to do with "until". It is a problem that the port
forwarded ssh.

>
> This is the script. Please note that "&" is not needed after qemu
> because "-daemonize" will detach it from the terminal already; however,
> adding it doesn't make much difference.
>
>
>
>
> $ cat test/boot-netbsd-virtio
> #!/bin/sh
> qemu-system-x86_64 \
> -drive if=virtio,file=/home/oc/VM/img/netbsd.image,index=0,media=disk \
> -drive if=virtio,file=/home/oc/VM/img/netbsd.image.old,index=1,media=disk \
> -M q35,accel=kvm -m 250M -cpu host -smp $(nproc) \
> -nic user,hostfwd=tcp:127.0.0.1:5555-:22,model=virtio-net-pci,ipv6=off \
> -daemonize -display none -vga none \
> -monitor telnet:127.0.0.1:6665,server,nowait \
> -pidfile /home/oc/VM/pid/netbsd-pid -nodefaults
>
>
> until (ssh 127.0.0.1 -p 5555)
> do
> echo "ssh not available..."
> sleep 10
> done
>
>
>
>

Re: Struggling with this "until" loop

<s4a1v5$pmq$1@dont-email.me>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=3582&group=comp.unix.shell#3582

 copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: ottavio2...@yahoo.com (Ottavio Caruso)
Newsgroups: comp.unix.shell
Subject: Re: Struggling with this "until" loop
Date: Sat, 3 Apr 2021 16:35:00 +0100
Organization: A noiseless patient Spider
Lines: 40
Message-ID: <s4a1v5$pmq$1@dont-email.me>
References: <s497dh$6t2$1@dont-email.me> <91Y9I.257971$7Kb.228854@fx37.ams4>
<s49qjl$g67$1@dont-email.me> <s49to5$ihg$1@news-1.m-online.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 3 Apr 2021 15:35:01 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="3c148b738e7022ac4ab4610cc7dbb38b";
logging-data="26330"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18XsTVbtlXnOtyxEbFnxeb2eX7W4Lu5G+o="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.3.1
Cancel-Lock: sha1:m/PcYvQfSiArYyQvy3yfE84RL2I=
X-No-Archive: yes
In-Reply-To: <s49to5$ihg$1@news-1.m-online.net>
Content-Language: en-GB
 by: Ottavio Caruso - Sat, 3 Apr 2021 15:35 UTC

On 03/04/2021 15:23, Janis Papanagnou wrote:
> The man page says:
>
> "ssh exits with the exit status of the remote command or
> with 255 if an error occurred."
>
> Are you expecting an error from the ssh call after the connection has
> been established?
>
> If regular operation does not lead to an error the loop will continue.
> Will the termination of qemu refuse subsequent ssh connections or will
> the connection stay open?

You might be on to something here: by shutting down the VM, I haven't
actually exited ssh, or alternatively I haven't exited the until loop,
therefore the loop will restart (or at least this is how I see it now).

If I just exit ssh (either via exit or ctrl-d), the loop will stop.

If this is the case, I'll have to find another way of trapping if sshd
is listening, probably netstat or nmap, and/or using two nested
if...then statements.

Last minute thought: or...

I could wrap both qemu and ssh into the until loop:

QEMU="<qemu command line parameters>

SSH="ssh 127.0.0.1 -p 5554"

until $QEMU && $SSH
do
....
done

I need to try that tomorrow, as I can't restart the VM right now.

--
Ottavio Caruso

'ssh' exit status (Was: Struggling with this "until" loop)

<s4acam$m5sp$1@news.xmission.com>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=3584&group=comp.unix.shell#3584

 copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!xmission!nnrp.xmission!.POSTED.shell.xmission.com!not-for-mail
From: gaze...@shell.xmission.com (Kenny McCormack)
Newsgroups: comp.unix.shell
Subject: 'ssh' exit status (Was: Struggling with this "until" loop)
Date: Sat, 3 Apr 2021 18:31:50 -0000 (UTC)
Organization: The official candy of the new Millennium
Message-ID: <s4acam$m5sp$1@news.xmission.com>
References: <s497dh$6t2$1@dont-email.me> <91Y9I.257971$7Kb.228854@fx37.ams4> <s49qjl$g67$1@dont-email.me> <s49to5$ihg$1@news-1.m-online.net>
Injection-Date: Sat, 3 Apr 2021 18:31:50 -0000 (UTC)
Injection-Info: news.xmission.com; posting-host="shell.xmission.com:166.70.8.4";
logging-data="726937"; mail-complaints-to="abuse@xmission.com"
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
Originator: gazelle@shell.xmission.com (Kenny McCormack)
 by: Kenny McCormack - Sat, 3 Apr 2021 18:31 UTC

In article <s49to5$ihg$1@news-1.m-online.net>,
Janis Papanagnou <janis_papanagnou@hotmail.com> wrote:
....
>The man page says:
>
> "ssh exits with the exit status of the remote command or
> with 255 if an error occurred."
>
>Are you expecting an error from the ssh call after the connection has
>been established?

Note that an interactive ssh (that is, one without a command at the end of
the command line) will never return 0 exit status.

At least I wasn't able to get it to...

--
"I have a simple philosophy. Fill what's empty. Empty what's full. And
scratch where it itches."

Alice Roosevelt Longworth

Re: 'ssh' exit status (Was: Struggling with this "until" loop)

<slrns6hi4a.2b5t.naddy@lorvorc.mips.inka.de>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=3586&group=comp.unix.shell#3586

 copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!news.szaf.org!inka.de!mips.inka.de!.POSTED.localhost!not-for-mail
From: nad...@mips.inka.de (Christian Weisgerber)
Newsgroups: comp.unix.shell
Subject: Re: 'ssh' exit status (Was: Struggling with this "until" loop)
Date: Sat, 3 Apr 2021 19:56:58 -0000 (UTC)
Message-ID: <slrns6hi4a.2b5t.naddy@lorvorc.mips.inka.de>
References: <s497dh$6t2$1@dont-email.me> <91Y9I.257971$7Kb.228854@fx37.ams4>
<s49qjl$g67$1@dont-email.me> <s49to5$ihg$1@news-1.m-online.net>
<s4acam$m5sp$1@news.xmission.com>
Injection-Date: Sat, 3 Apr 2021 19:56:58 -0000 (UTC)
Injection-Info: lorvorc.mips.inka.de; posting-host="localhost:::1";
logging-data="76990"; mail-complaints-to="usenet@mips.inka.de"
User-Agent: slrn/1.0.3 (FreeBSD)
 by: Christian Weisgerber - Sat, 3 Apr 2021 19:56 UTC

On 2021-04-03, Kenny McCormack <gazelle@shell.xmission.com> wrote:

> Note that an interactive ssh (that is, one without a command at the end of
> the command line) will never return 0 exit status.
>
> At least I wasn't able to get it to...

I think it returns the exit code of the remote shell. If you exit
normally, you get 0.

I had to think a bit how to provoke a different result: exiting by
kill -HUP $$ produced 129.

--
Christian "naddy" Weisgerber naddy@mips.inka.de

Re: 'ssh' exit status (Was: Struggling with this "until" loop)

<s4am6o$maak$1@news.xmission.com>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=3588&group=comp.unix.shell#3588

 copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!xmission!nnrp.xmission!.POSTED.shell.xmission.com!not-for-mail
From: gaze...@shell.xmission.com (Kenny McCormack)
Newsgroups: comp.unix.shell
Subject: Re: 'ssh' exit status (Was: Struggling with this "until" loop)
Date: Sat, 3 Apr 2021 21:20:24 -0000 (UTC)
Organization: The official candy of the new Millennium
Message-ID: <s4am6o$maak$1@news.xmission.com>
References: <s497dh$6t2$1@dont-email.me> <s49to5$ihg$1@news-1.m-online.net> <s4acam$m5sp$1@news.xmission.com> <slrns6hi4a.2b5t.naddy@lorvorc.mips.inka.de>
Injection-Date: Sat, 3 Apr 2021 21:20:24 -0000 (UTC)
Injection-Info: news.xmission.com; posting-host="shell.xmission.com:166.70.8.4";
logging-data="731476"; mail-complaints-to="abuse@xmission.com"
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
Originator: gazelle@shell.xmission.com (Kenny McCormack)
 by: Kenny McCormack - Sat, 3 Apr 2021 21:20 UTC

In article <slrns6hi4a.2b5t.naddy@lorvorc.mips.inka.de>,
Christian Weisgerber <naddy@mips.inka.de> wrote:
>On 2021-04-03, Kenny McCormack <gazelle@shell.xmission.com> wrote:
>
>> Note that an interactive ssh (that is, one without a command at the end of
>> the command line) will never return 0 exit status.
>>
>> At least I wasn't able to get it to...
>
>I think it returns the exit code of the remote shell. If you exit
>normally, you get 0.

You think wrong.

I just tested two things:

1) If I run bash from the a bash prompt, then immediately do ^D to exit,
the exit status of that process is 1 (yes, you'd expect it to be 0, but I
think the EOF action gets flagged as an error condition of some sort).

1a) The same thing happens if I type the word "exit" (and hit enter)
instead of ^D.

2) If I ssh to another system, and exit that shell via ^D, the exit status
of the ssh process is 127.

2a) There is some variation on this. One one system the exit status of the
ssh process was 127; on another it was 1.

But in any case, you certainly can't rely on the exit status of the 'ssh'
being 0 to indicate a successful connection - which is what OP seems to be
doing.

--
After Using Gender Slur Against AOC, GOP Rep. Yoyo Won't Apologize 'For Loving God'.

That's so sweet...

Re: 'ssh' exit status (Was: Struggling with this "until" loop)

<s4an0m$uft$1@dont-email.me>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=3589&group=comp.unix.shell#3589

 copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: unr...@invalid.ca (William Unruh)
Newsgroups: comp.unix.shell
Subject: Re: 'ssh' exit status (Was: Struggling with this "until" loop)
Date: Sat, 3 Apr 2021 21:34:14 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 45
Message-ID: <s4an0m$uft$1@dont-email.me>
References: <s497dh$6t2$1@dont-email.me> <s49to5$ihg$1@news-1.m-online.net>
<s4acam$m5sp$1@news.xmission.com>
<slrns6hi4a.2b5t.naddy@lorvorc.mips.inka.de>
<s4am6o$maak$1@news.xmission.com>
Injection-Date: Sat, 3 Apr 2021 21:34:14 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="4d5eff4498dc25a43cd1a2ee7548eb37";
logging-data="31229"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+idKRIdkli9KwydnbQZbTv"
User-Agent: slrn/1.0.3 (Linux)
Cancel-Lock: sha1:dius44/oxm4TP4ztVuV5MjyzoJk=
 by: William Unruh - Sat, 3 Apr 2021 21:34 UTC

On 2021-04-03, Kenny McCormack <gazelle@shell.xmission.com> wrote:
> In article <slrns6hi4a.2b5t.naddy@lorvorc.mips.inka.de>,
> Christian Weisgerber <naddy@mips.inka.de> wrote:
>>On 2021-04-03, Kenny McCormack <gazelle@shell.xmission.com> wrote:
>>
>>> Note that an interactive ssh (that is, one without a command at the end of
>>> the command line) will never return 0 exit status.
>>>
>>> At least I wasn't able to get it to...
>>
>>I think it returns the exit code of the remote shell. If you exit
>>normally, you get 0.
>
> You think wrong.

I think he is using the exit status to tell him if the attempt to ssh to
the virtual system failed and using that to tell him if it is running

Eg
tunnel:0.0[unruh]>ssh localhost -p 333
ssh: connect to host localhost port 333: Connection refused
tunnel:0.0[unruh]>echo $?
255

is my attempt to ssh to an unused port.
>
> I just tested two things:
>
> 1) If I run bash from the a bash prompt, then immediately do ^D to exit,
> the exit status of that process is 1 (yes, you'd expect it to be 0, but I
> think the EOF action gets flagged as an error condition of some sort).
>
> 1a) The same thing happens if I type the word "exit" (and hit enter)
> instead of ^D.
>
> 2) If I ssh to another system, and exit that shell via ^D, the exit status
> of the ssh process is 127.
>
> 2a) There is some variation on this. One one system the exit status of the
> ssh process was 127; on another it was 1.
>
> But in any case, you certainly can't rely on the exit status of the 'ssh'
> being 0 to indicate a successful connection - which is what OP seems to be
> doing.
>

Re: 'ssh' exit status (Was: Struggling with this "until" loop)

<s4anmi$maak$2@news.xmission.com>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=3590&group=comp.unix.shell#3590

 copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!xmission!nnrp.xmission!.POSTED.shell.xmission.com!not-for-mail
From: gaze...@shell.xmission.com (Kenny McCormack)
Newsgroups: comp.unix.shell
Subject: Re: 'ssh' exit status (Was: Struggling with this "until" loop)
Date: Sat, 3 Apr 2021 21:45:54 -0000 (UTC)
Organization: The official candy of the new Millennium
Message-ID: <s4anmi$maak$2@news.xmission.com>
References: <s497dh$6t2$1@dont-email.me> <slrns6hi4a.2b5t.naddy@lorvorc.mips.inka.de> <s4am6o$maak$1@news.xmission.com> <s4an0m$uft$1@dont-email.me>
Injection-Date: Sat, 3 Apr 2021 21:45:54 -0000 (UTC)
Injection-Info: news.xmission.com; posting-host="shell.xmission.com:166.70.8.4";
logging-data="731476"; mail-complaints-to="abuse@xmission.com"
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
Originator: gazelle@shell.xmission.com (Kenny McCormack)
 by: Kenny McCormack - Sat, 3 Apr 2021 21:45 UTC

In article <s4an0m$uft$1@dont-email.me>,
William Unruh <unruh@invalid.ca> wrote:
....
>I think he is using the exit status to tell him if the attempt to ssh to
>the virtual system failed and using that to tell him if it is running
>
>Eg
>tunnel:0.0[unruh]>ssh localhost -p 333
>ssh: connect to host localhost port 333: Connection refused
>tunnel:0.0[unruh]>echo $?
>255
>
>is my attempt to ssh to an unused port.

Right - but the point is that either simply doesn't work or works but only
in a brittle way.

I.e., it looks like you actually have to know and rely on the exit code of
255 to indicate "no server" (i.e., "Connection refused"). I.e., you'd be
doing something like:

ssh localhost -p 333
case $? in
0) echo "Success!";;
255) echo "No server running";;
*) echo "Any number of other possibilities, which we can't enumerate"
esac

My contention is you will never get "Success!". And you will/may get
various values other than 255 (such as 1 or 127).

I suspect that there are better ways to check to see if the port is being
listened on (yet). Perhaps, netcat or nmap?

Note also that you may get situations where it blocks - that is waits more
or less forever for a connection that never comes. So, to be safe, you
need to engineer some kind of timeout mechanism around it.

--
The people who were, are, and always will be, wrong about everything, are still
calling *us* "libtards"...

(John Fugelsang)

Re: 'ssh' exit status (Was: Struggling with this "until" loop)

<s4at73$hf0$1@dont-email.me>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=3591&group=comp.unix.shell#3591

 copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: unr...@invalid.ca (William Unruh)
Newsgroups: comp.unix.shell
Subject: Re: 'ssh' exit status (Was: Struggling with this "until" loop)
Date: Sat, 3 Apr 2021 23:20:03 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 70
Message-ID: <s4at73$hf0$1@dont-email.me>
References: <s497dh$6t2$1@dont-email.me>
<slrns6hi4a.2b5t.naddy@lorvorc.mips.inka.de>
<s4am6o$maak$1@news.xmission.com> <s4an0m$uft$1@dont-email.me>
<s4anmi$maak$2@news.xmission.com>
Injection-Date: Sat, 3 Apr 2021 23:20:03 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="381890e381466f84085a4690dfa5340e";
logging-data="17888"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/A7IGt0jRiJRHc7n7TN4Dh"
User-Agent: slrn/1.0.3 (Linux)
Cancel-Lock: sha1:zeGczQP0aqKzg086S72afDvDuOM=
 by: William Unruh - Sat, 3 Apr 2021 23:20 UTC

On 2021-04-03, Kenny McCormack <gazelle@shell.xmission.com> wrote:
> In article <s4an0m$uft$1@dont-email.me>,
> William Unruh <unruh@invalid.ca> wrote:
> ...
>>I think he is using the exit status to tell him if the attempt to ssh to
>>the virtual system failed and using that to tell him if it is running
>>
>>Eg
>>tunnel:0.0[unruh]>ssh localhost -p 333
>>ssh: connect to host localhost port 333: Connection refused
>>tunnel:0.0[unruh]>echo $?
>>255
>>
>>is my attempt to ssh to an unused port.
>
> Right - but the point is that either simply doesn't work or works but only
> in a brittle way.

As I said, the symptoms are that that the ssh is hanging, instead of
either connecting or exiting with an error code. When ssh error's out it
usually gives and error message. But I have had the situation in which
the localhost things that there is something attached to the port but
there is not, and ssh simply sits there waiting for the response from
the far side. And it will wait forever. I suspect that the problem is
that the he is trying to connect too soon. The local port has been
opened for the ssh connection but the virtual OS is not ready to process
the ssh connection. Thus neither the local linux ( who thinks there is
something attached to the port to service the ssh request) nor the
remote machine ( who is not ready) sends anything back, neither an error
nor a negotiation. As I have said, wait for 10 sec, and then try
connecting with ssh, instead of doing it a microsecond after the
virtual OS has started.

Ie put a "sleep 10" before the ssh request.
If that works you can try reducing the "10" to see how long you have to
have to wait.

>
> I.e., it looks like you actually have to know and rely on the exit code of
> 255 to indicate "no server" (i.e., "Connection refused"). I.e., you'd be

But I suspect that there IS a server, but the server is not ready. And
once it is ready, it no longer realises that there is a request to login
wating at the ssh port. I Know this can happen from my own use of ssh
port forwarding.

> doing something like:
>
> ssh localhost -p 333
> case $? in
> 0) echo "Success!";;
> 255) echo "No server running";;
> *) echo "Any number of other possibilities, which we can't enumerate"
> esac
>
> My contention is you will never get "Success!". And you will/may get
> various values other than 255 (such as 1 or 127).
>
> I suspect that there are better ways to check to see if the port is being
> listened on (yet). Perhaps, netcat or nmap?
>
> Note also that you may get situations where it blocks - that is waits more
> or less forever for a connection that never comes. So, to be safe, you
> need to engineer some kind of timeout mechanism around it.

Yes, that would certainly help. And I suspect a wait before trying would
also help.

>

Re: Struggling with this "until" loop

<ambaI.263869$7Kb.240624@fx37.ams4>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=3594&group=comp.unix.shell#3594

 copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!news.dns-netz.com!news.freedyn.net!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!50.7.236.18.MISMATCH!news-out.netnews.com!news.alt.net!fdc3.netnews.com!peer04.ams1!peer.ams1.xlned.com!news.xlned.com!peer02.ams4!peer.am4.highwinds-media.com!news.highwinds-media.com!fx37.ams4.POSTED!not-for-mail
From: spi...@gmail.com (Spiros Bousbouras)
Newsgroups: comp.unix.shell
Subject: Re: Struggling with this "until" loop
References: <s497dh$6t2$1@dont-email.me> <91Y9I.257971$7Kb.228854@fx37.ams4> <s49qjl$g67$1@dont-email.me>
In-Reply-To: <s49qjl$g67$1@dont-email.me>
X-Organisation: Weyland-Yutani
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
Lines: 55
Message-ID: <ambaI.263869$7Kb.240624@fx37.ams4>
X-Complaints-To: http://netreport.virginmedia.com
NNTP-Posting-Date: Sun, 04 Apr 2021 04:35:18 UTC
Organization: virginmedia.com
Date: Sun, 04 Apr 2021 04:35:18 GMT
X-Received-Bytes: 2633
 by: Spiros Bousbouras - Sun, 4 Apr 2021 04:35 UTC

On Sat, 3 Apr 2021 14:29:25 +0100
Ottavio Caruso <ottavio2006-usenet2012@yahoo.com> wrote:
> On 03/04/2021 12:08, Spiros Bousbouras wrote:
> > On Sat, 3 Apr 2021 09:01:52 +0100
> > Ottavio Caruso <ottavio2006-usenet2012@yahoo.com> wrote:
> >>
> >> $ cat test/boot-netbsd-virtio
> >> #!/bin/sh
> >> qemu-system-x86_64 \
> >> -drive if=virtio,file=/home/oc/VM/img/netbsd.image,index=0,media=disk \
> >> -drive if=virtio,file=/home/oc/VM/img/netbsd.image.old,index=1,media=disk \
> >> -M q35,accel=kvm -m 250M -cpu host -smp $(nproc) \
> >> -nic user,hostfwd=tcp:127.0.0.1:5555-:22,model=virtio-net-pci,ipv6=off \
> >> -daemonize -display none -vga none \
> >> -monitor telnet:127.0.0.1:6665,server,nowait \
> >> -pidfile /home/oc/VM/pid/netbsd-pid -nodefaults
> >>
> >>
> >> until (ssh 127.0.0.1 -p 5555)
> >> do
> >> echo "ssh not available..."
> >> sleep 10
> >> done
> >
> > What happens if you start qemu and then simply type on some terminal
> > ssh 127.0.0.1 -p 5555
> >
> > ? When and how does ssh terminate ?
>
> If I just launch qemu, it will detach. If I ssh into the VM, it will
> work as intended. Shutting down the VM will also detach from ssh as
> expected.

What does "work as intended" mean ? Do you execute some commands on the
remote virtual machine ? Do you terminate qemu and then ssh also
terminates ? In either case what is the exit code ?

> > What is the exit code ?
>
> How do I trap the exit code? But the problem is that the script doesn't
> exit the loop.

echo $?

right after ssh has terminated. You might also try
ssh 127.0.0.1 -p 5555 /bin/true

and see what happens.

> > I also don't see why you can't just write
> > until ssh 127.0.0.1 -p 5555
>
> Removing parentheses doesn't change the outcome.

I didn't suggest that it would.

Re: Struggling with this "until" loop

<s4bn1r$t4f$1@dont-email.me>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=3595&group=comp.unix.shell#3595

 copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: unr...@invalid.ca (William Unruh)
Newsgroups: comp.unix.shell
Subject: Re: Struggling with this "until" loop
Date: Sun, 4 Apr 2021 06:40:59 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 92
Message-ID: <s4bn1r$t4f$1@dont-email.me>
References: <s497dh$6t2$1@dont-email.me>
Injection-Date: Sun, 4 Apr 2021 06:40:59 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="381890e381466f84085a4690dfa5340e";
logging-data="29839"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+2E7rH0J08iAuhDbxx/4yW"
User-Agent: slrn/1.0.3 (Linux)
Cancel-Lock: sha1:QZ0e1WpuTggkDbCcoexcyGQO0ro=
 by: William Unruh - Sun, 4 Apr 2021 06:40 UTC

On 2021-04-03, Ottavio Caruso <ottavio2006-usenet2012@yahoo.com> wrote:
> Hi,
>
> I am sure there is more than one way to skin a cat, but, for the purpose
> of using a shell script, I am trying to troubleshoot a miscreant.
>
> My goal is:
>
> 1) boot a qemu VM
> 2) connect to the VM using ssh;
> a) if sshd in the guest is not listening, output an error message,
> wait 10 seconds and retry, else:
> b) exit the loop.
>
> What happens instead is:
>
> 1) qemu VM boots fine (I know that because I can telnet to the qemu
> monitor);
> 2) error message doesn't display;

BEcause your Linux sees that the remote machine has reserved the port,
and ships the ssh hello off to the qeme. However qemu is not ready so it
just delays answering (ie there is nothing on the other end, like an
sshd, listening on that port). Eventually it your ssh gets tired of just
waiting and sends another hello and the sshd on qemu answers.

> 3) eventually it will connect to VM instance, but:
> 4) upon shutdown of the VM the loop restarts, while I'd have expected
> exiting from the script.

When you shut down the qemu, Linux sees that the port that ssh is
connected to no longer has anything at the other end to respond, so it
comes back with and error. Your script sees the error code and says, "OK
lets try again, the remote end did not answer. And off it goes again.
only now there really is nothing at the other end of the line and Linux
keeps returning error codes, and your script keeps trying again, ad
infinitum.

a) You need to wait to send the first ssh attempt so that the qemu and
its ssh is properly set up. Put in a "sleep 10" before the first
attempt.
b) you need to timeout the connection, so that if you are connected, and
then after 10 sec you are disconnected, it stops trying to connect. (or
if it gets say three non-zero error codes, it quits.)

>
> I am considering using a nested if ... fi statement, or maybe bash
> instead of sh, but this should have worked with "until".
>
> This is the script. Please note that "&" is not needed after qemu
> because "-daemonize" will detach it from the terminal already; however,
> adding it doesn't make much difference.
>
>
>
>
> $ cat test/boot-netbsd-virtio
> #!/bin/sh
> qemu-system-x86_64 \
> -drive if=virtio,file=/home/oc/VM/img/netbsd.image,index=0,media=disk \
> -drive if=virtio,file=/home/oc/VM/img/netbsd.image.old,index=1,media=disk \
> -M q35,accel=kvm -m 250M -cpu host -smp $(nproc) \
> -nic user,hostfwd=tcp:127.0.0.1:5555-:22,model=virtio-net-pci,ipv6=off \
> -daemonize -display none -vga none \
> -monitor telnet:127.0.0.1:6665,server,nowait \
> -pidfile /home/oc/VM/pid/netbsd-pid -nodefaults
>
>
> until (ssh 127.0.0.1 -p 5555)

This first attempt will occur a few microseconds after the above
qemu-system-x85_64 command, far far too soon for qemu to have been
properly set up and set up sshd to listen to that port 5555. But your
sshd will patiently wait for a response to its first attempt to connect,
until it times out (not sure what the timeout is but it seems to be at
least minutes).

> do
> echo "ssh not available..."
> sleep 10
> done

And when qemu shuts down, the until from the successful attempt will see
an error code from that last successful ssh, and thus will try again and
again and again, because now ssh will return with error because qemu has not reserved
that port 5555 on the linux system so there is nowhere to ship off the
packet to. It is called a race. and your system always loses.

>
>
>
>

Re: 'ssh' exit status (Was: Struggling with this "until" loop)

<s4bvd4$htc$1@dont-email.me>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=3596&group=comp.unix.shell#3596

 copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: richard....@gmail.com (Richard Harnden)
Newsgroups: comp.unix.shell
Subject: Re: 'ssh' exit status (Was: Struggling with this "until" loop)
Date: Sun, 4 Apr 2021 10:03:32 +0100
Organization: A noiseless patient Spider
Lines: 43
Message-ID: <s4bvd4$htc$1@dont-email.me>
References: <s497dh$6t2$1@dont-email.me>
<slrns6hi4a.2b5t.naddy@lorvorc.mips.inka.de>
<s4am6o$maak$1@news.xmission.com> <s4an0m$uft$1@dont-email.me>
<s4anmi$maak$2@news.xmission.com>
Reply-To: nospam.harnden@gmail.com
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 4 Apr 2021 09:03:33 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="f257c9a0cd473837675c68b6cafff874";
logging-data="18348"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+Ucj6guH+IxYx/AjyUfeyLKxFSHIZ40Lo="
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0)
Gecko/20100101 Thunderbird/78.9.0
Cancel-Lock: sha1:k80n/ui/wx1qWgXcayxXeArr+sg=
In-Reply-To: <s4anmi$maak$2@news.xmission.com>
Content-Language: en-GB
 by: Richard Harnden - Sun, 4 Apr 2021 09:03 UTC

On 03/04/2021 22:45, Kenny McCormack wrote:
> In article <s4an0m$uft$1@dont-email.me>,
> William Unruh <unruh@invalid.ca> wrote:
> ...
>> I think he is using the exit status to tell him if the attempt to ssh to
>> the virtual system failed and using that to tell him if it is running
>>
>> Eg
>> tunnel:0.0[unruh]>ssh localhost -p 333
>> ssh: connect to host localhost port 333: Connection refused
>> tunnel:0.0[unruh]>echo $?
>> 255
>>
>> is my attempt to ssh to an unused port.
>
> Right - but the point is that either simply doesn't work or works but only
> in a brittle way.
>
> I.e., it looks like you actually have to know and rely on the exit code of
> 255 to indicate "no server" (i.e., "Connection refused"). I.e., you'd be
> doing something like:
>
> ssh localhost -p 333
> case $? in
> 0) echo "Success!";;
> 255) echo "No server running";;
> *) echo "Any number of other possibilities, which we can't enumerate"
> esac
>
> My contention is you will never get "Success!". And you will/may get
> various values other than 255 (such as 1 or 127).

ssh localhost -p 333 "exit 0"

>
> I suspect that there are better ways to check to see if the port is being
> listened on (yet). Perhaps, netcat or nmap?
>
> Note also that you may get situations where it blocks - that is waits more
> or less forever for a connection that never comes. So, to be safe, you
> need to engineer some kind of timeout mechanism around it.
>

Re: 'ssh' exit status (Was: Struggling with this "until" loop)

<s4c48l$n38g$1@news.xmission.com>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=3597&group=comp.unix.shell#3597

 copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!xmission!nnrp.xmission!.POSTED.shell.xmission.com!not-for-mail
From: gaze...@shell.xmission.com (Kenny McCormack)
Newsgroups: comp.unix.shell
Subject: Re: 'ssh' exit status (Was: Struggling with this "until" loop)
Date: Sun, 4 Apr 2021 10:26:29 -0000 (UTC)
Organization: The official candy of the new Millennium
Message-ID: <s4c48l$n38g$1@news.xmission.com>
References: <s497dh$6t2$1@dont-email.me> <s4an0m$uft$1@dont-email.me> <s4anmi$maak$2@news.xmission.com> <s4bvd4$htc$1@dont-email.me>
Injection-Date: Sun, 4 Apr 2021 10:26:29 -0000 (UTC)
Injection-Info: news.xmission.com; posting-host="shell.xmission.com:166.70.8.4";
logging-data="757008"; mail-complaints-to="abuse@xmission.com"
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
Originator: gazelle@shell.xmission.com (Kenny McCormack)
 by: Kenny McCormack - Sun, 4 Apr 2021 10:26 UTC

In article <s4bvd4$htc$1@dont-email.me>,
Richard Harnden <nospam.harnden@gmail.com> wrote:
....
>> My contention is you will never get "Success!". And you will/may get
>> various values other than 255 (such as 1 or 127).
>
>ssh localhost -p 333 "exit 0"

Right. But the original condition was "From a 'command-less' ssh".

I.e., from an ssh that gets you an interactive shell.

--
I shot a man on Fifth Aveneue, just to see him die.

Re: 'ssh' exit status (Was: Struggling with this "until" loop)

<slrns6jhud.2uih.naddy@lorvorc.mips.inka.de>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=3598&group=comp.unix.shell#3598

 copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!news.szaf.org!inka.de!mips.inka.de!.POSTED.localhost!not-for-mail
From: nad...@mips.inka.de (Christian Weisgerber)
Newsgroups: comp.unix.shell
Subject: Re: 'ssh' exit status (Was: Struggling with this "until" loop)
Date: Sun, 4 Apr 2021 14:06:05 -0000 (UTC)
Message-ID: <slrns6jhud.2uih.naddy@lorvorc.mips.inka.de>
References: <s497dh$6t2$1@dont-email.me> <s49to5$ihg$1@news-1.m-online.net>
<s4acam$m5sp$1@news.xmission.com>
<slrns6hi4a.2b5t.naddy@lorvorc.mips.inka.de>
<s4am6o$maak$1@news.xmission.com>
Injection-Date: Sun, 4 Apr 2021 14:06:05 -0000 (UTC)
Injection-Info: lorvorc.mips.inka.de; posting-host="localhost:::1";
logging-data="96850"; mail-complaints-to="usenet@mips.inka.de"
User-Agent: slrn/1.0.3 (FreeBSD)
 by: Christian Weisgerber - Sun, 4 Apr 2021 14:06 UTC

On 2021-04-03, Kenny McCormack <gazelle@shell.xmission.com> wrote:

>>> Note that an interactive ssh (that is, one without a command at the end of
>>> the command line) will never return 0 exit status.
>>>
>>> At least I wasn't able to get it to...
>>
>>I think it returns the exit code of the remote shell. If you exit
>>normally, you get 0.
>
> You think wrong.

Anybody who doubts that ssh returns the exit code of the remote
shell, as documented, should go ahead and try it. If you exit the
remote interactive shell with "exit N", ssh will return the exit
code N.

> 1) If I run bash from the a bash prompt, then immediately do ^D to exit,
> the exit status of that process is 1 (yes, you'd expect it to be 0, but I
> think the EOF action gets flagged as an error condition of some sort).
>
> 1a) The same thing happens if I type the word "exit" (and hit enter)
> instead of ^D.

The shell returns the exit code of the last command it ran, i.e.,
plain "exit" is NOT equivalent to "exit 0" but to "exit $?".
If you run commands from .profile, .bashrc, or such, then the last
one determines the exit status when you immediately exit.

> But in any case, you certainly can't rely on the exit status of the 'ssh'
> being 0 to indicate a successful connection

Correct.

You can check for an exit status of 255 to indicate that the
connection failed, albeit with the remote chance that it actually
succeeded and the remote command returned 255.

--
Christian "naddy" Weisgerber naddy@mips.inka.de

Re: 'ssh' exit status (Was: Struggling with this "until" loop)

<slrns6kat8.7bi.naddy@lorvorc.mips.inka.de>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=3600&group=comp.unix.shell#3600

 copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!news.szaf.org!inka.de!mips.inka.de!.POSTED.localhost!not-for-mail
From: nad...@mips.inka.de (Christian Weisgerber)
Newsgroups: comp.unix.shell
Subject: Re: 'ssh' exit status (Was: Struggling with this "until" loop)
Date: Sun, 4 Apr 2021 21:12:08 -0000 (UTC)
Message-ID: <slrns6kat8.7bi.naddy@lorvorc.mips.inka.de>
References: <s497dh$6t2$1@dont-email.me> <s49to5$ihg$1@news-1.m-online.net>
<s4acam$m5sp$1@news.xmission.com>
<slrns6hi4a.2b5t.naddy@lorvorc.mips.inka.de>
<s4am6o$maak$1@news.xmission.com>
<slrns6jhud.2uih.naddy@lorvorc.mips.inka.de>
Injection-Date: Sun, 4 Apr 2021 21:12:08 -0000 (UTC)
Injection-Info: lorvorc.mips.inka.de; posting-host="localhost:::1";
logging-data="7539"; mail-complaints-to="usenet@mips.inka.de"
User-Agent: slrn/1.0.3 (FreeBSD)
 by: Christian Weisgerber - Sun, 4 Apr 2021 21:12 UTC

On 2021-04-04, Christian Weisgerber <naddy@mips.inka.de> wrote:

> Anybody who doubts that ssh returns the exit code of the remote
> shell, as documented, should go ahead and try it. If you exit the
> remote interactive shell with "exit N", ssh will return the exit
> code N.

Note that when the remote shell exits due to a signal, ssh's exit
status can vary due to an interaction between the behavior of the
remote login shell and an implementation gap in ssh(1).

I reported this corner case to the OpenSSH mailing list:
https://lists.mindrot.org/pipermail/openssh-unix-dev/2021-April/039273.html

--
Christian "naddy" Weisgerber naddy@mips.inka.de

Re: 'ssh' exit status (Was: Struggling with this "until" loop)

<s4hnil$pj97$1@news.xmission.com>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=3607&group=comp.unix.shell#3607

 copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!xmission!nnrp.xmission!.POSTED.shell.xmission.com!not-for-mail
From: gaze...@shell.xmission.com (Kenny McCormack)
Newsgroups: comp.unix.shell
Subject: Re: 'ssh' exit status (Was: Struggling with this "until" loop)
Date: Tue, 6 Apr 2021 13:26:45 -0000 (UTC)
Organization: The official candy of the new Millennium
Message-ID: <s4hnil$pj97$1@news.xmission.com>
References: <s497dh$6t2$1@dont-email.me> <slrns6hi4a.2b5t.naddy@lorvorc.mips.inka.de> <s4am6o$maak$1@news.xmission.com> <slrns6jhud.2uih.naddy@lorvorc.mips.inka.de>
Injection-Date: Tue, 6 Apr 2021 13:26:45 -0000 (UTC)
Injection-Info: news.xmission.com; posting-host="shell.xmission.com:166.70.8.4";
logging-data="838951"; mail-complaints-to="abuse@xmission.com"
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
Originator: gazelle@shell.xmission.com (Kenny McCormack)
 by: Kenny McCormack - Tue, 6 Apr 2021 13:26 UTC

In article <slrns6jhud.2uih.naddy@lorvorc.mips.inka.de>,
Christian Weisgerber <naddy@mips.inka.de> wrote:
....
>> 1) If I run bash from the a bash prompt, then immediately do ^D to exit,
>> the exit status of that process is 1 (yes, you'd expect it to be 0, but I
>> think the EOF action gets flagged as an error condition of some sort).
>>
>> 1a) The same thing happens if I type the word "exit" (and hit enter)
>> instead of ^D.
>
>The shell returns the exit code of the last command it ran, i.e.,
>plain "exit" is NOT equivalent to "exit 0" but to "exit $?".
>If you run commands from .profile, .bashrc, or such, then the last
>one determines the exit status when you immediately exit.

I believe (now) that you are correct - in that exiting the interactive
shell via "exit 0" will cause the ssh process to set its exit status to 0.
(Presumably, "exit N" should also set it to N, for any value of N)

But nobody does that. People exit interactive shells either via ^D (most
common) or via a command like exit, bye, quit, stop or whatever their shell
supports. I believe that in all such cases, you will get a non-zero exit
status passed back to ssh. That is, the mere act of hitting ^D (or any of
the equivalent commands) sets a non-zero value in $?

I admit I haven't thoroughly tested this last, but it syncs with my
experience.

>> But in any case, you certainly can't rely on the exit status of the 'ssh'
>> being 0 to indicate a successful connection
>
>Correct.
>
>You can check for an exit status of 255 to indicate that the
>connection failed, albeit with the remote chance that it actually
>succeeded and the remote command returned 255.

Yes, we agree on this. Checking for specifically 255 seems awfully shaky
and brittle to me. I wouldn't want to rely on it.

There's got to be a better way to check to see if the ssh succeeded in
connecting and launching an interactive shell.

--
The randomly chosen signature file that would have appeared here is more than 4
lines long. As such, it violates one or more Usenet RFCs. In order to remain
in compliance with said RFCs, the actual sig can be found at the following URL:
http://user.xmission.com/~gazelle/Sigs/WeekendAwayFromHome

Re: 'ssh' exit status (Was: Struggling with this "until" loop)

<slrns6ou2k.1mtq.naddy@lorvorc.mips.inka.de>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=3608&group=comp.unix.shell#3608

 copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!news.swapon.de!weretis.net!feeder8.news.weretis.net!news.szaf.org!inka.de!mips.inka.de!.POSTED.localhost!not-for-mail
From: nad...@mips.inka.de (Christian Weisgerber)
Newsgroups: comp.unix.shell
Subject: Re: 'ssh' exit status (Was: Struggling with this "until" loop)
Date: Tue, 6 Apr 2021 15:03:48 -0000 (UTC)
Message-ID: <slrns6ou2k.1mtq.naddy@lorvorc.mips.inka.de>
References: <s497dh$6t2$1@dont-email.me>
<slrns6hi4a.2b5t.naddy@lorvorc.mips.inka.de>
<s4am6o$maak$1@news.xmission.com>
<slrns6jhud.2uih.naddy@lorvorc.mips.inka.de>
<s4hnil$pj97$1@news.xmission.com>
Injection-Date: Tue, 6 Apr 2021 15:03:48 -0000 (UTC)
Injection-Info: lorvorc.mips.inka.de; posting-host="localhost:::1";
logging-data="56251"; mail-complaints-to="usenet@mips.inka.de"
User-Agent: slrn/1.0.3 (FreeBSD)
 by: Christian Weisgerber - Tue, 6 Apr 2021 15:03 UTC

On 2021-04-06, Kenny McCormack <gazelle@shell.xmission.com> wrote:

> I believe (now) that you are correct - in that exiting the interactive
> shell via "exit 0" will cause the ssh process to set its exit status to 0.
> (Presumably, "exit N" should also set it to N, for any value of N)
>
> But nobody does that. People exit interactive shells either via ^D (most
> common) or via a command like exit, bye, quit, stop or whatever their shell
> supports. I believe that in all such cases, you will get a non-zero exit
> status passed back to ssh.

Plain "exit" will return the exit status of the previous command.
I would expect ^D to be equivalent.

> I admit I haven't thoroughly tested this last, but it syncs with my
> experience.

Well, let's try it:

$ true
$ ^D
=> 0

$ false
$ ^D
=> 1

$ (exit 42)
$ ^D
=> 42

Same results for bash and FreeBSD's sh as login shell.

--
Christian "naddy" Weisgerber naddy@mips.inka.de

Re: 'ssh' exit status (Was: Struggling with this "until" loop)

<pasangan@bongo-ra.co>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=3609&group=comp.unix.shell#3609

 copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!news.uzoreto.com!fdc2.netnews.com!news-out.netnews.com!news.alt.net!fdc3.netnews.com!peer03.ams1!peer.ams1.xlned.com!news.xlned.com!peer03.iad!feed-me.highwinds-media.com!peer01.ams4!peer.am4.highwinds-media.com!news.highwinds-media.com!fx23.ams4.POSTED!not-for-mail
From: spi...@gmail.com (Spiros Bousbouras)
Newsgroups: comp.unix.shell
Subject: Re: 'ssh' exit status (Was: Struggling with this "until" loop)
Message-ID: <pasangan@bongo-ra.co>
References: <s497dh$6t2$1@dont-email.me> <slrns6hi4a.2b5t.naddy@lorvorc.mips.inka.de> <s4am6o$maak$1@news.xmission.com>
<slrns6jhud.2uih.naddy@lorvorc.mips.inka.de> <s4hnil$pj97$1@news.xmission.com>
In-Reply-To: <s4hnil$pj97$1@news.xmission.com>
X-Organisation: Weyland-Yutani
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
Lines: 17
X-Complaints-To: http://netreport.virginmedia.com
NNTP-Posting-Date: Tue, 06 Apr 2021 15:37:24 UTC
Organization: virginmedia.com
Date: Tue, 06 Apr 2021 15:37:24 GMT
X-Received-Bytes: 1785
 by: Spiros Bousbouras - Tue, 6 Apr 2021 15:37 UTC

On Tue, 6 Apr 2021 13:26:45 -0000 (UTC)
gazelle@shell.xmission.com (Kenny McCormack) wrote:
> In article <slrns6jhud.2uih.naddy@lorvorc.mips.inka.de>,
> Christian Weisgerber <naddy@mips.inka.de> wrote:
> >You can check for an exit status of 255 to indicate that the
> >connection failed, albeit with the remote chance that it actually
> >succeeded and the remote command returned 255.
>
> Yes, we agree on this. Checking for specifically 255 seems awfully shaky
> and brittle to me. I wouldn't want to rely on it.
>
> There's got to be a better way to check to see if the ssh succeeded in
> connecting and launching an interactive shell.

For an *interactive* shell wouldn't just checking with one's eyes (or
ears if one uses a screen reader) be enough ? I mean the point of an
interactive shell is to interact with it.

Re: 'ssh' exit status (Was: Struggling with this "until" loop)

<s4i3d2$pn44$1@news.xmission.com>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=3610&group=comp.unix.shell#3610

 copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!xmission!nnrp.xmission!.POSTED.shell.xmission.com!not-for-mail
From: gaze...@shell.xmission.com (Kenny McCormack)
Newsgroups: comp.unix.shell
Subject: Re: 'ssh' exit status (Was: Struggling with this "until" loop)
Date: Tue, 6 Apr 2021 16:48:34 -0000 (UTC)
Organization: The official candy of the new Millennium
Message-ID: <s4i3d2$pn44$1@news.xmission.com>
References: <s497dh$6t2$1@dont-email.me> <slrns6jhud.2uih.naddy@lorvorc.mips.inka.de> <s4hnil$pj97$1@news.xmission.com> <pasangan@bongo-ra.co>
Injection-Date: Tue, 6 Apr 2021 16:48:34 -0000 (UTC)
Injection-Info: news.xmission.com; posting-host="shell.xmission.com:166.70.8.4";
logging-data="842884"; mail-complaints-to="abuse@xmission.com"
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
Originator: gazelle@shell.xmission.com (Kenny McCormack)
 by: Kenny McCormack - Tue, 6 Apr 2021 16:48 UTC

In article <pasangan@bongo-ra.co>, Spiros Bousbouras <spibou@gmail.com> wrote:
>On Tue, 6 Apr 2021 13:26:45 -0000 (UTC)
>gazelle@shell.xmission.com (Kenny McCormack) wrote:
>> In article <slrns6jhud.2uih.naddy@lorvorc.mips.inka.de>,
>> Christian Weisgerber <naddy@mips.inka.de> wrote:
>> >You can check for an exit status of 255 to indicate that the
>> >connection failed, albeit with the remote chance that it actually
>> >succeeded and the remote command returned 255.
>>
>> Yes, we agree on this. Checking for specifically 255 seems awfully shaky
>> and brittle to me. I wouldn't want to rely on it.
>>
>> There's got to be a better way to check to see if the ssh succeeded in
>> connecting and launching an interactive shell.
>
>For an *interactive* shell wouldn't just checking with one's eyes (or
>ears if one uses a screen reader) be enough ? I mean the point of an
>interactive shell is to interact with it.

Because that was what OP was doing in the original post (check the parent
thread to this one). He was basically launching a virtual machine, then
looping until he is able to (interactively) ssh into it. The code was
something like:

until ssh localhost -p 5555
do sleep 2
done

And my point in this sub-thread was that was not likely to be a very robust
solution to the problem. At a minimum, you'd have to check for the exit
status of the ssh being exactly 255, and even that seems pretty brittle.

Thinking about it, maybe what OP really *should* be doing is this:

until ssh localhost -p 5555 :
do sleep 2
done
ssh localhost -p 5555

--
(Cruz certainly has an odd face) ... it looks like someone sewed pieces of a
waterlogged Reagan mask together at gunpoint ...

http://www.rollingstone.com/politics/news/how-america-made-donald-trump-unstoppable-20160224

1
server_pubkey.txt

rocksolid light 0.9.7
clearnet tor