#!/usr/local/bin/expect # Licensed to the Apache Software Foundation (ASF) under one # or more contributor license agreements. See the NOTICE file # distributed with this work for additional information # regarding copyright ownership. The ASF licenses this file # to you under the Apache License, Version 2.0 (the # "License"); you may not use this file except in compliance # with the License. You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. #-------------------------------------------------------------------------- # ftp.exp # # derived from: supportmode.exp # # This script downloads a file from remote ftp host. # #-------------------------------------------------------------------------- exp_version -exit 5.0 # globals set script_name "ftp.exp" set debug 0 proc usage {} { global script_name send_user "\[Usage\] $script_name \[-fh ftp-host\] \[-fd ftp-dir\] \[-fn ftp-file\] \[-fm ftp-mode\] \[-fu ftp-user\] \[-fp ftp-password\]\n" send_user " \[-h\]\n" send_user " -fh - ftp host\n" send_user " -fd - ftp directory\n" send_user " -fn - ftp file\n" send_user " -fm - ftp mode\n" send_user " -fu - ftp user id\n" send_user " -fp - ftp user password\n" send_user " -h - help (this screen)\n" exit } proc ftp {host dir name mode user password} { global debug # default 10 second timeout set timeout 10 spawn ftp $host # authenticate expect "Name*:" { } timeout { if ($debug) { send_user "\[Error\] ftp session timed-out waiting for name\n" } close return 0 } send "$user\r" expect "Password:" { } timeout { if ($debug) { send_user "\[Error\] ftp session timed-out waiting for password\n" } close return 0 } send "$password\r" expect "ftp>" { } timeout { if ($debug) { send_user "\[Error\] ftp session timed-out waiting for ftp prompt\n" } close return 0 } # change directory send "cd $dir\r" expect "250*ftp>" { } timeout { if ($debug) { send_user "\[Error\] ftp session timed-out while changing dir\n" } close return 0 } # turn interactive mode off send "prompt\r" expect "ftp>" { } timeout { if ($debug) { send_user "\[Error\] ftp session timed-out while changing interactive mode\n" } close return 0 } # set mode send "$mode\r" expect "200*ftp>" { } timeout { if ($debug) { send_user "\[Error\] ftp session timed-out while setting mode\n" } close return 0 } # set timeout to 10 min for transferring all files set timeout 600 # get the file send "get $name\r" expect "ftp>" { } timeout { if ($debug) { send_user "\[Error\] ftp session timed-out during get\n" } close return 0 } send "bye\r" close return 1 } #-------------------------------------------------------------------------- # main() begins here #-------------------------------------------------------------------------- set help 0 set mode_set 0 set ftp_host_set 0 set ftp_dir_set 0 set ftp_fn_set 0 set ftp_mode_set 0 set ftp_usr_set 0 set ftp_pw_set 0 # parse command args while {[llength $argv]>0} { set flag [lindex $argv 0] switch -- $flag "-h" { set help 1 break } "-fh" { set argv [lrange $argv 1 end] set ftp_host [lindex $argv 0] set ftp_host_set 1 set argv [lrange $argv 1 end] } "-fd" { set argv [lrange $argv 1 end] set ftp_dir [lindex $argv 0] set ftp_dir_set 1 set argv [lrange $argv 1 end] } "-fn" { set argv [lrange $argv 1 end] set ftp_fn [lindex $argv 0] set ftp_fn_set 1 set argv [lrange $argv 1 end] } "-fm" { set argv [lrange $argv 1 end] set ftp_mode [lindex $argv 0] set ftp_mode_set 1 set argv [lrange $argv 1 end] } "-fu" { set argv [lrange $argv 1 end] set ftp_usr [lindex $argv 0] set ftp_usr_set 1 set argv [lrange $argv 1 end] } "-fp" { set argv [lrange $argv 1 end] set ftp_pw [lindex $argv 0] set ftp_pw_set 1 set argv [lrange $argv 1 end] } default { break } } # do we need some help? if {$help} { usage } if {($ftp_host_set) && ($ftp_dir_set) && ($ftp_fn_set) &&($ftp_mode_set) && ($ftp_usr_set) && ($ftp_pw_set)} { ftp $ftp_host $ftp_dir $ftp_fn $ftp_mode $ftp_usr $ftp_pw }