not getting input from ssh server

17 views
Skip to first unread message

klut...@gmail.com

unread,
Oct 28, 2020, 7:02:02 PM10/28/20
to asyncssh-users
First i want to say thank you for amazing project.
i have issue to get data from the SSHReader when using ssh server:

this is the "process_factory" 
============================== 
async def handle_client(process):
    while 1:
        process.stdout.write("< ")
        print("HERE")
        data = await process.stdin.read(5)
        print("There")
        logger.info(data)
        await asyncio.sleep(1)        
    process.exit(0)
==============================  

And on debug i can see i get data
================================
DEBUG:asyncssh:[conn=0, chan=0] Sending 2 data bytes
DEBUG:asyncssh:[conn=0, pktid=17] Sent MSG_IGNORE (2), 5 bytes
  00000000: 02 00 00 00 00                                   .....
DEBUG:asyncssh:[conn=0, chan=0, pktid=18] Sent MSG_CHANNEL_DATA (94), 11 bytes
  00000000: 5e 00 00 00 00 00 00 00 02 3c 20                 ^........<
HERE
DEBUG:asyncssh:[conn=0, chan=0, pktid=10] Received MSG_CHANNEL_DATA (94), 11 bytes
  00000000: 5e 00 00 00 00 00 00 00 02 3b 3b                 ^........;;
DEBUG:asyncssh:[conn=0, chan=0] Received 2 data bytes
DEBUG:asyncssh:[conn=0, chan=0] Sending 1 data byte
DEBUG:asyncssh:[conn=0, pktid=19] Sent MSG_IGNORE (2), 5 bytes
  00000000: 02 00 00 00 00                                   .....
DEBUG:asyncssh:[conn=0, chan=0, pktid=20] Sent MSG_CHANNEL_DATA (94), 10 bytes
  00000000: 5e 00 00 00 00 00 00 00 01 3b                    ^........;
DEBUG:asyncssh:[conn=0, chan=0] Sending 1 data byte
DEBUG:asyncssh:[conn=0, pktid=21] Sent MSG_IGNORE (2), 5 bytes
  00000000: 02 00 00 00 00                                   .....
DEBUG:asyncssh:[conn=0, chan=0, pktid=22] Sent MSG_CHANNEL_DATA (94), 10 bytes
  00000000: 5e 00 00 00 00 00 00 00 01 3b                    ^........;
DEBUG:asyncssh:[conn=0, chan=0, pktid=11] Received MSG_CHANNEL_DATA (94), 10 bytes
  00000000: 5e 00 00 00 00 00 00 00 01 3b                    ^........;
DEBUG:asyncssh:[conn=0, chan=0] Received 1 data byte
DEBUG:asyncssh:[conn=0, chan=0] Sending 1 data byte
DEBUG:asyncssh:[conn=0, pktid=23] Sent MSG_IGNORE (2), 5 bytes
  00000000: 02 00 00 00 00                                   .....
DEBUG:asyncssh:[conn=0, chan=0, pktid=24] Sent MSG_CHANNEL_DATA (94), 10 bytes
  00000000: 5e 00 00 00 00 00 00 00 01 3b                    ^........;
DEBUG:asyncssh:[conn=0, chan=0, pktid=12] Received MSG_CHANNEL_DATA (94), 10 bytes
  00000000: 5e 00 00 00 00 00 00 00 01 3b                    ^........;
DEBUG:asyncssh:[conn=0, chan=0] Received 1 data byte
DEBUG:asyncssh:[conn=0, chan=0] Sending 1 data byte
DEBUG:asyncssh:[conn=0, pktid=25] Sent MSG_IGNORE (2), 5 bytes
  00000000: 02 00 00 00 00                                   .....
DEBUG:asyncssh:[conn=0, chan=0, pktid=26] Sent MSG_CHANNEL_DATA (94), 10 bytes
  00000000: 5e 00 00 00 00 00 00 00 01 3b                    ^........;
DEBUG:asyncssh:[conn=0, chan=0, pktid=13] Received MSG_CHANNEL_DATA (94), 10 bytes
  00000000: 5e 00 00 00 00 00 00 00 01 3b                    ^........;
DEBUG:asyncssh:[conn=0, chan=0] Received 1 data byte
DEBUG:asyncssh:[conn=0, chan=0] Sending 1 data byte
=====================================

but never get into print("There") on the client_handler.
i also tried readuntil(';') and readexactly(1)

Thanks

Ron Frederick

unread,
Oct 28, 2020, 9:35:04 PM10/28/20
to klut...@gmail.com, asyncssh-users
Hello,

I think the problem here is that you have line editing enabled. It’s enabled by default if the client requests a PTY when opening the connection. That’s why you are seeing sends of the “;” characters in the log output — the line editor is echoing those characters back to the client as it adds them to the input line. However, none of that data will be returned to your read() call until the client sends a newline, as the line editor operates in line-at-a-time mode (allowing things like input editing before the bytes are passed to the application).

You can disable this by setting the option line_editor to False when you create the listener, and you’ll want to do that if you don’t want line at a time input with things like input echo back to the client and history support.


-- 
Ron Frederick
ro...@timeheart.net



klut...@gmail.com

unread,
Oct 29, 2020, 4:25:17 AM10/29/20
to asyncssh-users
hey,
Disabling line_editor worked and not i get the data.

is there a way to work with asnyc generator
async for line in process.stdin:
    print(line)
but setting the newline to ";"?


ב-יום חמישי, 29 באוקטובר 2020 בשעה 3:35:04 UTC+2, ‪Ron Frederick‬‏ כתב/ה:

klut...@gmail.com

unread,
Oct 29, 2020, 4:28:17 AM10/29/20
to asyncssh-users
if i wil look at the SSHReader:

async def __anext__(self):
        """Return one line at a time when used as an async iterator"""

        line = await self.readline()

        if line:
            return line
        else:
            raise StopAsyncIteration
async def readline(self):
        try:
            return await self.readuntil(_NEWLINE)
        except asyncio.IncompleteReadError as exc:
            return exc.partial

but i can't change the _NEWLINE, maybe this is an feature request.
ב-יום חמישי, 29 באוקטובר 2020 בשעה 10:25:17 UTC+2, klut...@gmail.com כתב/ה:

Ron Frederick

unread,
Oct 29, 2020, 9:35:49 AM10/29/20
to klut...@gmail.com, asyncssh-users
AsyncSSH currently mirrors the behavior of asyncio’s StreamReader, which hardcodes a call to self.readline() when used as an async iterator. However, it would be very easy for you to build your own async iterator that reads whatever you want. It would look something like:

class MySSHIterator:
    def __init__(self, reader):
        self.reader = reader

    def __aiter__(self):
        return self

    async def __anext__(self):
        result = await self.reader.readline()    # Replace this with whatever read function you want on self.reader

        if result == b'':
            raise StopAsyncIteration

        return result

Reply all
Reply to author
Forward
0 new messages