Operating System - HP-UX
1753543 Members
5742 Online
108795 Solutions
New Discussion юеВ

Re: expect script for sftp file transfer.

 
SOLVED
Go to solution
Narendra Uttekar
Regular Advisor

expect script for sftp file transfer.

Hi,
I want to write a expect script for sftp file transfer.
I am able to connect the sftp server through expect script. But i don't know how to put the logic to send an e-mail for me when the script runs and how many files send and also if there is problem connecting sftp server should send me an e-mail. I am already having the shell script for sftp but without password.

cd /ABC/EFC/DF/TEST
file=$(ls)
for x in $file
do
mv $x $x.txt
chown aaaa:bbbb $x
chmod 666 $x
done
/usr/bin/sftp -b /abc/script/put xxxxxxxx@aaaaa.ssss.co
RC=$?
echo " "
echo "sftp return code is : $RC"
if test $RC -eq 255
then
echo "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
echo "There is some problem connecting to the sftp server so exiting the script here"
echo "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
exit
elif test $RC -eq 1
then
echo "Looks like there are no files to transfer to the sftp server"
elif test $RC -eq 0
then
echo "sftp ran OK"
else
echo "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
echo "There was some unknown error occurred during sftp so exiting the script here"
echo "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
exit
fi

Datestamp=$(date +%Y%m%d%H%M)

file=$(ls)
N=0
for x in $file
do
(( N = N + 1 ))
echo "Processing the file: $x"
mv $x ../old/$x.$Datestamp
done
echo "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
echo "Number of files transferred to the ftp server are: $N"
echo "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"

And in script /abc/script/put
mput *.txt
quit

Thanks,
Narendra
4 REPLIES 4
Steven Schweda
Honored Contributor

Re: expect script for sftp file transfer.

> I want to write a expect script [...]

I don't see "expect" anywhere in your script.
(Which is good, but confusing.)

> But i don't know how to put the logic to
> send an e-mail [...]

I'm confused. Are you asking (again) how to
send e-mail from a script? A Forum search
for keywords like:
script send e-mail
should find many examples, including, for
example:

http://forums.itrc.hp.com/service/forums/questionanswer.do?threadId=1351442
jerry1
Super Advisor

Re: expect script for sftp file transfer.

You might want to use Perl with Net::SFTP.
You can then trap errors in that.
Otherwise you can try using:

In Expect you should be able to use
something like this:

expect {
"some expected string" {
exec echo "error" | mailx -s "error"
}
{

But I too do not see any expect code.
Looks like you are just running a shell
script. In that case just run mailx after
you determine $RC.

echo "string" | mailx -s "Subject"


rmueller58
Valued Contributor
Solution

Re: expect script for sftp file transfer.

Try this..

#!/usr/bin/expect -f

# Customizable things start here

set site "site.domain.tld"
set user "useraccountname"
set password "userpassword"

# Timeout, in seconds, when first making the connection
set inittimeout "15"

# Timeout, in seconds, when actually uploading the file
set conntimeout "60"

# Location of the nonstandard scp(1) program
set scpprog "scp"

# Customizable things end here
# ==============================

# Leave the remote directory as is...
set remotefile "/incoming"

# Create an incoming directory on your local server. "mkdir /esu3-fs/blr/school_fusion/incoming"
# Insert or upload all *.csv files in "/esu3-fs/blr/school_fusion/incoming"

set localfile1 "/localfilelocation/filename.csv"

send_user "Uploading $localfile1 to $user@$site:$remotefile ....\n\n"

# launch SCP
set result [spawn $scpprog $localfile1 $user@$site:$remotefile]
if { $result == 0 } {
send_user "\nSCP program $scpprog failed to start!\n"
exit 1
}
sleep 1

set timeout $inittimeout
set isgood 0

# nudge SCP along, answering "yes" and giving password
set needrepeat 1
while { $needrepeat != 0 } {
set needrepeat 0
expect {
"assword" {
sleep 1
send "$password\r"
set isgood 1
}
"re you sure you want" {
sleep 1
send "yes\r"
set needrepeat 1
}
timeout {
send_user "\nSCP program $scpprog failed to connect!\n"
exit 1
}
}
}

if { $isgood != 1 } {
send_user "\nSCP failed to establish connection!\n";
exit 1
}

set timeout $conntimeout
set isgood 0

# wait for SCP to complete transferring the file
expect {
"ermission denied" {
send_user "\nSCP did not like that password!\n"
exit 1
}
"100%" {
set isgood 1
send_user "\nDone!\n"
exit 1
}
timeout {
send_user "\nSCP failed to work after giving password!\n"
exit 1
}
}

if { $isgood != 1 } {
send_user "\nSCP failed during upload of the file!\n"
exit 1
}

# see if it worked
set resultlist [wait -i $spawn_id]
set resultstatus [lindex $resultlist 2]
set resultcode [lindex $resultlist 3]
if { $resultstatus != 0 } {
send_user "\nSystem error $resultcode while running SCP program!\n";
exit 1
}
if { $resultcode != 0 } {
send_user "\nError $resultcode returned by SCP program!\n";
exit 1
}

exit 0
Narendra Uttekar
Regular Advisor

Re: expect script for sftp file transfer.

Thanks for the solution.