connect to linux server from windows machine and need to extract last 1000 lines from log file and write to seperate text file in python

  linux, python, windows
for server in servers:
    try:
        ssh = connectToSSH(server.attributes['host'].value, server.attributes['keyfilepath'].value,
                           server.attributes['username'].value, server.attributes['password'].value)
        folders = server.getElementsByTagName('folder')
        for folder in folders:

         stdout=executeCommand(ssh, 'tail -1000f /opt/hybris1875/hybris/log/tomcat/console-20200406.log')
         logFiles = stdout.read().splitlines()
    except Exception as e:
        print(e)

below is the class to Connect to SSH from windows:

def connectToSSH(server, keyFilePath, userName, passWord):
    try:
        logging.info('Connecting to SSH..')
        k = paramiko.RSAKey(filename=keyFilePath)
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(server, username=userName, password=passWord, pkey=k)
        logging.info('Connection established')
        return ssh
    except Exception as e:
        logging.error('Error occurred while establishing connection')
        logging.error(e)
        raise

Source: Windows Questions

LEAVE A COMMENT