{"id":6856,"date":"2016-04-15T09:00:19","date_gmt":"2016-04-15T13:00:19","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=6856"},"modified":"2016-04-14T02:42:53","modified_gmt":"2016-04-14T06:42:53","slug":"secure-ftp-sshsftpscp","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2016\/04\/15\/secure-ftp-sshsftpscp\/","title":{"rendered":"Secure FTP (SSH\/SFTP\/SCP)"},"content":{"rendered":"\r\n\r\n<div class=\"content\"><p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/15007\">Jiro<\/a>'s pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/35409-ssh-sftp-scp-for-matlab--v2-\">\"SSH\/SFTP\/SCP For Matlab (v2)\"<\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/2396847\">David Freedman<\/a>.<\/p><p>Recently, we changed our internal server for our blogs, and now we are using <a href=\"https:\/\/en.wikipedia.org\/wiki\/SSH_File_Transfer_Protocol\">Secure FTP<\/a> to upload images and files. When we were using the old system (FTP), many of us used MATLAB's <a title=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/ftp-class.html (link no longer works)\"><tt>ftp<\/tt><\/a> or <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/6626-passive-mode-ftp-in-matlab\">Passive Mode FTP<\/a> (<a href=\"https:\/\/blogs.mathworks.com\/pick\/2015\/09\/04\/passive-mode-ftp\">Picked<\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/3208495\">Sean<\/a>) to automate the file transfer.<\/p><p>With the new system, I needed an SFTP-equivalence to the <tt>ftp<\/tt> function. Of course, I looked to the File Exchange! David's entry is a well-written package that interfaces to an open-source Ganymed SSH-2 Java library. The entry comes with a version of the library, but it can also be set up to download a version from the web. The setup is very simple and seamless. What I also like about this entry is that David provides simple interface commands to SSH\/SCP\/SFT, as well as advanced interface functions.<\/p><p>The simple interface works like this:<\/p><pre class=\"language-matlab\">scp_simple_put(<span class=\"string\">'sftp.thishost.com'<\/span>,<span class=\"string\">'username'<\/span>,<span class=\"string\">'password'<\/span>,<span class=\"keyword\">...<\/span>\r\n    {<span class=\"string\">'file1.m'<\/span>,<span class=\"string\">'file2.txt'<\/span>},<span class=\"string\">'\/var\/host\/user\/'<\/span>,pwd)\r\n<\/pre><p>With a single command, you specify the server, the files, and the location you want to upload to.<\/p><p>If you want more control, the advanced interface works like this:<\/p><pre class=\"language-matlab\"><span class=\"comment\">% Create a connection<\/span>\r\ns_conn = ssh2_config(<span class=\"string\">'sftp.thishost.com'<\/span>,<span class=\"string\">'username'<\/span>,<span class=\"string\">'password'<\/span>);\r\n\r\n<span class=\"comment\">% Run a command, such as \"create a folder\"<\/span>\r\nssh2_command(s_conn,<span class=\"string\">'mkdir \/var\/host\/user\/temp'<\/span>);\r\n\r\n<span class=\"comment\">% Copy files from local to remote<\/span>\r\nscp_put(s_conn,{<span class=\"string\">'file1.m'<\/span>,<span class=\"string\">'file2.txt'<\/span>},<span class=\"string\">'\/var\/host\/user\/temp'<\/span>,pwd);\r\n\r\n<span class=\"comment\">% Close the connection<\/span>\r\nssh2_close(s_conn);\r\n<\/pre><p>David has documented the functions well and also has included examples for you to try. Thanks, David, for making this transition from FTP to SFTP so simple!<\/p><p><b>Comments<\/b><\/p><p>Give this a try, and let us know what you think <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=6856#respond\">here<\/a> or leave a <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/35409-ssh-sftp-scp-for-matlab--v2-#comments\">comment<\/a> for David.<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_d93133cd23a04a8c95759bc4a53ef821() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='d93133cd23a04a8c95759bc4a53ef821 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' d93133cd23a04a8c95759bc4a53ef821';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        copyright = 'Copyright 2016 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add copyright line at the bottom if specified.\r\n        if (copyright.length > 0) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n\r\n        d.title = title + ' (MATLAB code)';\r\n        d.close();\r\n    }   \r\n     --> <\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_d93133cd23a04a8c95759bc4a53ef821()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n      the MATLAB code <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2016a<br><\/p><p class=\"footer\"><br>\r\n      Published with MATLAB&reg; R2016a<br><\/p><\/div><!--\r\nd93133cd23a04a8c95759bc4a53ef821 ##### SOURCE BEGIN #####\r\n%%\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/15007\r\n% Jiro>'s pick this week is\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/35409-ssh-sftp-scp-for-matlab--v2- \"SSH\/SFTP\/SCP\r\n% For Matlab (v2)\"> by\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/2396847 David\r\n% Freedman>.\r\n%\r\n% Recently, we changed our internal server for our blogs, and now we are\r\n% using <https:\/\/en.wikipedia.org\/wiki\/SSH_File_Transfer_Protocol Secure\r\n% FTP> to upload images and files. When we were using the old system (FTP),\r\n% many of us used MATLAB's\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/ftp-class.html |ftp|> or\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/6626-passive-mode-ftp-in-matlab Passive Mode\r\n% FTP> (<https:\/\/blogs.mathworks.com\/pick\/2015\/09\/04\/passive-mode-ftp\r\n% Picked> by\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/3208495 Sean>) to\r\n% automate the file transfer.\r\n%\r\n% With the new system, I needed an SFTP-equivalence to the |ftp| function.\r\n% Of course, I looked to the File Exchange! David's entry is a well-written\r\n% package that interfaces to an open-source Ganymed SSH-2 Java library. The\r\n% entry comes with a version of the library, but it can also be set up to\r\n% download a version from the web. The setup is very simple and seamless.\r\n% What I also like about this entry is that David provides simple interface\r\n% commands to SSH\/SCP\/SFT, as well as advanced interface functions.\r\n%\r\n% The simple interface works like this:\r\n%\r\n%   scp_simple_put('sftp.thishost.com','username','password',...\r\n%       {'file1.m','file2.txt'},'\/var\/host\/user\/',pwd)\r\n%\r\n% With a single command, you specify the server, the files, and the\r\n% location you want to upload to.\r\n%\r\n% If you want more control, the advanced interface works like this:\r\n%\r\n%   % Create a connection\r\n%   s_conn = ssh2_config('sftp.thishost.com','username','password');\r\n%   \r\n%   % Run a command, such as \"create a folder\"\r\n%   ssh2_command(s_conn,'mkdir \/var\/host\/user\/temp');\r\n%   \r\n%   % Copy files from local to remote\r\n%   scp_put(s_conn,{'file1.m','file2.txt'},'\/var\/host\/user\/temp',pwd);\r\n%\r\n%   % Close the connection\r\n%   ssh2_close(s_conn);\r\n%\r\n% David has documented the functions well and also has included examples\r\n% for you to try. Thanks, David, for making this transition from FTP to\r\n% SFTP so simple!\r\n%\r\n% *Comments*\r\n%\r\n% Give this a try, and let us know what you think\r\n% <https:\/\/blogs.mathworks.com\/pick\/?p=6856#respond here> or leave a\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/35409-ssh-sftp-scp-for-matlab--v2-#comments\r\n% comment> for David.\r\n\r\n##### SOURCE END ##### d93133cd23a04a8c95759bc4a53ef821\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n\r\nJiro's pick this week is \"SSH\/SFTP\/SCP For Matlab (v2)\" by David Freedman.Recently, we changed our internal server for our blogs, and now we are using Secure FTP to upload images and files. When... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2016\/04\/15\/secure-ftp-sshsftpscp\/\">read more >><\/a><\/p>","protected":false},"author":35,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/6856"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/35"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=6856"}],"version-history":[{"count":5,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/6856\/revisions"}],"predecessor-version":[{"id":6862,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/6856\/revisions\/6862"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=6856"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=6856"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=6856"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}