PHP excute Python SFTP script
Question: 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


Answer: 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](https://pymotw.com/2/getopt/) 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](http://php.net/manual/en/function.system.php) in PHP:

Something like this:

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

