3
$\begingroup$

Is there any option to directly import to a Windows-driven Mathematica kernel a file from a remote server without copying it on hard drive? Right now I'm using CopyFile with RemoteFile and $SSHAuthentication. It is very inconvenient because for every remote file I have to provide a path on harddrive to copy it and then use just regular Import. It there a workaround?

Import["/server/folder", "Username" -> "username", "Password" -> "password"] from Import files from password-protected remote server doesn't work, as well as Import[RemoteFile[...]]

$\endgroup$

1 Answer 1

5
$\begingroup$

Since your know ssh, why not directly use linux commands like

 Import["!echo `ssh user@host 'cat  /home/user/file.txt'`", "String"]

You can avoid password request by simply sharing public/private key with your computer and server (see ssh-keygen and ssh-copy-id -i ~/.ssh/mykey user@host in ubuntu)

Edit 1 Other solution (not related to Mathematica) is to use sshfs command to mount remote server folder to local computer file system. Seems there are attempts to implement the functionality for windows as well. See, https://stackoverflow.com/questions/67182778/mounting-linux-fs-in-windows-10-using-sshfs for instructions. After mounting remote folder effectively becomes a local folder, so every program can access files as if they were located on local computer.

$\endgroup$
5
  • $\begingroup$ Thanks for Your answer!I'm not familiar with linux commands since unfortunately I work on windows. I'm actually using ssh key pair but this approach doesn't work - it just imports the string in quotes. maybe I have to provide somehow a path to a private key before importing? $\endgroup$
    – Lechuu
    Apr 27 at 15:42
  • $\begingroup$ First, check if you can connect to the remote server without password. You can read this hostinger.com/tutorials/how-to-use-putty-ssh . In the end they have a section how to generate public key for putty. I think in windows the example will not work anyway, because the string "!echo ...." is in fact is linux terminal command. I suspect it is possible to write something similar in powershell, but I don't know it. $\endgroup$
    – Acus
    Apr 27 at 15:57
  • $\begingroup$ Oh, I'm sorry, I'm using putty with ssh key and I can connect without password - it works great (via WinSCP works as well). I don't really know how to connet from powershell, but when I find it i will try to use it in Mathematica. Thanks! $\endgroup$
    – Lechuu
    Apr 27 at 16:22
  • 1
    $\begingroup$ @Lechuu Another solution which comes to mind is to use sshfs command to mount remote server folder to local computer file system. I use this routinely and I just checked that recently there were attempts to do this for windows also Look for example at stackoverflow.com/questions/67182778/… . After mounting remote folder it effectively becomes as a local folder, so every program can access files as if they were located on local computer. Sometimes this is really convenient. $\endgroup$
    – Acus
    Apr 28 at 7:53
  • $\begingroup$ Thank You very much! It is exactly what I was looking for! If you could just edit Your answer so everyone could know the solution I would be very greateful. $\endgroup$
    – Lechuu
    Apr 28 at 12:23

Your Answer

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

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