16
$\begingroup$

I have MMA-generated html and would like to automate uploading to a named directory to a web host via SFTP or HTTP POST, ideally without creating local temp files. The closest question I could find on MMA.SE is this question which doesn't really address this issue.

Based on this "minimal http upload gci" in Python, and HTTP Request-Authentication flow-chart, it seems that uploading by FTP may be relatively easier. Sjoerd C. de Vries suggested this question may answer the POST part.

Others have found FTP in non-interactive mode to be difficult. But one would think authentication, put and closing the session in interactive mode could be managed within MMA.

However, Run is not meant to capture output from the external program. From the sparse documentation, I think RunThrough is meant to capture external program output, but I can only manage to capture an exit condition code:

RunThrough["sftp", "####@understars.org:temp.understars.org"]

Doesn't output to the front end, but does set the Out[] to 256.

So how can an interactive FTP session be automated?

(Note I've tagged this "html" as well since ultimately that is what I want to export.)

$\endgroup$
6
  • 1
    $\begingroup$ Have you seen this question: mathematica.stackexchange.com/questions/3097/… ? $\endgroup$ Commented Aug 29, 2012 at 21:04
  • $\begingroup$ @SjoerdC.deVries, thanks, no I had not seen that. But reading it I am confused as to how to apply the answers to my situation. Do you recommend I edit my Q to just focus on FTP? $\endgroup$ Commented Aug 29, 2012 at 21:11
  • $\begingroup$ It seems that the POST part of your question has been handled. Since I haven't worked with the solutions intensively I don't know whether they're sufficient for your case. If they are, I suggest removing the POST part from your question. $\endgroup$ Commented Aug 29, 2012 at 21:15
  • $\begingroup$ sftp wants its destination as command line argument, not as standard input. Maybe you'll have more success with RunThrough["sftp ####@understars.org:temp.understars.org","commands to execute"] $\endgroup$
    – celtschk
    Commented Aug 31, 2012 at 12:22
  • 1
    $\begingroup$ The title should be changed to say "SFTP" instead of "FTP". $\endgroup$
    – Joel Klein
    Commented Feb 4, 2013 at 16:35

1 Answer 1

15
$\begingroup$

Using a slightly modified version of vngx-jsch (source included), an open-source implementation of jsch, and JLink and a small but efficient Mathematica package this is now easily possible. All code can be browsed here, and most simply be installed by executing this twitterable line: (tested on Windows, Linux and Mac, but not on the Raspberry Pi). It should all work on Mathematica 7, 8, 9 and 10 (though I did not test the new version on Mathematica 7 and 8).

Get @ "http://goo.gl/y4pCZ8"

This loads from Github my updated MathematicaPackageInstall function in order to automatically install the package from Github to your local $UserBaseDirectory/Applications folder. At the end of the installation process (give it half a minute) a notebook MSFTP.nb opens with some examples in it.

If you have set up your sftp (ssh) server (any Linux box will do) and opened port 22 on your firewall you can do things like

MSFTPPut[NotebookDirectory[], "test", "Password"->"secret",
  "UserName" -> "testuser", "HostName" -> "testserver.example.com"]]

There is also a MSFTPGet function for getting files and directories.

As a goodie I added PassEncode, so you can encode your password on your $MachineID, i.e., on my machine

PassEncode["secret"]

gives

   {40, 42, 33, 49, 72, 33, 42, 41, 57, 50, 125, 10, 64, 120, 122, 106, \
124, 63, 34, 77, 113, 55, 94, 57, 113, 110, 89, 70}

which can be used insted of "secret" in MSFTPPut or MSFTPGet. So the advantage is you don't have to save clear-text passwords in your notebook, but some $MachineID dependent encryption of it.

$\endgroup$
3
  • $\begingroup$ Tks Rolf!.. Tested and approved. I'll start to use it in some automatic file exchanges! $\endgroup$
    – Murta
    Commented Apr 11, 2013 at 0:54
  • $\begingroup$ as of 2020, this doesn’t seem to work in OS X. I’m getting "A Java exception occurred: "org.vngx.jsch.kex.KexException: Failed to run KexAlgorithm “ $\endgroup$ Commented Jun 3, 2020 at 13:26
  • $\begingroup$ In 2020 CopyFile should work, even on macOS, or? $\endgroup$ Commented Jun 3, 2020 at 14:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.