KBNanda
KBNanda

Reputation: 667

Reading standard output second time from Paramiko ssh client did not work

I have a python function to read a specific docker container log.

First I try to get the container id as below

def test_1():
    logging.info("Test debug logs")
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect('10.18.18.10', username='usr1', password='pwd1')
    ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command('sudo docker ps -aqf "name=container1"')
    container_id = ssh_stdout.read().decode("utf-8").replace('\n', '')   
    ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command(f'sudo docker logs -f {container_id} --since 4m')
    container_logs = ssh_stdout.read()
    ssh.close()

I am getting the container_id fine. But I am not getting the container_logs. The execution keeps going when I try to retrieve the logs.

P.S. Anyway technically I can read the docker logs by container name too. But I need to know why I was not able to read the output the second time.

Upvotes: 0

Views: 79

Answers (1)

KBNanda
KBNanda

Reputation: 667

The docker command will keep waiting because it's executed to show live logs.

To solve that I need to do as below

ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command(f'sudo docker logs {container_id} --since 4m')

The '-f' need to be removed from the command so that it won't keep holding.

Upvotes: 1

Related Questions