zsh-workers
 help / color / mirror / code / Atom feed
* zpty woes
@ 2008-05-15 12:02 Jaime Vargas
  2008-05-15 13:10 ` Stephane Chazelas
                   ` (2 more replies)
  0 siblings, 3 replies; 15+ messages in thread
From: Jaime Vargas @ 2008-05-15 12:02 UTC (permalink / raw)
  To: zsh-workers

In the script attached, when trying to follwing line always fails.

     zpty -r scppty line "*:" || echo "no password asked" && exit

It appears that the "password" string from the ssh session is discarded
by the psuedo terminal.

Is there a way to fix this? or Am I missing something?

Thanks,

Jaime

#SCRIPT FOLLOWS

#!/bin/zsh -x

zmodload zsh/zpty

zpty scppty scp hello.world luser@host:~/
zpty -t scppty || echo "something went wrong" && exit
zpty -r scppty line "*:" || echo "no password asked" && exit
zpty -w scppty "TopSecret"
while zpty -r scppty line;
     do
         result+="$line"$'\n'
     done
zpty -d scppty
print $result


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: zpty woes
  2008-05-15 12:02 zpty woes Jaime Vargas
@ 2008-05-15 13:10 ` Stephane Chazelas
  2008-05-15 13:18 ` Peter Stephenson
  2008-05-15 13:26 ` zpty woes Stephane Chazelas
  2 siblings, 0 replies; 15+ messages in thread
From: Stephane Chazelas @ 2008-05-15 13:10 UTC (permalink / raw)
  To: Jaime Vargas; +Cc: zsh-workers

On Thu, May 15, 2008 at 08:02:04AM -0400, Jaime Vargas wrote:
[...]
> zpty -t scppty || echo "something went wrong" && exit
[...]

That would always exit unless echo returned with a non-zero exit
status

A || B && C

must be read

(A || B) && C

So C is executed if either A or B succeeds.

You should write it:

A || {
  B
  C
}

or

if ! A; then
  B
  C
fi

or

die() {print -r -- $1 >&2; exit 1;}
A || die "something wrong"

-- 
Stéphane


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: zpty woes
  2008-05-15 12:02 zpty woes Jaime Vargas
  2008-05-15 13:10 ` Stephane Chazelas
@ 2008-05-15 13:18 ` Peter Stephenson
  2008-05-15 14:50   ` Stephane Chazelas
  2008-05-15 13:26 ` zpty woes Stephane Chazelas
  2 siblings, 1 reply; 15+ messages in thread
From: Peter Stephenson @ 2008-05-15 13:18 UTC (permalink / raw)
  To: zsh-workers

Jaime Vargas wrote:
> In the script attached, when trying to follwing line always fails.
> 
>      zpty -r scppty line "*:" || echo "no password asked" && exit
> 
> It appears that the "password" string from the ssh session is discarded
> by the psuedo terminal.
> 
> Is there a way to fix this? or Am I missing something?

Yes and yes, but it's annoying and I spent a few minutes rediscovering
the arcana.  zpty attempts to read in whole lines, while the password
prompt isn't a whole line.  I came across this myself and looked at the
code a while ago and it seemed unnecessarily obscure.  However, I worked
around it and don't have time to make it work sensibly in all the
possible cases, so I'm not touching it.  (If anyone's interested it's in
ptyread() in Src/Modules/zpty.c and could definitely do with someone
taking it over; it's pretty much self-contained.)

To work around this, you need to use non-blocking mode, i.e. start with
"zpty -b scppty ...".  I think (although the code is obscure) that with
your "*:" pattern this will work, i.e. although it doesn't block it will
carry on reading until it gets the password input.  If not, you would
need to delay (the zsh/zselect module allows you to do this in 100ths of
a second) and retry.  (Waiting for a pattern with noblocking is
effectively a busy wait so even this isn't ideal---you can add the -t
option to the -r command line to test first, but then it *won't* wait
if there's no input and you do have to delay in your script.)

Anyway, it now seems to work for me (though if there were an occasional
race I might have missed it).

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: zpty woes
  2008-05-15 12:02 zpty woes Jaime Vargas
  2008-05-15 13:10 ` Stephane Chazelas
  2008-05-15 13:18 ` Peter Stephenson
@ 2008-05-15 13:26 ` Stephane Chazelas
  2008-05-15 15:44   ` Peter Stephenson
  2 siblings, 1 reply; 15+ messages in thread
From: Stephane Chazelas @ 2008-05-15 13:26 UTC (permalink / raw)
  To: Jaime Vargas; +Cc: zsh-workers

On Thu, May 15, 2008 at 08:02:04AM -0400, Jaime Vargas wrote:
[...]
> #!/bin/zsh -x
>
> zmodload zsh/zpty
>
> zpty scppty scp hello.world luser@host:~/

Also, if you use "set -x", the subshell run within the pty will
have set -x as well, so:

> zpty -t scppty || echo "something went wrong" && exit
> zpty -r scppty line "*:" || echo "no password asked" && exit

will read the "+myscript:" in
"+myscript:1> scp hello...."

(by, the way, shouldn't it be something like "+(zpty):1> scp
hello"?)

-- 
Stéphane


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: zpty woes
  2008-05-15 13:18 ` Peter Stephenson
