K
K
Koipse2021-04-25 00:58:19
Python
Koipse, 2021-04-25 00:58:19

Proxmox | Python | Paramiko | Entering a container?

Greetings!

Essence of a question: There is a server that works at proxmox. There are containers that are spinning there. Can someone please suggest a way to write a method to "inject" into a container through a server we ssh into using Paramiko?

What do we have?

1. SSH class with initialization
2. A method that implements a connection to the server using already known parameters.

def connect_to_server(self):
        try:
            self.client.connect(hostname=self.connection, username=self.login, password=self.password, port=22,
                                auth_timeout=True, timeout=5)
        except ssh.ssh_exception.AuthenticationException:
            print('Ошибка при логировании на сервер')
            self.close_connection()
        except socket.error:
            print("Сервер недоступен!")
            self.close_connection()


There is a simple method thanks to which I can see all the container inside. Ordinary conclusion.

def show_containers(self):
        if not self.closeSSH:
            stdin, stdout, stderr = self.client.exec_command("pct list", get_pty=True)
            stdin.write('')
            stdin.flush()
            data = stdout.read() + stderr.read()
            print(data.decode())
        else:
            return "No connection to server"


No matter how I tried to enter any container, when checking the "correctness" of the entry, I looked at the hostname of the container, trying the command , but all the time the output was the same - "mainserver", when I needed a different output, for example: "slavecontainer" I tried to enter the container by writing a similar code that I used to list containers, but nothing sensible happens:.exec_command("cat /etc/hostname, get_pty=True)


def enter_to_server(self):
        if not self.closeSSH:
            self.client.exec_command("pct enter 81103", get_pty=True) [Заходим внутрь контейнера]
            stdin, stdout, stderr = self.client.exec_command("cat /etc/hostname", get_pty=True) [Пытаемся узнать имя контейнера]
            stdin.write('')
            stdin.flush()
            data = stdout.read() + stderr.read()
            print(data.decode()) [Выводит "mainserver"]
        else:
            return "No connection to server"


I suspect that somewhere it is necessary, I can put it incorrectly, to intercept the moment of entering the container, akin to another tty, but it seems that the command is either not executed at all, or the request is coming, but we ourselves "remained" at the same point.

Well, a stupid implementation (because there is simply nothing to output):

def enter_to_server(self):
        if not self.closeSSH:
            stdin, stdout, stderr = self.client.exec_command("pct enter 81103", get_pty=True)
            stdin.write('')
            stdin.flush()
            data = stdout.read() + stderr.read()
            print(data.decode())
        else:
            return "No connection to server"


Of course, when executing:
stdin, stdout, stderr = self.client.exec_command("pct enter 81103", get_pty=True)


We will just hang, and when we add an argument with a timeout, we will run into paramiko.buffered_pipe.PipeTimeout and socket.timeout errors.

This raises the question: perhaps the moment of "transition" from the server to the container can be intercepted for the correct execution of the command and subsequent output? Subprocesses, for example?

Thank you for attention.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
AUser0, 2021-04-25
@AUser0

I am not an expert in python, but I can tell you something:

stdin, stdout, stderr = self.client.exec_command("echo 'pwd ; exit' | pct enter 81103", get_pty=True)

Well, the explanation: IMHO, exec_command executes the command and waits for its completion in order to return the result. And you have an interactive shell there, to which no one gives commands, especially commands to close the shell itself. Or do invoke_shell and send, or this way.
PS role model

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question