Automated FTP in Unix
#!/bin/sh ### Sample Script and Method for FTP files from one ### Machine/Host to another Machine. ### ### If you want to ftp few file from ### Host-A (location /b002/org_files/) ### to Host-B (location /u001/b_loc), ### here is the script and methods. ### ### 1) Login into Host-B ### 2) cd /u001/b_loc ### 3) Then run the following FTP scripts as follows ### # auto_ftp.ksh > auto_ftp.lst & ### ### ftp -n host-A.com << !EOF user unix-username password cd /b002/org_files prompt bin mget filename1 mget filename2 mget filename3 quit !EOF
Script 2
#!/usr/local/expect -f ############################################################################### # File Name : ftp2host.exp # # # Purpose : This script automates the ftp telnet interface # ############################################################################### # 1. Set the ftp prompt using a regular expression. set ftp_prompt "ftp>?" set hostname [lindex $argv 0] set username mbt set password mbt set ftp_prompt ftp set option bin set fname [lindex $argv 1] ## procedure for check for proper agrguments proc testArgs {} { global hostname global fname if {$hostname == "" || $fname == ""} { puts "\n#**************Invalid no of argumeters!!************#\n" puts " usage : expect ftp2host.exp <hostIP> <currentDirFile> " puts "\n Note: given file(arg2) should be in current directory" puts "\n#****************************************************#\n" exit 0 } } ## main program stats here starts with calling procedure testArgs {testArgs} # Connect to the FTP server using the "spawn" command. spawn ftp $hostname # Wait for a login prompt. expect -re "(Name|login|Login|Username).*:.*" { # Login prompt received. Send user name to Unix server. exp_send "$username\r" } eof { # No login prompt received. Display an error. exp_send_user "could not connect\n" } # Wait for a password prompt from the Unix server. expect "Password:" { # Password prompt received. Send the password. exp_send "$password\r" } # Wait for an FTP prompt. Enter FTP commands. expect -re $ftp_prompt { # Change to the upload directory on the Unix server. exp_send "$option\r" } expect -re $ftp_prompt { # Upload the file to the Unix server. exp_send "put $fname\r" } expect -re "sent" { # Close the FTP connection to the Unix server. exp_send "bye\r" puts "\n ********$fname Transferred Successfully********" }