@ 2008-05-15 14:50   ` Stephane Chazelas
  2008-05-15 15:26     ` Peter Stephenson
                       ` (2 more replies)
  0 siblings, 3 replies; 15+ messages in thread
From: Stephane Chazelas @ 2008-05-15 14:50 UTC (permalink / raw)
  To: Peter Stephenson; +Cc: zsh-workers

On Thu, May 15, 2008 at 02:18:57PM +0100, Peter Stephenson wrote:
> Jaime Vargas wrote:
> > In the script attached, when trying to follwing line always fails.
> > 
> >      zpty -r scppty line "*:" || echo "no password asked" && exit
> > 
> > It appears that the "password" string from the ssh session is discarded
> > by the psuedo terminal.
> > 
> > Is there a way to fix this? or Am I missing something?
> 
> Yes and yes, but it's annoying and I spent a few minutes rediscovering
> the arcana.  zpty attempts to read in whole lines, while the password
> prompt isn't a whole line.  I came across this myself and looked at the
> code a while ago and it seemed unnecessarily obscure.  However, I worked
> around it and don't have time to make it work sensibly in all the
> possible cases, so I'm not touching it.  (If anyone's interested it's in
> ptyread() in Src/Modules/zpty.c and could definitely do with someone
> taking it over; it's pretty much self-contained.)
> 
> To work around this, you need to use non-blocking mode, i.e. start with
> "zpty -b scppty ...".  I think (although the code is obscure) that with
> your "*:" pattern this will work, i.e. although it doesn't block it will
> carry on reading until it gets the password input.  If not, you would
> need to delay (the zsh/zselect module allows you to do this in 100ths of
> a second) and retry.  (Waiting for a pattern with noblocking is
> effectively a busy wait so even this isn't ideal---you can add the -t
> option to the -r command line to test first, but then it *won't* wait
> if there's no input and you do have to delay in your script.)
[...]

It works for me without "-b", why would you say "-b" is
necessary?

The problem I've found is that zpty -r doesn't return when the
command in the pty has terminated. A strace -p shows:

read(12, 0xb7ba04c8, 1)                 = -1 EIO (Input/output error)
read(12, 0xb7ba04c8, 1)                 = -1 EIO (Input/output error)
read(12, 0xb7ba04c8, 1)                 = -1 EIO (Input/output error)
read(12, 0xb7ba04c8, 1)                 = -1 EIO (Input/output error)
read(12, 0xb7ba04c8, 1)                 = -1 EIO (Input/output error)
read(12, 0xb7ba04c8, 1)                 = -1 EIO (Input/output error)
read(12, 0xb7ba04c8, 1)                 = -1 EIO (Input/output error)
read(12, 0xb7ba04c8, 1)                 = -1 EIO (Input/output error)
read(12, 0xb7ba04c8, 1)                 = -1 EIO (Input/output error)
[...]

And the other problem I mentionned in another email about the
PS4 output not being correct.

-- 
Stéphane


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: zpty woes
  2008-05-15 14:50   ` Stephane Chazelas
@ 2008-05-15 15:26     ` Peter Stephenson
  2008-05-15 16:25     ` Jaime Vargas
       [not found]     ` <99E8D43E-EC34-48DB-A5AD-BFA197A3AAA3@mac.com>
  2 siblings, 0 replies; 15+ messages in thread
From: Peter Stephenson @ 2008-05-15 15:26 UTC (permalink / raw)
  To: zsh-workers

Stephane Chazelas wrote:
> On Thu, May 15, 2008 at 02:18:57PM +0100, Peter Stephenson wrote:
> > Jaime Vargas wrote:
> > > In the script attached, when trying to follwing line always fails.
> > > 
> > >      zpty -r scppty line "*:" || echo "no password asked" && exit
> > > 
> > > It appears that the "password" string from the ssh session is discarded
> > > by the psuedo terminal.
> > > 
> > > Is there a way to fix this? or Am I missing something?
> > 
> > To work around this, you need to use non-blocking mode, i.e. start with
> > "zpty -b scppty ...".
> 
> It works for me without "-b", why would you say "-b" is
> necessary?

OK, it works without -b if and only if you have the pattern.  That
stops it needing the newline.  I said the code was tortuous.

> The problem I've found is that zpty -r doesn't return when the
> command in the pty has terminated. A strace -p shows:
> 
> read(12, 0xb7ba04c8, 1)                 = -1 EIO (Input/output error)

Highly plausible.  That's likely to be the loop where it waits for the
pattern regardless.

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: zpty woes
  2008-05-15 13:26 ` zpty woes Stephane Chazelas
@ 2008-05-15 15:44   ` Peter Stephenson
  0 siblings, 0 replies; 15+ messages in thread
From: Peter Stephenson @ 2008-05-15 15:44 UTC (permalink / raw)
  To: zsh-workers

On Thu, 15 May 2008 14:26:09 +0100
Stephane Chazelas <Stephane_Chazelas@yahoo.fr> wrote:
> "+myscript:1> scp hello...."
> 
> (by, the way, shouldn't it be something like "+(zpty):1> scp
> hello"?)

zpty currently exec's code directly, it doesn't go through eval.  Borrowing
the code from eval should fix this.  There may be other
parse_string()/execode() pairs that need this.  This is crying out to be
object-orientated...

Index: Src/builtin.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/builtin.c,v
retrieving revision 1.194
diff -u -r1.194 builtin.c
--- Src/builtin.c	14 May 2008 10:48:28 -0000	1.194
+++ Src/builtin.c	15 May 2008 15:42:33 -0000
@@ -4681,7 +4681,7 @@
 /* eval: simple evaluation */
 
 /**/
-int ineval;
+mod_export int ineval;
 
 /**/
 int
Index: Src/utils.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/utils.c,v
retrieving revision 1.190
diff -u -r1.190 utils.c
--- Src/utils.c	14 May 2008 10:48:28 -0000	1.190
+++ Src/utils.c	15 May 2008 15:42:37 -0000
@@ -33,7 +33,7 @@
 /* name of script being sourced */
 
 /**/
-char *scriptname;
+mod_export char *scriptname;
 
 #ifdef MULTIBYTE_SUPPORT
 struct widechar_array {
Index: Src/Modules/zpty.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/Modules/zpty.c,v
retrieving revision 1.37
diff -u -r1.37 zpty.c
--- Src/Modules/zpty.c	25 Jan 2008 16:48:24 -0000	1.37
+++ Src/Modules/zpty.c	15 May 2008 15:42:37 -0000
@@ -290,22 +290,34 @@
 newptycmd(char *nam, char *pname, char **args, int echo, int nblock)
 {
     Ptycmd p;
-    int master, slave, pid;
+    int master, slave, pid, oineval = ineval;
+    char *oscriptname = scriptname;
     Eprog prog;
 
+    /* code borrowed from bin_eval() */
+    ineval = !isset(EVALLINENO);
+    if (!ineval)
+	scriptname = "(zpty)";
+
     prog = parse_string(zjoin(args, ' ', 1));
     if (!prog) {
 	errflag = 0;
+	scriptname = oscriptname;
+	ineval = oineval;
 	return 1;
     }
 
     if (get_pty(1, &master)) {
 	zwarnnam(nam, "can't open pseudo terminal: %e", errno);
+	scriptname = oscriptname;
+	ineval = oineval;
 	return 1;
     }
     if ((pid = fork()) == -1) {
 	zwarnnam(nam, "can't create pty command %s: %e", pname, errno);
 	close(master);
+	scriptname = oscriptname;
+	ineval = oineval;
 	return 1;
     } else if (!pid) {
 	/* This code copied from the clone module, except for getting *
@@ -406,6 +418,8 @@
     if (nblock)
 	ptynonblock(master);
 
+    scriptname = oscriptname;
+    ineval = oineval;
     return 0;
 }
 

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: zpty woes
  2008-05-15 14:50   ` Stephane Chazelas
  2008-05-15 15:26     ` Peter Stephenson
@ 2008-05-15 16:25     ` Jaime Vargas
  2008-05-15 16:36       ` * " Peter Stephenson
       [not found]     ` <99E8D43E-EC34-48DB-A5AD-BFA197A3AAA3@mac.com>
  2 siblings, 1 reply; 15+ messages in thread
From: Jaime Vargas @ 2008-05-15 16:25 UTC (permalink / raw)
  To: zsh-workers

Still doesn't work for me. Below is the modified script and the debug  
output.

#!/opt/csw/bin/zsh

set -x

zmodload zsh/zpty

die() {print -r -- $1 >&2; exit 1;}

zpty scppty scp hello.world jvargas@aoma6000-4-9.aoma.rhbss.com:~/
zpty -t scppty || die "fuck"
zpty -r scppty line "*:" || die "no password asked"
zpty -w scppty "3lp&tbw"
while zpty -r scppty line;
     do
         result+="$line"$'\n'
     done
zpty -d scppty
print $result

I changed my credentials for security. Basically it now hangs waitng  
for password and doesnt' do anything.  -- Jaime

nerd% ./zpty-test.zsh
+./zpty-test.zsh:5> zmodload zsh/zpty
+./zpty-test.zsh:10> zpty scppty scp hello.world 'luser@host:~/'
+./zpty-test.zsh:11> zpty -t scppty
+./zpty-test.zsh:12> zpty -r scppty line '*:'
+./zpty-test.zsh:13> zpty -w scppty 'TopSecret'
+./zpty-test.zsh:14> zpty -r scppty line
+./zpty-test.zsh:16> result+='1> scp hello.world  
'\''jvargas@aoma6000-4-9.aoma.rhbss.com:~/'\''

'
+./zpty-test.zsh:14> zpty -r scppty line
+./zpty-test.zsh:16> result+='THIS IS A PROPRIETARY RANDOM HOUSE  
SYSTEM, RESTRICTED TO AUTHORIZED

'
+./zpty-test.zsh:14> zpty -r scppty line
+./zpty-test.zsh:16> result+='PERSONNEL AND FOR OFFICIAL, AUTHORIZED  
RANDOM HOUSE BUSINESS ONLY.

'
+./zpty-test.zsh:14> zpty -r scppty line
+./zpty-test.zsh:16> result+='ANYONE USING THIS SYSTEM, NETWORK OR  
DATA IS SUBJECT TO MONITORING

'
+./zpty-test.zsh:14> zpty -r scppty line
+./zpty-test.zsh:16> result+='AT ANY TIME.  ANYONE USING THIS SYSTEM  
THEREBY EXPRESSLY CONSENTS

'
+./zpty-test.zsh:14> zpty -r scppty line
+./zpty-test.zsh:16> result+='TO SUCH MONITORING AND IS FURTHER  
ADVISED THAT ANY EVIDENCE OF

'
+./zpty-test.zsh:14> zpty -r scppty line
+./zpty-test.zsh:16> result+='CRIMINAL AND/OR OTHERWISE IMPROPER OR  
UNAUTHORIZED ACTIVITY MAY

'
+./zpty-test.zsh:14> zpty -r scppty line
+./zpty-test.zsh:16> result+='BE PROVIDED TO LAW ENFORCEMENT  
OFFICIALS FOR PROSECUTION AND/OR

'
+./zpty-test.zsh:14> zpty -r scppty line
+./zpty-test.zsh:16> result+='USED BY RANDOM HOUSE AS IT SEES FIT.

'
+./zpty-test.zsh:14> zpty -r scppty line
+./zpty-test.zsh:16> result+='

'
+./zpty-test.zsh:14> zpty -r scppty line


..... it waits forever here.

On May 15, 2008, at 10:50 AM, Stephane Chazelas wrote:


