{"id":412,"date":"2009-08-06T14:31:17","date_gmt":"2009-08-06T19:31:17","guid":{"rendered":"http:\/\/www.gamescheat.ca\/?p=412"},"modified":"2019-09-04T19:57:40","modified_gmt":"2019-09-05T00:57:40","slug":"how-to-automate-ftp-login-and-file-transfer","status":"publish","type":"post","link":"http:\/\/www.gamescheat.ca\/?p=412","title":{"rendered":"How to automate ftp login and file transfer"},"content":{"rendered":"<p>As a Linux\/Unix server administrator I ran into an occasion where I had to automate the file transfer via ftp to a remote server.  I had to create a shell script to automatically login to the remote ftp server and then upload the file.  I then schedule this script to run daily through a cron job.<\/p>\n<p>Here is the script to automate the ftp login process and upload the file. Save the file as autoftp.<\/p>\n<p><strong>#! \/usr\/bin\/ksh<br \/>\n<em># The above line is required to define the file as a Korn shell script<\/em><\/p>\n<p><em># go to directory where the file you want to upload is<\/em>cd \/directory\/containing\/file\/to\/upload<\/p>\n<p><em># Define the variables<\/em><br \/>\nHOST=remote.ftp.server.name<br \/>\nUSERID=andrew_lin<br \/>\nPASSWORD=can\u2019t_tell_u<\/p>\n<p>exec 4>&1<br \/>\nftp -nv >&4 2>&4 |&<\/p>\n<p>print -p open $HOST<br \/>\nprint -p user $USERID $PASSWORD<br \/>\nprint -p binary<br \/>\n<em># Upload the file myfile.txt<\/em><br \/>\nprint -p put myfile.txt<br \/>\nprint -p bye<\/strong><\/p>\n<div style=\"float: left;\"><div style=\"margin: 15px 15px 15px 15px\";><script type=\"text\/javascript\"><!--\ngoogle_ad_client = \"pub-3319935785736004\";\ngoogle_alternate_color = \"FFFFFF\";\ngoogle_ad_width = 300;\ngoogle_ad_height = 250;\ngoogle_ad_format = \"300x250_as\";\ngoogle_ad_type = \"text_image\";\ngoogle_ad_channel =\"\";\ngoogle_color_border = \"cccccc\";\ngoogle_color_link = \"cc0000\";\ngoogle_color_bg = \"ffffff\";\ngoogle_color_text = \"000000\";\ngoogle_color_url = \"008000\";\n\/\/--><\/script>\n<script type=\"text\/javascript\"\n  src=\"http:\/\/pagead2.googlesyndication.com\/pagead\/show_ads.js\">\n<\/script><\/div><\/div><p>To schedule the autoftp script to run through a cron job, create a file called ftp_cron. The below is the contents of ftp_cron.<\/p>\n<p><strong>MAILTO=andrew_lin@andrew-lin.com<br \/>\n22 15 * * * ksh \/mydir\/autoftp<\/strong><\/p>\n<p>The MAILTO command will email the logs to andrew_lin@andrew_lin.com.  If you wanted to email more then one recipient then separate multiple email addresses with a comma, e.g. MAILTO=andrew@andrew.com,andrew_lin@andrew_lin.com.  The job is schedule to run daily at 3:22 p.m. (15:22). Preceding the script with ksh means that the script will execute in a separate Kron shell.<\/p>\n<p>Now issue the below command to schedule the corn job.<\/p>\n<p><strong>crontab ftp_cron<\/strong><\/p>\n<p>To confirm if the jobs has been scheduled enter the below command.<\/p>\n<p><strong>crontab -l<\/strong><\/p>\n<p>If you want to make and changes to the schedule simply edit the ftp_cron file and run crontab ftp_cron again.<\/p>\n<p>Here is link to the bash version of the script, <a href=\"http:\/\/www.gamescheat.ca\/2009\/08\/27\/how-to-automate-ftp-login-and-file-transfer-with-bash-script\/\">http:\/\/www.gamescheat.ca\/2009\/08\/27\/how-to-automate-ftp-login-and-file-transfer-with-bash-script\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>As a Linux\/Unix server administrator I ran into an occasion where I had to automate the file transfer via ftp to a remote server. I had to create a shell script to automatically login to the remote ftp server and&hellip;<\/p>\n<p class=\"more-link-p\"><a class=\"more-link\" href=\"http:\/\/www.gamescheat.ca\/?p=412\">Read more &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[19],"tags":[],"_links":{"self":[{"href":"http:\/\/www.gamescheat.ca\/index.php?rest_route=\/wp\/v2\/posts\/412"}],"collection":[{"href":"http:\/\/www.gamescheat.ca\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.gamescheat.ca\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.gamescheat.ca\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.gamescheat.ca\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=412"}],"version-history":[{"count":7,"href":"http:\/\/www.gamescheat.ca\/index.php?rest_route=\/wp\/v2\/posts\/412\/revisions"}],"predecessor-version":[{"id":993,"href":"http:\/\/www.gamescheat.ca\/index.php?rest_route=\/wp\/v2\/posts\/412\/revisions\/993"}],"wp:attachment":[{"href":"http:\/\/www.gamescheat.ca\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=412"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.gamescheat.ca\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=412"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.gamescheat.ca\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=412"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}