---
title: "windows上通过php页面查看linux系统上的各个最新日志信息"
author: "千道5"
date: "2012-02-07T17:34:40Z"
tags: []
categories: ["开源软件"]
slug: "windows-php-linux"
draft: false
lastmod: "2012-02-07T17:38:13Z"
id: 1396769
url: "https://qd5.iteye.com/blog/1396769"
---

采用：php  + paramiko  + python

 

在php页面中，通过exec执行调用.py文件，有两个.py文件

 

其一、为把PHP文件传入linux系统

.py文件一角

sock = socket.socket(socket.AF\_INET, socket.SOCK\_STREAM) #创建套接字接口  
sock.connect((HOST,PORT))  # 连接服务器  
t = paramiko.Transport(sock)  
t.start\_client()  
t.auth\_password(USERNAME,PASSWORD)  
remotefile1\_linux = "/tmp/fetchMyLogs\_doOnServer.php"  
localpath1\_wins = "fetchMyLogs\_doOnServer.php"  
sftptest=paramiko.SFTPClient.from\_transport(t)  
sftptest.put(localpath1\_wins,remotefile1\_linux)

 

 

其二、通过SSH远程执行命令

.py文件一角：

s.connect(hostname=HOST,username=USERNAME,password=PASSWORD)  
stdin,stdout,stderr=s.exec\_command('cd ../tmp/;php fetchMyLogs\_doOnServer.php;')# 执行linux命令  
print stdout.read()

...

...

sftptest=paramiko.SFTPClient.from\_transport(t)  
sftptest.put(localpath1\_wins,remotefile1\_linux)  
sftptest.close()  
t.close()

 

 

最后传回.log文件的最新8K长的最新日志信息。

 

 

 

执行后，获取到的截图如下：

   
![](http://dl.iteye.com/upload/attachment/0062/8360/3c202458-c5ba-3a99-8826-c4f9fce2af0c.jpg)  
 

 

 

 

 

 

 

附：

介绍：Paramiko: SSH and SFTP With Python