Re: [paramiko] Using invoke_shell to su root

2010-02-08 Thread Wan Li
Now I'm trying to figure it out by set a short channel timeout.
Work flow is basically like this:
1. Start a new thread to set the channel timeout to one second or so.
2. buffer = channel. recv(256)   output += buffer in a while True: loop,
break if timeout occurs.
3. In the main thread, make the new thread join(0.5) if isAlive()
4. If it's NOT alive, process the output as you want.

It may add additional 1-2s' delay, But it doesn't matter for me. :)
Any idea to improve it?

On Sun, Feb 7, 2010 at 9:28 PM, Wan Li wanli...@gmail.com wrote:


 I have one solution for this problem:

def execute(self, command):
Execute the given commands on a remote machine.
channel = self._transport.open_session()
channel.exec_command(command)
if command.startswith(sudo):
time.sleep(1)
channel.send(self.password + \n)
time.sleep(1)
channel.send(self.password + \n)
output = channel.makefile('rb', -1).readlines()
if not output:
output = channel.makefile_stderr('rb', -1).readlines()
channel.close()
return output

 it's really works, but have one problem, when the sudo version is newest
 or equal than 1.7.1, this doesn't works, i paste the output when we use
 the last version of sudo.

 sudo: no tty present and no askpass program specified


 Maybe we should go back to invoke_shell() call, not the open_session() one,
 isn't it?

 --
 : ~




-- 
: ~
___
paramiko mailing list
paramiko@lag.net
http://www.lag.net/cgi-bin/mailman/listinfo/paramiko

Re: [paramiko] Using invoke_shell to su root

2010-02-07 Thread Wan Li
 I have one solution for this problem:

def execute(self, command):
Execute the given commands on a remote machine.
channel = self._transport.open_session()
channel.exec_command(command)
if command.startswith(sudo):
time.sleep(1)
channel.send(self.password + \n)
time.sleep(1)
channel.send(self.password + \n)
output = channel.makefile('rb', -1).readlines()
if not output:
output = channel.makefile_stderr('rb', -1).readlines()
channel.close()
return output

 it's really works, but have one problem, when the sudo version is newest
 or equal than 1.7.1, this doesn't works, i paste the output when we use
 the last version of sudo.

 sudo: no tty present and no askpass program specified


Maybe we should go back to invoke_shell() call, not the open_session() one,
isn't it?

-- 
: ~
___
paramiko mailing list
paramiko@lag.net
http://www.lag.net/cgi-bin/mailman/listinfo/paramiko

[paramiko] Using invoke_shell to su root

2010-02-04 Thread Wan Li
Hi,

Sometimes I need to su root and execute some command, I tried to use the
chan = invoke_shell(), chan.send(su root) , wait the output and finally
send the passwd. After that, I use the chan.send(whoami) to check if it's
successful executed.
Bu I found the behavior is a bit different from the formal exec_command()
call, basically because of the channel management. For the exec_command
style, seems like each time a new channel is produced, I can easily set a
timeout to close it. But for the invoke_shell() style I need to reuse that
channel and I can't simply close the channel, because if it's closed, then I
need to invoke_shell() and su root again.

So is there a way to su root only one time to make each exec_command take
the advantage of root privilege?

Thanks.

-- 
: ~
___
paramiko mailing list
paramiko@lag.net
http://www.lag.net/cgi-bin/mailman/listinfo/paramiko

Re: [paramiko] Using invoke_shell to su root

2010-02-04 Thread Wan Li
I'm also looking into the demo.py to get some inspirations.
The input and output are in different thread, it's OK a command line usage.
But how can I know whether the command's output recv() is totally complete?
Is there a eof_received or command_output_eof like state or even a event
to check?

