Topic
1 reply Latest Post - ‏2011-05-18T07:53:46Z by SystemAdmin
kulbhushan333
kulbhushan333
1 Post
ACCEPTED ANSWER

Pinned topic Expect script for reading files from single directory

‏2011-03-24T07:20:33Z |
Hi experts,
I have written an expect script to upload single scv file but, i want an such expect script that should read multiple csv files from single directory or that should read data from single file.
Below is my that i have written for uploading single script.

#!/usr/local/bin/expect -f #/home/kulbhushan/sftp_prog.sh
  1. procedure to attempt connecting; result 0 if OK, 1 otherwise
proc connect {passw} {
expect {
"Password:" {
send "$passw\r"
expect {
"sftp*" {
return 0
}
}
}
}
# timed out
return 1
}

#read the input parameters
set user lindex $argv 0
set passw lindex $argv 1
set host lindex $argv 2
set location lindex $argv 3
set file1 lindex $argv 4
#puts "Argument data:\n";
#puts "user: $kpatil";
#puts "passw: $patil";
#puts "host: $helios";
#puts "location: $/groups/YBL";
#puts "file1: $TaskSheet.xls";
#check if all were provided
if { $user == "" || $passw == "" || $host == "" || $location == "" || $file1 == "" } {
puts "Usage: <user> <passw> <host> <location> <file1 to send>\n"
exit 1
}

#sftp to specified host and send the files
spawn sftp $user@$host

set rez connect $passw
if { $rez == 0 } {
send "cd $location\r"
set timeout -1
send "put $file1\r"
send "quit\r"
expect eof
exit 0
}
puts "\nError connecting to server: $host, user: $user and password: $passw!\n"
exit 1
Updated on 2011-05-18T07:53:46Z at 2011-05-18T07:53:46Z by SystemAdmin
  • SystemAdmin
    SystemAdmin
    232 Posts
    ACCEPTED ANSWER

    Re: Expect script for reading files from single directory

    ‏2011-05-18T07:53:46Z  in response to kulbhushan333
    Sorry but I really have to ask, why are you using expect? There are better way to handle this like scp (password less).

    Back to your script. Can you make the filename as parameter? If you want a quick and dirty fix, you can also use mput instead of put (ftp command) which will allow you to use wildcards like "*".