简体   繁体   中英

PHP excute Python SFTP script

in PHP I need to do some SFTP, but I am having issues because I am not allowed to install the SSH extension, and phpseclib is not working how I need it to.

As such, I am going to execute a Python script to do the SFTP for me. What I imaging is doing something like the following

exec("SFTPUpload.py remoteFile serverLocation");

So I execute SFTPUpload.py passing it the location of the file on my server which needs transferring, and the location on the server it needs transferring too.

In terms of the Python script (I am not too familiar with Python), I imagine it would be something like the following

username='sftpUser'
password='sftpPassword'
port=22


#SFTP
client.load_system_host_keys()
print " hostname =%s \n username=%s \n password=%s \n" (hostname,username,password)
t = paramiko.Transport((hostname, port)) 
t.connect(username=username,password=password)
sftp = paramiko.SFTPClient.from_transport(t)
sftp.put(source,destination)
sftp.close()
t.close()

However, the way I am calling it from PHP, I need the Python to be in a class or something so I can pass it the variables.

How would I achieve something like this?

Thanks

I believe you can do it with the exec() function as you described by simply parsing the command line parameters in Python.

Like:

import sys

print 'Number of arguments:', len(sys.argv), 'arguments.'
print 'Argument List:', str(sys.argv)

Where you can access the elements in sys.argv like a list.

(You could also take a look at the getopt module which offers even more (C-like) parameter passing options, but I believe the above solution will do.)

If the exec() function does not work, I believe you could consider to use the system function in PHP:

Something like this:

$mystring = system('SFTPUpload.py remoteFile serverLocation', $retval);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM