[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Multiple calls to channel_request_exec()


Hi,

Sorry, I don't agree.
While channel_request_shell() will span only one shell, the output of
your own command as well as the command prompt will not appear if you do
not call channel_request_pty(). You should not request a pty, because
it's for human, not for scripts.
Basicaly, you should write an interactive shell script around the shell
interpretor of the destination.
To have an idea on how the output will be, just type
cat | sh
in your interactive shell. Then type commands.
Here is an example:
aris@aris-belnet:~$ cat | sh
id
uid=1000(aris) gid=1000(aris)
groupes=4(adm),20(dialout),24(cdrom),46(plugdev),104(lpadmin),115(admin),120(sambashare),1000(aris)
id && echo OK || echo KO
uid=1000(aris) gid=1000(aris)
groupes=4(adm),20(dialout),24(cdrom),46(plugdev),104(lpadmin),115(admin),120(sambashare),1000(aris)
OK
cat /etc/shadow && echo OK || echo KO
cat: /etc/shadow: Permission non accordée
KO
exit

aris@aris-belnet:~$
Also take note that the error message (on cat for instance) is written
in stderr.

When I told "do not try to parse the prompt", I meant that trying to
wait for a $ or a # is prone to errors and will not work as you want, as
the pseudo-terminal (pty) interactive interface is meant for human and
not for scripts.
Just write a shell script around a non-pty shell, and use the && and ||
together with an echo command to do the end-of-execution processing if
needed.

kr,
Aris

Stephan Kountso a écrit :
>     So basically if I use channel_request_shell() then the environment on
>     the server will be retained between subsequent calls to
>     channel_request_shell() ?
> 
> When you requesting shell, then server actually starts shell for you.
> And you program will get all text you see, when you are connecting to
> server with openssh or putty client for example. And yes, you'll receive
> all prompts and duplication commands you input. And no, you won't be
> noticed when one program stops execution and shell is waiting for next
> command.
> 
> 
>     I don't fully understand the statement(s) about not being able to parse
>     the $ or # prompts (or in the previous email; "you must know shell
>     prompt before you begin communication". Is this because the shell prompt
>     is included in the contents of channel_read() ?
> 
> Yes. And because on different machines you may have (actually, you
> really will have) different prompts. So, there is no way to figure out
> current prompt in runtime.
> 
> 
>     Thanks, Mark
> 
>     On Thu, 2010-02-25 at 10:19 +0100, Aris Adamantiadis wrote:
>     > Hi,
>     >
>     > Indeed, you can execute only one command using
>     channel_request_exec. But
>     > you may either
>     >
>     > -execute several commands
>     > -start a scripting language
>     > example:
>     > channel_request_exec(channel,"cd /tmp; mkdir mytest; cd mytest; touch
>     > mytest");
>     > This will be executed as only one shell command. Another solution is
>     > // Do NOT put the channel into interactive mode/pty
>     > channel_request_shell(channel);
>     > channel_write(channel,"cd /tmp ; echo OK");
>     > channel_read(...)
>     > channel_write(channel,"mkdir mytest ; echo OK");
>     > ...
>     > basicaly that's like a shell script. Do not expect being able to parse
>     > the "#" or "$" prompts, it won't work...
>     >
>     > hope this helps.
>     >
>     > Aris
>     >
>     > Mark Hessling a écrit :
>     > > I'm looking at libssh to enable the replacement of an existing
>     > > application that uses raw sockets to control a telnet session.
>     In future
>     > > the connection must be done using ssh.
>     > >
>     > > I tried modifying examples/exec.c and duplicated the block of
>     code that
>     > > calls channel_request_exec() to execute "ps aux" and to read the
>     output.
>     > > I simply added a call to channel_request_exec() to execute "ls
>     -l", but
>     > > I received an error: "Channel exec request failed".
>     > >
>     > > Should I be able to with libssh, execute a shell command on the
>     remote
>     > > host, read its output and execute another shell command and read its
>     > > output?
>     > >
>     > >>From my reading of the documentation it appears that each call to
>     > > channel_request_exec() spawns another shell on the remote
>     server, so if
>     > > I wanted to execute the following on the remote server:
>     > > "cd tmp"
>     > > "./run_my_command"
>     > > then the second command would not be executed in the "tmp"
>     directory.
>     > >
>     > > Does libssh then need a "changedirectory" function similar to
>     the one
>     > > that sets environment variables?
>     > >
>     > > Thanks in advance for your responses.
>     > >
>     >
>     >
> 
> 
>     --
> 
>     * Mark Hessling, mark@xxxxxxxx <mailto:mark@xxxxxxxx>
>     http://www.rexx.org/
>     * Author of THE, a Free XEDIT/KEDIT editor, Rexx/SQL, Rexx/CURL,  etc.
>     * Maintainer of Regina Rexx interpreter and Rexx/Tk
>     * Use Rexx? join the Rexx Language Association: http://www.rexxla.org/
> 
> 
> 
> 
> 
> -- 
> Best Wishes,
> Stephan Kountso aka StepLg


Follow-Ups:
Re: Multiple calls to channel_request_exec()Stephan Kountso <steplg@xxxxxxxxx>
Re: Multiple calls to channel_request_exec()Mark Hessling <mark@xxxxxxxx>
References:
Multiple calls to channel_request_exec()Mark Hessling <mark@xxxxxxxx>
Re: Multiple calls to channel_request_exec()Aris Adamantiadis <aris@xxxxxxxxxxxx>
Re: Multiple calls to channel_request_exec()Mark Hessling <mark@xxxxxxxx>
Re: Multiple calls to channel_request_exec()Stephan Kountso <steplg@xxxxxxxxx>
Archive administrator: postmaster@lists.cynapses.org