0

When channel_data is printed with no indexing I receive a fairly standard output from a windows terminal.

I'm running this, expecting to receive the #### followed by the last 5 characters of channel_data. Instead I'm receiving [?25h. I assume this is some literal byte translation of something?

What's confusing me also is when I change channel_data[-5::] to channel_data[-30::] I get a complete different message which includes indows\system32\cmd.exe and I think I hear some windows error noise also. What is causing this to happen?

Finally, how can I figure out what to put in my if endswith condition, so that I'm prompted for an input once the terminal is 'loaded'? I've tried several arguments for this method, including \n new line strings. Is there just some paramiko method I should be using instead?

import paramiko
import os
import sys

host   = '...'
user   = '...'
passwd = '...'
ssh    = paramiko.SSHClient()

ssh.load_system_host_keys()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host, port=22, username=user, password=passwd, look_for_keys=False, allow_agent=False)
#command = os.path.abspath("...")
command = 'echo Test'
channel = ssh.invoke_shell()

channel_data = str()
host = str()
srcfile = str()

while True:
    if channel.recv_ready():
        channel_data+=str(channel.recv(9999))
        os.system('cls')
        print('#######################')
        #print(channel_data)
        print(channel_data[-5::])
    else:
        continue

    if channel_data.endswith('>'):
        print("Continue (y)")
        user_input = str(input())
        
        if user_input == 'y':
            channel.send(command)
            channel.send('\n')
        else:
            break
    
Rory McDonald
  • 253
  • 2
  • 8
  • @MartinPrikryl hm perhaps I'm doing this all incorrectly. I was having an issue with exec_command (that there probably exists a workaround for): I executed, via exec_command, a script.cmd which sets up environment variables then finally runs a second.bat file. After this point the stdout ended (ends in C:/.../second.bat), but I need to see and respond to what happens after further files are called. Perhaps there's an easier way, I've been reading through the docs to but I don't have enough sysadmin understanding to know which route I should be going, or to interpret a lot of the documentation. – Rory McDonald Jun 28 '21 at 15:33
  • This is the problem with SO. Try to find a solution on your own and people complain about your solution, ask people how to solve something and people complain that you don't try to find your own solution. I'm not ungrateful for your help, it's just very difficult to ask questions as a beginner. – Rory McDonald Jun 28 '21 at 18:20
  • Ah yes will do, I always forget to upvote comments on stack sites. – Rory McDonald Jun 28 '21 at 19:00
  • Ah I see, will do. – Rory McDonald Jun 28 '21 at 19:01
  • My mistake, I'd upvoted the questions in a rush. All the answers helped me. – Rory McDonald Jun 29 '21 at 08:11

0 Answers0