Hi Steve,
On Sep 1, 2015, at 9:41 PM, Steve <
steve....@gmail.com> wrote:
> I'm new to python and asyncssh. This is just from copying the simple client example:
http://asyncssh.readthedocs.org/en/latest/#simple-client
>
> I need to use asyncssh to ssh from host into the a server1 and then ssh from server1 into server2. My username and password on server1 differs from my username and password on server2. The port into server1, 24698, differs from into server2, 22.
>
> I connect to server1 and succesfully get result from ls command. Then I try to ssh to server2, but it fails.
>
> with conn:
> chan, session = yield from conn.create_session(MySSHClientSession, 'ls -l')
> chan, session = yield from conn.create_session(MySSHClientSession, 'ssh uname2@server2')
> chan, session = yield from conn.create_session(MySSHClientSession, "password2")
> chan, session = yield from conn.create_session(MySSHClientSession, "ls -l")
>
>
> I get this error:
> Pseudo-terminal will not be allocated because stdin is not a terminal.
>
> Any ideas? Any assistance would be much appreciated.
You’re not going to want to use multiple calls to create_session() here, at least not for sending multiple lines of input to the same upstream session. You’d either want to do writes to the channel you get back to provide input (as in the “math_client” example of writing input to the ‘bc’ program) or you’d want to provide the extra input on the command line of the command you invoke.
Since ssh doesn’t provide a way to directly provide the password as a command line argument, you’d either want to switch to key-based authentication between server1 and server2, with the appropriate keys in your .ssh directory on either side so that server2 doesn’t challenge for a password, or you might want to consider installing the ‘sshpass’ program if you absolutely must use password-based auth. Then, you could do something like:
with conn:
chan, session = yield from conn.create_session(MySSHClientSession,
‘sshpass -p password2 ssh uname2@server2 ls -l')
If you wanted to write the password yourself, you’d need to wait until you got the ‘Password:’ prompt from server1 in your data_received() function and then do a chan.write() of the password (followed by a newline). You could then read and output the rest of what it sends back to you to stdout as you did in the example.
For interactive code like this, the higher-level streams API is probably better to use that the callback-based approach. That would let you block until you received the ‘Password:’ prompt and then respond to it and go into a loop to display the rest of the output all in a single coroutine. However, it’s still somewhat involved. There’s no guarantee you’ll get all of the output of the ‘Password:’ prompt in a single read call for instance. That’s why the approach outlined above of using either key-based auth or sshpass is likely to be much easier.
--
Ron Frederick
ro...@timeheart.net