1. 代码
import paramiko
 
 
def get_server_log():
    hostname = ""
    port = 22
    username = ""
    password = ""
    client = paramiko.SSHClient()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    client.connect(hostname, port, username, password, compress=True)
    sftp_client = client.open_sftp()
    remote_file = sftp_client.open("/opt/xxx/logs/error_log")
    try:
        count = 0
        for line in remote_file:
            if 'Traceback' in line:
                print(line)
                count = count + 1
        print(count)
    finally:
        remote_file.close()
 
 
if __name__ == "__main__":
    get_server_log()
    print('****done')
  1. 验证 Traceback 在日志文件出现次数

grep -F "Traceback" -R logs/error_log

  1. 普通用户登录时可能报 sftp 权限错误,可以先给改目录分配用户权限

chown -R username:username logs/error_log

  1. 如果不方便使用 chown 授权,但是可以当你用普通账号登录后可以切换成 root 用户,也可以这样写(就是 sudo 切换一下):
client = paramiko.SSHClient()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    client.connect(hostname, port, username, password, compress=True)
    if username != 'root':
        ssh = client.invoke_shell()
        time.sleep(0.1)
        ssh.send('sudo su \n')
        buff = ''
        while not buff.endswith('Enter domain (xxx) password:'): #  这里改成你自己的
            resp = ssh.recv(9999)
            buff += resp
        ssh.send(password)
        ssh.send('\n')
        buff = ''
        while not buff.endswith('# '):
            resp = ssh.recv(9999)
            buff += resp
        ssh.send(remote_command)
        ssh.send('\n')
        buff = ''
        while not buff.endswith('# '):
            resp = ssh.recv(9999)
            buff += resp
        client.close()
        result = buff
    else:
        stdin, stdout, stderr = client.exec_command(remote_command)
        result = stdout.read()