On Fri, Feb 5, 2010 at 11:33 AM, Wan Li wanli...@gmail.com wrote:

 On Fri, Feb 5, 2010 at 10:36 AM, Charles Duffy char...@dyfis.net wrote:

 Instead of needing to bother with chan.send(), just use exec_command()
 to invoke su - -c 'whatever_set_of_commands_you_need_to_run'.

 For instance, I have a helper I often use which uses sftp to upload a
 script into a temporary directory, and then over the same Transport
 uses exec_command() to run that script. You could do the same, using a
 single exec_command() call to run su with a command line which starts
 the script you uploaded, putting everything which needs to be run as
 root within that script.


 It's a nice solution, but the output of each step is very important for me
 and sometimes interactive inputs are required, so I think it will be hard to
 achieve my aim if using this solution.

 --
 : ~


-- 
: ~
___
paramiko mailing list
paramiko@lag.net
http://www.lag.net/cgi-bin/mailman/listinfo/paramiko

[paramiko] All kinds of timeout in paramiko

2010-02-02 Thread Wan Li
Hi gurus,

As I'm using paramiko for a long-running application, sometimes I found the
ssh command execution will never return a result which basically because
there are many operations lack of timeout support. For example, command
execution don't have timeout option, ssh negotiation don't have it and even
ssh password auth process don't have ether.
I think many other users may hit those bugs and don't even know what's
happened, shall we add timeout option for all these operations?
Correct me if I'm obviously wrong.

Thanks

-- 
: ~
___
paramiko mailing list
paramiko@lag.net
http://www.lag.net/cgi-bin/mailman/listinfo/paramiko

Re: [paramiko] Add timeout for exe_command() ?

2010-01-15 Thread Wan Li
On Fri, Jan 15, 2010 at 11:41 PM, james bardin jbar...@bu.edu wrote:

 On Fri, Jan 15, 2010 at 5:18 AM, Wan Li wanli...@gmail.com wrote:
 
  Shall I simply call ssh.close() first and login again to make the retry
  works? Or maybe there is a way to cancel the exe_command?
 
 
  I spend some time looking into paramiko. Here are thoughts in my mind,
  correct me if I'm wrong. :-)
 
  Basically, we initialize a SSHClient, call connect() to establish the
  connection(a new Transport), exe_command will call the open_session() to
 add
  a new channel for this transport which will be stored in
  Transport._channels. If I wanna cancel the exe_command, the best way may
 be
  simply call
 ssh._transport._channels[ssh._transport.channel_count].close()
  to close the previous channel and call the exe_command() again.
 

 That's probably not a good way to go about it. There's no guarantee
 that ssh._transport._channels[ssh._transport.channel_count] will
 return the channel you expect
 (not to mention, _transport and _channels are marked private).


You're right, the returned channel may not be the one I expected. But I can
mark it after I call the exec_command.


 Just take the transport (get_transport), and run exec_command() on
 your own. SSHClient.exec_command is only 5 lines of code, and then you
 can do whatever you want with the channel.


It's good way to mark the channel, but I don't know if it's the right
approach to work around my problem.

-- 
: ~
___
paramiko mailing list
paramiko@lag.net
http://www.lag.net/cgi-bin/mailman/listinfo/paramiko

[paramiko] Add timeout for exe_command() ?

2010-01-14 Thread Wan Li
Hi Gurus,

I wanna control the command execution timeout of SSH client, but I found
paramiko doesn't provide such an option. So I do it myself in this way:
Throw the ssh.exe_command to another thread which will quit after I get the
stdout.readlines(), sleep in main thread and check if the thread is still
alive. If it's true, I will start a new thread to call ssh.exe_command again
to execute the previous command. After many tries, if the result of this
command is still not available, I will throw an exception.

The main problem here is: If the first exe_command is timed out, then the
retry calls will never succeed.
I'm not so familiar with the paramiko itself, but I doubt the following
commands may NOT be executed as the previous one blocks there.

Shall I simply call ssh.close() first and login again to make the retry
works? Or maybe there is a way to cancel the exe_command?

Thanks in advanced.

-- 
: ~
___
paramiko mailing list
paramiko@lag.net
http://www.lag.net/cgi-bin/mailman/listinfo/paramiko