I am writing a script that will get execute a few commands on the terminal using
stdin, stdout, stderr = ssh_client.exec_command('date')
I know that script screen.log will create a log file and on executing exit The output is logged. It works when I type it manually on the terminal but does not work when run using paramiko stdin, stdout, stderr = ssh_client.exec_command('script screen.log')
Attaching the complete code that I use.
import paramiko
import os.path
import sys
import requests
import subprocess
import logging
def passing(ans):
ssh_client=paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname='devserver.com',username='username',password='password')
stdin, stdout, stderr = ssh_client.exec_command("script screen.log")
for x in range(len(ans)):
stdin, stdout, stderr = ssh_client.exec_command(ans[x])
stdin, stdout, stderr = ssh_client.exec_command("netstat")
stdin, stdout, stderr = ssh_client.exec_command("exit")
#stdin, stdout, stderr = ssh_client.exec_command("netstat")
logging.basicConfig()
logging.getLogger("paramiko").setLevel(logging.WARNING)
paramiko.util.log_to_file("screen.log")
cmd_output=stdout.read()
print(cmd_output)
passing(['script screen.log','netstat','ls','date','exit'])
This creates screen.log but does not write the output to screen.log.
It would be really appreciated if you could help me write the output to screen.log.