> On Thu, May 15, 2008 at 02:18:57PM +0100, Peter Stephenson wrote:
>
>> Jaime Vargas wrote:
>>
>>> In the script attached, when trying to follwing line always fails.
>>>
>>>      zpty -r scppty line "*:" || echo "no password asked" && exit
>>>
>>> It appears that the "password" string from the ssh session is  
>>> discarded
>>> by the psuedo terminal.
>>>
>>> Is there a way to fix this? or Am I missing something?
>>>
>>
>> Yes and yes, but it's annoying and I spent a few minutes  
>> rediscovering
>> the arcana.  zpty attempts to read in whole lines, while the password
>> prompt isn't a whole line.  I came across this myself and looked  
>> at the
>> code a while ago and it seemed unnecessarily obscure.  However, I  
>> worked
>> around it and don't have time to make it work sensibly in all the
>> possible cases, so I'm not touching it.  (If anyone's interested  
>> it's in
>> ptyread() in Src/Modules/zpty.c and could definitely do with someone
>> taking it over; it's pretty much self-contained.)
>>
>> To work around this, you need to use non-blocking mode, i.e. start  
>> with
>> "zpty -b scppty ...".  I think (although the code is obscure) that  
>> with
>> your "*:" pattern this will work, i.e. although it doesn't block  
>> it will
>> carry on reading until it gets the password input.  If not, you would
>> need to delay (the zsh/zselect module allows you to do this in  
>> 100ths of
>> a second) and retry.  (Waiting for a pattern with noblocking is
>> effectively a busy wait so even this isn't ideal---you can add the -t
>> option to the -r command line to test first, but then it *won't* wait
>> if there's no input and you do have to delay in your script.)
>>
> [...]
>
> It works for me without "-b", why would you say "-b" is
> necessary?
>
> The problem I've found is that zpty -r doesn't return when the
> command in the pty has terminated. A strace -p shows:
>
> read(12, 0xb7ba04c8, 1)                 = -1 EIO (Input/output error)
> read(12, 0xb7ba04c8, 1)                 = -1 EIO (Input/output error)
> read(12, 0xb7ba04c8, 1)                 = -1 EIO (Input/output error)
> read(12, 0xb7ba04c8, 1)                 = -1 EIO (Input/output error)
> read(12, 0xb7ba04c8, 1)                 = -1 EIO (Input/output error)
> read(12, 0xb7ba04c8, 1)                 = -1 EIO (Input/output error)
> read(12, 0xb7ba04c8, 1)                 = -1 EIO (Input/output error)
> read(12, 0xb7ba04c8, 1)                 = -1 EIO (Input/output error)
> read(12, 0xb7ba04c8, 1)                 = -1 EIO (Input/output error)
> [...]
>
> And the other problem I mentionned in another email about the
> PS4 output not being correct.
>
> -- 
> Stéphane
On May 15, 2008, at 10:50 AM, Stephane Chazelas wrote:

> On Thu, May 15, 2008 at 02:18:57PM +0100, Peter Stephenson wrote:
>> Jaime Vargas wrote:
>>> In the script attached, when trying to follwing line always fails.
>>>
>>>      zpty -r scppty line "*:" || echo "no password asked" && exit
>>>
>>> It appears that the "password" string from the ssh session is  
>>> discarded
>>> by the psuedo terminal.
>>>
>>> Is there a way to fix this? or Am I missing something?
>>
>> Yes and yes, but it's annoying and I spent a few minutes  
>> rediscovering
>> the arcana.  zpty attempts to read in whole lines, while the password
>> prompt isn't a whole line.  I came across this myself and looked  
>> at the
>> code a while ago and it seemed unnecessarily obscure.  However, I  
>> worked
>> around it and don't have time to make it work sensibly in all the
>> possible cases, so I'm not touching it.  (If anyone's interested  
>> it's in
>> ptyread() in Src/Modules/zpty.c and could definitely do with someone
>> taking it over; it's pretty much self-contained.)
>>
>> To work around this, you need to use non-blocking mode, i.e. start  
>> with
>> "zpty -b scppty ...".  I think (although the code is obscure) that  
>> with
>> your "*:" pattern this will work, i.e. although it doesn't block  
>> it will
>> carry on reading until it gets the password input.  If not, you would
>> need to delay (the zsh/zselect module allows you to do this in  
>> 100ths of
>> a second) and retry.  (Waiting for a pattern with noblocking is
>> effectively a busy wait so even this isn't ideal---you can add the -t
>> option to the -r command line to test first, but then it *won't* wait
>> if there's no input and you do have to delay in your script.)
> [...]
>
> It works for me without "-b", why would you say "-b" is
> necessary?
>
> The problem I've found is that zpty -r doesn't return when the
> command in the pty has terminated. A strace -p shows:
>
> read(12, 0xb7ba04c8, 1)                 = -1 EIO (Input/output error)
> read(12, 0xb7ba04c8, 1)                 = -1 EIO (Input/output error)
> read(12, 0xb7ba04c8, 1)                 = -1 EIO (Input/output error)
> read(12, 0xb7ba04c8, 1)                 = -1 EIO (Input/output error)
> read(12, 0xb7ba04c8, 1)                 = -1 EIO (Input/output error)
> read(12, 0xb7ba04c8, 1)                 = -1 EIO (Input/output error)
> read(12, 0xb7ba04c8, 1)                 = -1 EIO (Input/output error)
> read(12, 0xb7ba04c8, 1)                 = -1 EIO (Input/output error)
> read(12, 0xb7ba04c8, 1)                 = -1 EIO (Input/output error)
> [...]
>
> And the other problem I mentionned in another email about the
> PS4 output not being correct.
>
> -- 
> Stéphane


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: * Re: zpty woes
  2008-05-15 16:25     ` Jaime Vargas
@ 2008-05-15 16:36       ` Peter Stephenson
  0 siblings, 0 replies; 15+ messages in thread
From: Peter Stephenson @ 2008-05-15 16:36 UTC (permalink / raw)
  To: zsh-workers

Jaime Vargas wrote:
> Still doesn't work for me. Below is the modified script and the debug
>...
> I changed my credentials for security. Basically it now hangs waitng
> for password and doesnt' do anything.  -- Jaime

I don't think it does...

> +./zpty-test.zsh:16> result+='1> scp hello.world
> '\''jvargas@aoma6000-4-9.aoma.rhbss.com:~/'\''
> 
> '
> +./zpty-test.zsh:14> zpty -r scppty line

This is where it's asking for the password, and it hasn't hung.

> +./zpty-test.zsh:14> zpty -r scppty line
> 
> 
> ..... it waits forever here.

At *this* point it will wait if it doesn't get a complete line because
of my previous point.  Try adding "*" (including quotes) to the end of
the `zpty -r' command.

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: zpty woes
       [not found]     ` <99E8D43E-EC34-48DB-A5AD-BFA197A3AAA3@mac.com>
@ 2008-05-15 17:03       ` Stephane Chazelas
       [not found]       ` <480CEB80051F27AE@mac.com>
  1 sibling, 0 replies; 15+ messages in thread
From: Stephane Chazelas @ 2008-05-15 17:03 UTC (permalink / raw)
  To: Jaime Vargas; +Cc: Zsh hackers list

On Thu, May 15, 2008 at 12:00:25PM -0400, Jaime Vargas wrote:
> Still doesn't work for me. Below is the modified script and the debug 
> output.
>
> #!/opt/csw/bin/zsh
>
> set -x
>
> zmodload zsh/zpty
>
> die() {print -r -- $1 >&2; exit 1;}
>
> zpty scppty scp hello.world jvargas@aoma6000-4-9.aoma.rhbss.com:~/
> zpty -t scppty || die "fuck"
> zpty -r scppty line "*:" || die "no password asked"
> zpty -w scppty "3lp&tbw"
> while zpty -r scppty line;
>     do
>         result+="$line"$'\n'
>     done
> zpty -d scppty
> print $result
>
> I changed my credentials for security. Basically it now hangs waitng for 
> password and doesnt' do anything.  -- Jaime
>
> nerd% ./zpty-test.zsh
> +./zpty-test.zsh:5> zmodload zsh/zpty
> +./zpty-test.zsh:10> zpty scppty scp hello.world 'luser@host:~/'
> +./zpty-test.zsh:11> zpty -t scppty
> +./zpty-test.zsh:12> zpty -r scppty line '*:'

Had you printed $line, you'd have seen something like
"+myscript:", not "Passwd: ".


[...]
> +./zpty-test.zsh:14> zpty -r scppty line
[...]

As Peter said, if you don't provide with a pattern to look for,
zpty will look for NL characters.

That last zpty is probably still waiting because so far, it has
only received "Password: " and is waiting for a NL character
that will never come.

So, in your code above, you should wait for something more
specific than just ":":

zpty -r scppty line "assword: " || die "no password asked"

for instance.

-- 
Stéphane


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: zpty woes
       [not found]       ` <480CEB80051F27AE@mac.com>
@ 2008-05-15 17:38         ` Jaime Vargas
  2008-05-15 17:53           ` Jaime Vargas
  0 siblings, 1 reply; 15+ messages in thread
From: Jaime Vargas @ 2008-05-15 17:38 UTC (permalink / raw)
  To: Zsh hackers list

Still no luck. Am I missing something stupid?

nerd% ./zpty-test.zsh
+./zpty-test.zsh:4> zmodload zsh/zpty
+./zpty-test.zsh:8> zpty scppty scp hello.world 'user@host:~/'
+./zpty-test.zsh:9> zpty -t scppty
+./zpty-test.zsh:10> zpty -r scppty line assword:

#!/opt/csw/bin/zsh
set -x
zmodload zsh/zpty
die() {print -r -- $1 >&2; exit 1;}
zpty scppty scp hello.world user@host:~/
zpty -t scppty || die "fuck"
zpty -r scppty line "assword:" || die "no password asked"
zpty -w scppty "TopSecret"
while zpty -r scppty line;
     do
         result+="$line"$'\n'
     done
zpty -d scppty
print $result

On May 15, 2008, at 1:03 PM, Stephane Chazelas wrote:

> On Thu, May 15, 2008 at 12:00:25PM -0400, Jaime Vargas wrote:
>> Still doesn't work for me. Below is the modified script and the debug
>> output.
>>
>> #!/opt/csw/bin/zsh
>>
>> set -x
>>
>> zmodload zsh/zpty
>>
>> die() {print -r -- $1 >&2; exit 1;}
>>
>> zpty scppty scp hello.world jvargas@aoma6000-4-9.aoma.rhbss.com:~/
>> zpty -t scppty || die "fuck"
>> zpty -r scppty line "*:" || die "no password asked"
>> zpty -w scppty "3lp&tbw"
>> while zpty -r scppty line;
>>     do
>>         result+="$line"$'\n'
>>     done
>> zpty -d scppty
>> print $result
>>
>> I changed my credentials for security. Basically it now hangs  
>> waitng for
>> password and doesnt' do anything.  -- Jaime
>>
>> nerd% ./zpty-test.zsh
>> +./zpty-test.zsh:5> zmodload zsh/zpty
>> +./zpty-test.zsh:10> zpty scppty scp hello.world 'luser@host:~/'
>> +./zpty-test.zsh:11> zpty -t scppty
>> +./zpty-test.zsh:12> zpty -r scppty line '*:'
>
> Had you printed $line, you'd have seen something like
> "+myscript:", not "Passwd: ".
>
>
> [...]
>> +./zpty-test.zsh:14> zpty -r scppty line
> [...]
>
> As Peter said, if you don't provide with a pattern to look for,
> zpty will look for NL characters.
>
> That last zpty is probably still waiting because so far, it has
> only received "Password: " and is waiting for a NL character
> that will never come.
>
> So, in your code above, you should wait for something more
> specific than just ":":
>
> zpty -r scppty line "assword: " || die "no password asked"
>
> for instance.
>
> -- 
> Stéphane


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: zpty woes
  2008-05-15 17:38         ` Jaime Vargas
@ 2008-05-15 17:53           ` Jaime Vargas
  2008-05-20  3:25             ` Jaime Vargas
  0 siblings, 1 reply; 15+ messages in thread
From: Jaime Vargas @ 2008-05-15 17:53 UTC (permalink / raw)
  To: Zsh hackers list

I got it working. By changing the pattern to '*assword:'. Thanks for  
the help. -- Jaime


On May 15, 2008, at 1:38 PM, Jaime Vargas wrote:

> Still no luck. Am I missing something stupid?
>
> nerd% ./zpty-test.zsh
> +./zpty-test.zsh:4> zmodload zsh/zpty
> +./zpty-test.zsh:8> zpty scppty scp hello.world 'user@host:~/'
> +./zpty-test.zsh:9> zpty -t scppty
> +./zpty-test.zsh:10> zpty -r scppty line assword:
>
> #!/opt/csw/bin/zsh
> set -x
> zmodload zsh/zpty
> die() {print -r -- $1 >&2; exit 1;}
> zpty scppty scp hello.world user@host:~/
> zpty -t scppty || die "fuck"
> zpty -r scppty line "assword:" || die "no password asked"
> zpty -w scppty "TopSecret"
> while zpty -r scppty line;
>     do
>         result+="$line"$'\n'
>     done
> zpty -d scppty
> print $result
>
> On May 15, 2008, at 1:03 PM, Stephane Chazelas wrote:
>
>> On Thu, May 15, 2008 at 12:00:25PM -0400, Jaime Vargas wrote:
>>> Still doesn't work for me. Below is the modified script and the  
>>> debug
>>> output.
>>>
>>> #!/opt/csw/bin/zsh
>>>
>>> set -x
>>>
>>> zmodload zsh/zpty
>>>
>>> die() {print -r -- $1 >&2; exit 1;}
>>>
>>> zpty scppty scp hello.world jvargas@aoma6000-4-9.aoma.rhbss.com:~/
>>> zpty -t scppty || die "fuck"
>>> zpty -r scppty line "*:" || die "no password asked"
>>> zpty -w scppty "3lp&tbw"
>>> while zpty -r scppty line;
>>>     do
>>>         result+="$line"$'\n'
>>>     done
>>> zpty -d scppty
>>> print $result
>>>
>>> I changed my credentials for security. Basically it now hangs  
>>> waitng for
>>> password and doesnt' do anything.  -- Jaime
>>>
>>> nerd% ./zpty-test.zsh
>>> +./zpty-test.zsh:5> zmodload zsh/zpty
>>> +./zpty-test.zsh:10> zpty scppty scp hello.world 'luser@host:~/'
>>> +./zpty-test.zsh:11> zpty -t scppty
>>> +./zpty-test.zsh:12> zpty -r scppty line '*:'
>>
>> Had you printed $line, you'd have seen something like
>> "+myscript:", not "Passwd: ".
>>
>>
>> [...]
>>> +./zpty-test.zsh:14> zpty -r scppty line
>> [...]
>>
>> As Peter said, if you don't provide with a pattern to look for,
>> zpty will look for NL characters.
>>
>> That last zpty is probably still waiting because so far, it has
>> only received "Password: " and is waiting for a NL character
>> that will never come.
>>
>> So, in your code above, you should wait for something more
>> specific than just ":":
>>
>> zpty -r scppty line "assword: " || die "no password asked"
>>
>> for instance.
>>
>> -- 
>> Stéphane
>


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: zpty woes
  2008-05-15 17:53           ` Jaime Vargas
@ 2008-05-20  3:25             ` Jaime Vargas
  2008-05-20 14:28               ` zpty woes (enhancement request) Jaime Vargas
  0 siblings, 1 reply; 15+ messages in thread
From: Jaime Vargas @ 2008-05-20  3:25 UTC (permalink / raw)
  To: Zsh hackers list

More strange behavior. In the script below, two different functions  
doTask and doShellTask are required because when invoking a shell  
script the prompt is repeated and if a read is not done twice then  
`zpty -d ch' will kill the session. This is kind of weird, the same  
happens with the `pwd' command.

Is there a way to avoid this "double prompt" problem? (It is kind of  
hard which commands would require double reads.)

#!/opt/csw/bin/zsh

# FLAGS
set -x                                           # Turn on to debug
zmodload zsh/zpty

# HELPERS
die () {print -r -- $1 >&2; exit 1;}

doTask () {
     zpty -w ch $1
     zpty -t ch || die "Error: command $1 didn't return"
     zpty -r ch line "*aoma6000-4-9? "
     print $line
}

doShellTask () {
     zpty -w ch $1
     zpty -t ch || die "Error: command $1 didn't return"
     zpty -r ch line "*aoma6000-4-9? "
     zpty -r ch line "*aoma6000-4-9? "
     print $line
}


# CREDENTIALS
user='user'
pass='topsecret'
supw='topsecret'
host='host'

# TASKS
task1='cat >test <<"EOF"
     #!/bin/zsh
     who -r
     uname -a
     echo hello >test2.out
EOF
chmod +x test
'

# CONNECT
zpty -b ch ssh -l ${user} ${host}
zpty -t ch || die "connection failed"
zpty -r ch line "*assword:" || die "no password asked"
zpty -w ch ${pass}
zpty -r ch line "*aoma6000-4-9? "; print $line

doTask 'ls -ltr'
doTask ${task1}
doShellTask './test >test.out'
doShellTask 'pwd'

# DISCONNECT
zpty -d ch


On May 15, 2008, at 1:53 PM, Jaime Vargas wrote:

> I got it working. By changing the pattern to '*assword:'. Thanks for  
> the help. -- Jaime
>
>
> On May 15, 2008, at 1:38 PM, Jaime Vargas wrote:
>
>> Still no luck. Am I missing something stupid?
>>
>> nerd% ./zpty-test.zsh
>> +./zpty-test.zsh:4> zmodload zsh/zpty
>> +./zpty-test.zsh:8> zpty scppty scp hello.world 'user@host:~/'
>> +./zpty-test.zsh:9> zpty -t scppty
>> +./zpty-test.zsh:10> zpty -r scppty line assword:
>>
>> #!/opt/csw/bin/zsh
>> set -x
>> zmodload zsh/zpty
>> die() {print -r -- $1 >&2; exit 1;}
>> zpty scppty scp hello.world user@host:~/
>> zpty -t scppty || die "fuck"
>> zpty -r scppty line "assword:" || die "no password asked"
>> zpty -w scppty "TopSecret"
>> while zpty -r scppty line;
>>    do
>>        result+="$line"$'\n'
>>    done
>> zpty -d scppty
>> print $result
>>
>> On May 15, 2008, at 1:03 PM, Stephane Chazelas wrote:
>>
>>> On Thu, May 15, 2008 at 12:00:25PM -0400, Jaime Vargas wrote:
>>>> Still doesn't work for me. Below is the modified script and the  
>>>> debug
>>>> output.
>>>>
>>>> #!/opt/csw/bin/zsh
>>>>
>>>> set -x
>>>>
>>>> zmodload zsh/zpty
>>>>
>>>> die() {print -r -- $1 >&2; exit 1;}
>>>>
>>>> zpty scppty scp hello.world jvargas@aoma6000-4-9.aoma.rhbss.com:~/
>>>> zpty -t scppty || die "fuck"
>>>> zpty -r scppty line "*:" || die "no password asked"
>>>> zpty -w scppty "3lp&tbw"
>>>> while zpty -r scppty line;
>>>>    do
>>>>        result+="$line"$'\n'
>>>>    done
>>>> zpty -d scppty
>>>> print $result
>>>>
>>>> I changed my credentials for security. Basically it now hangs  
>>>> waitng for
>>>> password and doesnt' do anything.  -- Jaime
>>>>
>>>> nerd% ./zpty-test.zsh
>>>> +./zpty-test.zsh:5> zmodload zsh/zpty
>>>> +./zpty-test.zsh:10> zpty scppty scp hello.world 'luser@host:~/'
>>>> +./zpty-test.zsh:11> zpty -t scppty
>>>> +./zpty-test.zsh:12> zpty -r scppty line '*:'
>>>
>>> Had you printed $line, you'd have seen something like
>>> "+myscript:", not "Passwd: ".
>>>
>>>
>>> [...]
>>>> +./zpty-test.zsh:14> zpty -r scppty line
>>> [...]
>>>
>>> As Peter said, if you don't provide with a pattern to look for,
>>> zpty will look for NL characters.
>>>
>>> That last zpty is probably still waiting because so far, it has
>>> only received "Password: " and is waiting for a NL character
>>> that will never come.
>>>
>>> So, in your code above, you should wait for something more
>>> specific than just ":":
>>>
>>> zpty -r scppty line "assword: " || die "no password asked"
>>>
>>> for instance.
>>>
>>> -- 
>>> Stéphane
>>
>


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: zpty woes (enhancement request)
  2008-05-20  3:25             ` Jaime Vargas
@ 2008-05-20 14:28               ` Jaime Vargas
  2008-05-20 16:25                 ` Bart Schaefer
  0 siblings, 1 reply; 15+ messages in thread
From: Jaime Vargas @ 2008-05-20 14:28 UTC (permalink / raw)
  To: Jaime Vargas; +Cc: Zsh hackers list

It would be nice if zpty had a call to inspect the pseudo-terminal  
buffer. I find that `zpty -r' with or without pattern to be very  
difficult to debug. Any ideas on below problem?

On May 19, 2008, at 11:25 PM, Jaime Vargas wrote:

> More strange behavior. In the script below, two different functions  
> doTask and doShellTask are required because when invoking a shell  
> script the prompt is repeated and if a read is not done twice then  
> `zpty -d ch' will kill the session. This is kind of weird, the same  
> happens with the `pwd' command.
>
> Is there a way to avoid this "double prompt" problem? (It is kind  
> of hard which commands would require double reads.)
>
> #!/opt/csw/bin/zsh
>
> # FLAGS
> set -x                                           # Turn on to debug
> zmodload zsh/zpty
>
> # HELPERS
> die () {print -r -- $1 >&2; exit 1;}
>
> doTask () {
>     zpty -w ch $1
>     zpty -t ch || die "Error: command $1 didn't return"
>     zpty -r ch line "*aoma6000-4-9? "
>     print $line
> }
>
> doShellTask () {
>     zpty -w ch $1
>     zpty -t ch || die "Error: command $1 didn't return"
>     zpty -r ch line "*aoma6000-4-9? "
>     zpty -r ch line "*aoma6000-4-9? "
>     print $line
> }
>
>
> # CREDENTIALS
> user='user'
> pass='topsecret'
> supw='topsecret'
> host='host'
>
> # TASKS
> task1='cat >test <<"EOF"
>     #!/bin/zsh
>     who -r
>     uname -a
>     echo hello >test2.out
> EOF
> chmod +x test
> '
>
> # CONNECT
> zpty -b ch ssh -l ${user} ${host}
> zpty -t ch || die "connection failed"
> zpty -r ch line "*assword:" || die "no password asked"
> zpty -w ch ${pass}
> zpty -r ch line "*aoma6000-4-9? "; print $line
>
> doTask 'ls -ltr'
> doTask ${task1}
> doShellTask './test >test.out'
> doShellTask 'pwd'
>
> # DISCONNECT
> zpty -d ch
>
>
> On May 15, 2008, at 1:53 PM, Jaime Vargas wrote:
>
>> I got it working. By changing the pattern to '*assword:'. Thanks  
>> for the help. -- Jaime
>>
>>
>> On May 15, 2008, at 1:38 PM, Jaime Vargas wrote:
>>
>>> Still no luck. Am I missing something stupid?
>>>
>>> nerd% ./zpty-test.zsh
>>> +./zpty-test.zsh:4> zmodload zsh/zpty
>>> +./zpty-test.zsh:8> zpty scppty scp hello.world 'user@host:~/'
>>> +./zpty-test.zsh:9> zpty -t scppty
>>> +./zpty-test.zsh:10> zpty -r scppty line assword:
>>>
>>> #!/opt/csw/bin/zsh
>>> set -x
>>> zmodload zsh/zpty
>>> die() {print -r -- $1 >&2; exit 1;}
>>> zpty scppty scp hello.world user@host:~/
>>> zpty -t scppty || die "fuck"
>>> zpty -r scppty line "assword:" || die "no password asked"
>>> zpty -w scppty "TopSecret"
>>> while zpty -r scppty line;
>>>    do
>>>        result+="$line"$'\n'
>>>    done
>>> zpty -d scppty
>>> print $result
>>>
>>> On May 15, 2008, at 1:03 PM, Stephane Chazelas wrote:
>>>
>>>> On Thu, May 15, 2008 at 12:00:25PM -0400, Jaime Vargas wrote:
>>>>> Still doesn't work for me. Below is the modified script and the  
>>>>> debug
>>>>> output.
>>>>>
>>>>> #!/opt/csw/bin/zsh
>>>>>
>>>>> set -x
>>>>>
>>>>> zmodload zsh/zpty
>>>>>
>>>>> die() {print -r -- $1 >&2; exit 1;}
>>>>>
>>>>> zpty scppty scp hello.world jvargas@aoma6000-4-9.aoma.rhbss.com:~/
>>>>> zpty -t scppty || die "fuck"
>>>>> zpty -r scppty line "*:" || die "no password asked"
>>>>> zpty -w scppty "3lp&tbw"
>>>>> while zpty -r scppty line;
>>>>>    do
>>>>>        result+="$line"$'\n'
>>>>>    done
>>>>> zpty -d scppty
>>>>> print $result
>>>>>
>>>>> I changed my credentials for security. Basically it now hangs  
>>>>> waitng for
>>>>> password and doesnt' do anything.  -- Jaime
>>>>>
>>>>> nerd% ./zpty-test.zsh
>>>>> +./zpty-test.zsh:5> zmodload zsh/zpty
>>>>> +./zpty-test.zsh:10> zpty scppty scp hello.world 'luser@host:~/'
>>>>> +./zpty-test.zsh:11> zpty -t scppty
>>>>> +./zpty-test.zsh:12> zpty -r scppty line '*:'
>>>>
>>>> Had you printed $line, you'd have seen something like
>>>> "+myscript:", not "Passwd: ".
>>>>
>>>>
>>>> [...]
>>>>> +./zpty-test.zsh:14> zpty -r scppty line
>>>> [...]
>>>>
>>>> As Peter said, if you don't provide with a pattern to look for,
>>>> zpty will look for NL characters.
>>>>
>>>> That last zpty is probably still waiting because so far, it has
>>>> only received "Password: " and is waiting for a NL character
>>>> that will never come.
>>>>
>>>> So, in your code above, you should wait for something more
>>>> specific than just ":":
>>>>
>>>> zpty -r scppty line "assword: " || die "no password asked"
>>>>
>>>> for instance.
>>>>
>>>> -- 
>>>> Stéphane
>>>
>>
>


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: zpty woes (enhancement request)
  2008-05-20 14:28               ` zpty woes (enhancement request) Jaime Vargas
@ 2008-05-20 16:25                 ` Bart Schaefer
  0 siblings, 0 replies; 15+ messages in thread
From: Bart Schaefer @ 2008-05-20 16:25 UTC (permalink / raw)
  To: Jaime Vargas; +Cc: Zsh hackers list

On May 20, 10:28am, Jaime Vargas wrote:
}
} It would be nice if zpty had a call to inspect the pseudo-terminal  
} buffer.

There is no "pseudo-terminal buffer" to examine.  From the point of
view of the calling shell, there's only a bi-directional pipe; things
can be read and written exacly once in each direction.  This isn't
like curses where there's an internal "screen image" maintained that
you might peek at.  It's only the process running on the pseudo-tty
that gets a buffered terminal-like view of the interaction.

} I find that `zpty -r' with or without pattern to be very  
} difficult to debug. Any ideas on below problem?

I don't know what might be causing the problem, though I suspect it's
something ssh-related.

Try changing
    zpty -b ch ssh -l ${user} ${host}
to
    zpty -b ch script -q -c \"ssh -l ${user} ${host}\"

Then examine the resulting typescript file.
 


^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2008-05-20 16:26 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-05-15 12:02 zpty woes Jaime Vargas
2008-05-15 13:10 ` Stephane Chazelas
2008-05-15 13:18 ` Peter Stephenson
2008-05-15 14:50   ` Stephane Chazelas
2008-05-15 15:26     ` Peter Stephenson
2008-05-15 16:25     ` Jaime Vargas
2008-05-15 16:36       ` * " Peter Stephenson
     [not found]     ` <99E8D43E-EC34-48DB-A5AD-BFA197A3AAA3@mac.com>
2008-05-15 17:03       ` Stephane Chazelas
     [not found]       ` <480CEB80051F27AE@mac.com>
2008-05-15 17:38         ` Jaime Vargas
2008-05-15 17:53           ` Jaime Vargas
2008-05-20  3:25             ` Jaime Vargas
2008-05-20 14:28               ` zpty woes (enhancement request) Jaime Vargas
2008-05-20 16:25                 ` Bart Schaefer
2008-05-15 13:26 ` zpty woes Stephane Chazelas
2008-05-15 15:44   ` Peter Stephenson

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).