Python sftp到远程服务器读取日志文件

来源:互联网 发布:windows 7破解 编辑:程序博客网 时间:2024/06/13 20:47


#!/usr/bin/python
# -*- coding:utf-8 -*-
import paramiko

remotedir = "/root"
remotefile = "/root/log_history.txt"
hostname = "10.142.152.xxx"
port = 22
username = "user"
password = "******"

paramiko.util.log_to_file('paramiko.log')
s = paramiko.SSHClient()
s.load_system_host_keys()

s.connect(hostname,port,username,password)
command = 'tail -30 /root/log_history.txt'
#command = 'df -h'
stdin,stdout,stderr = s.exec_command(command)
#print(2,stdout.read())
logs = stdout.readlines()
for i in range(len(logs)):
    print(logs[i].rstrip())
s.close()

0 0
原创粉丝点击