Thursday, September 1, 2022

[SOLVED] paramiko - error with a large wget command

Issue

this is my code:

import paramiko
import time

host = "123.456.789"
username = "myusername"
password = "mypassword"

client = paramiko.client.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(host, username=username, password=password)
_stdin, _stdout,_stderr = client.exec_command("sudo -i")
_stdin.write(password + '\n')
_stdin, _stdout,_stderr = client.exec_command("sudo wget -O- 'https://abc.com.gz' | gunzip | dd of=/dev/sda", get_pty=True)
_stdin.flush()
#Print content
for line in _stdout.readlines():
   print(_stdout.read().decode())
# Close ssh connect
time.sleep(5)
client.close()

the result I get is the screen doesn't print anything, and after a period of ~30-40 minutes the server doesn't receive any files from the wget command....


Solution

Try to invoke the Shell instead:

import paramiko, time

host = "123.456.789"
username = "myusername"
password = "mypassword"
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(host, username=username, password=password)
remote_shell = client.invoke_shell()

remote_shell.send("sudo -i")
remote_shell.send(password + "\n")
remote_shell.send("sudo wget -O- 'https://abc.com.gz' | gunzip | dd of=/dev/sda")

# print reply
time.sleep(1)
print(remote_shell.recv(65535))
time.sleep(1)
client.close()


Answered By - user56700
Answer Checked By - Mary Flores (WPSolving Volunteer)