- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- expect script for sftp file transfer.
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-24-2009 03:00 AM
тАО07-24-2009 03:00 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-24-2009 05:42 AM
тАО07-24-2009 05:42 AM
Re: expect script for sftp file transfer.
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-24-2009 06:39 AM
тАО07-24-2009 06:39 AM
Re: expect script for sftp file transfer.
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"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-24-2009 07:28 AM
тАО07-24-2009 07:28 AM
Solution#!/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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-26-2009 09:42 PM
тАО07-26-2009 09:42 PM