#!/usr/local/bin/expect -f

expect_version -exit 4.8

set host [lindex $argv 0]
set userid [lindex $argv 1]
set passwd [lindex $argv 1]
set timeout 30
	spawn ftp 
		expect "ftp>"
		send "verbose off\r"
		expect "Verbose*ftp>"
		send "open $host\r" 
		expect {
			timeout  
				{
				send_user "\nERROR:Unable to connect to $host\n"
			 	exit 1
				}
			"Name*:"   
			}
		send "$userid\r"
		expect {
			timeout  
				{
				send_user "\nERROR:Timeout on login id\n"
			 	exit 1
				}
			"Password:"
			}
		send "$passwd\r"
		expect {
			timeout  
				{
				send_user "\nERROR:Timeout on password\n"
			 	exit 1
				}
		"ftp>"
		}
		send "verbose on\r"
		expect "Verbose*ftp>"
		set timeout 60
		send "dir\r"
		expect {
			timeout
				{
				send_user "\nERROR:Timeout on file transfer\n"
				exit 1
				}
			"\n450 *ftp>"
				{
				send_user "\nERROR:File transfer failed\n"
				exit 1
				}
			"*bytes received*ftp>" 
			}
		send "quit\r"
	close
wait
