2016-12-01 11 views
-2

pythonを使ってリモートサーバーに接続したいのですが、同じサーバーからphpのデータを取得します。PythonからSsh接続し、PHPからファイルを読み込みますか?

+0

SOは個人のメモを保存する場所ではありません – bansi

+0

ありがとうございます。 – user6795317

+0

@ user6795317、SOはコードサービスのWebサイトではありません。あなたのやり遂げたことを親切に見せてください。私たちはあなたを助けてくれることをうれしく思います。 – Ronald

答えて

0
import sys 
import os 
import paramiko 
import time 
import datetime 
import json 

#Define Server Detail for sftp 

hostname = "hostname" 
username = "username" 
password = "password" 
serverpath = "/var/www/projectfolder" 

server, user, password, port = (hostname, username, password, 2020) 
ssh = paramiko.SSHClient() 
paramiko.util.log_to_file('paramiko.log') 
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 
ssh.load_host_keys(os.path.expanduser(os.path.join("~", ".ssh", "known_hosts"))) 
ssh.connect(server, username=user, password=password, port=port) 
ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command('ls /tmp') 

error = ssh_stderr.read() 

# print "err", (error), len(error) 

sftp = ssh.open_sftp() 

# data = sftp.listdir() 

data = sftp.listdir(serverpath) 

dir_paths = [] 

# for i in data: 
#  dir_paths.append(i) 
#  print (i) 

for line in data: 
    if line.endswith('.mp4'): 
     print(line) 

#The local path where files to be downloded 
#local_path = "/root/backup/" 

#The Remote path of the Server 
#sftp.chdir(remote_path) 
#remote_path = '/root/backup/' 

#print "changed" 

# for filename in (sftp.listdir()): 
# 
# fullpath = os.path.join(remote_path, filename) 
# # get timestamp of file in epoch secon 
# timestamp = sftp.stat(fullpath).st_atime 
# createtime = datetime.datetime.now() 
# now = time.mktime(createtime.timetuple()) 
# datetime.timedelta = now - timestamp 
# 
# #Download the latest file 
# if datetime.timedelta < 86400: 
#  print filename 
# 
#  localpath = os.path.join(local_path, filename) 
#  print "downloading %s" % filename 
#  sftp.get(filename, localpath) 
#  print "downloading %s completed" % filename 

sftp.close() 
ssh.close() 


So this is my code and i want to execute it from php script. 
関連する問題