0

i use the following class to run commands via SSH on a remote server:

class ShellHandler:

    def __init__(self, host, user, psw):
        self.ssh = paramiko.SSHClient()
        self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        self.ssh.connect(host, username=user, password=psw, port=22)

        channel = self.ssh.invoke_shell()
        self.stdin = channel.makefile('wb')
        self.stdout = channel.makefile('r')
        self.shell = channel

    def __del__(self):
        self.ssh.close()

    def ftp(self):
        ftp_client = self.ssh.open_sftp()
        return ftp_client

    def execute(self, cmd):
        """

        :param cmd: the command to be executed on the remote computer
        :examples:  execute('ls')
                    execute('finger')
                    execute('cd folder_name')
        """
        cmd = cmd.strip('\n')
        self.stdin.write(cmd + '\n')
        finish = 'end of stdOUT buffer. finished with exit status'
        echo_cmd = 'echo {} $?'.format(finish)
        self.stdin.write(echo_cmd + '\n')
        shin = self.stdin
        self.stdin.flush()

        shout = []
        sherr = []
        exit_status = 0
        for line in self.stdout:
            if str(line).startswith("FINISHED"): # add "; echo FINISHED" to the end of commands that take much longer to excute
                break
            if str(line).startswith(cmd) or str(line).startswith(echo_cmd):
                # up for now filled with shell junk from stdin
                shout = []
            elif str(line).startswith(finish):
                # our finish command ends with the exit status
                exit_status = int(str(line).rsplit(maxsplit=1)[1])
                if exit_status:
                    # stderr is combined with stdout.
                    # thus, swap sherr with shout in a case of failure.
                    sherr = shout
                    shout = []
                break
            else:
                # get rid of 'coloring and formatting' special characters
                shout.append(re.compile(r'(\x9B|\x1B\[)[0-?]*[ -/]*[@-~]').sub('', line).
                             replace('\b', '').replace('\r', ''))

        # first and last lines of shout/sherr contain a prompt
        if shout and echo_cmd in shout[-1]:
            shout.pop()
        if shout and cmd in shout[0]:
            shout.pop(0)
        if sherr and echo_cmd in sherr[-1]:
            sherr.pop()
        if sherr and cmd in sherr[0]:
            sherr.pop(0)

        return shin, shout, sherr

i run my commands as follows:

server= ShellHandler(ip_address, username, password)
cmd = avamar.execute("whoami")
output = cmd[1]

it almost works most of the time, but sometimes it gets stuck and when I check my log file, this is all it says:

DEBUG - [chan 0] Unhandled channel request "keepalive@openssh.com"

what does this output mean, and how to fix it. I'd appreciate it if anyone can point me in the right direction.

Adam
  • 53
  • 6
  • You have asked this already: https://stackoverflow.com/q/71881448/850848 – Martin Prikryl Apr 20 '22 at 17:44
  • yea i just realized the problem probably has nothing to do with su - – Adam Apr 20 '22 at 17:48
  • because this time it got hug when i was running commands normally as user admin – Adam Apr 20 '22 at 17:49
  • I'm trying to learn more about what is ServerAliveInterval ClientAliveInterval and if that could be a possible fix to my problem – Adam Apr 20 '22 at 17:50
  • i'm just wondering if someone ran into the similar output, maybe they can guide me to the right direction – Adam Apr 20 '22 at 17:50
  • So instead of posting new nearly identical question, edit your original question will all your new findings. And answer my question from your other post. + I do not think that your problem has anything to do with any "keepaliaves". I'm quite sure you are on a wrong track. – Martin Prikryl Apr 21 '22 at 06:14

0 Answers0