0% found this document useful (0 votes)
238 views

Automate SFTP Expect Script

The document describes an Expect script that automates the SFTP process of downloading and uploading files between two servers. It explains how to install the necessary Expect and Tcl packages on the SUN server. It then provides the Expect script code which logs into the source server, downloads files modified on the current date, logs into the destination server, and uploads the files. It also describes how to modify the script to get files from the previous date.

Uploaded by

KARTHIK145
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
238 views

Automate SFTP Expect Script

The document describes an Expect script that automates the SFTP process of downloading and uploading files between two servers. It explains how to install the necessary Expect and Tcl packages on the SUN server. It then provides the Expect script code which logs into the source server, downloads files modified on the current date, logs into the destination server, and uploads the files. It also describes how to modify the script to get files from the previous date.

Uploaded by

KARTHIK145
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

0

More NextBlog

CreateBlog SignIn

JibbyGeorgeUNIXandVirtualizationBlog
TUESDAY,JANUARY26,2010

Automatingsftpwithexpectscript
Igotarequesttoautomatethesftpprocess.Ifinditdifficultwithshellscript.Ifindaneasywaytodowith
expectscript.

ThefollowingpackagesneedtobeinstalledontheSUNserverfortheexpectscript.Idownloadedthe
x86packagesfromsunfreewareasI'mrunningthescriptfromanx86server.
tcl8.5.3sol10x86local
libgcc3.4.6sol10x86local
expect5.43.0sol10x86local

1)pkgadddtcl8.5.3sol10x86local
2)pkgadddlibgcc3.4.6sol10x86local
Thefollowingpackagesareavailable:
1SMClgcc346libgcc
(x86)3.4.6
Installationofwassuccessful.
3)pkgadddexpect5.43.0sol10x86local

Thefollowingpackagesareavailable:
1SMCexpectexpect
(x86)5.43.0
Installationofwassuccessful.
Theexpectwillinstalledinthe/usr/local/bindirectory.

ABOUTJIBBY

JibbyGeorge
9+yearsexperienceworkingin
complexinfrastructure
environments,workedwitha
varietyoftechnologieslike
NetApp,CiscoUCS,Cisco
Nexus5k,VMwarevSphere,
Sun/OracleSolaris,Sparc
hardware's,RedhatLinuxand
virtualizationwithSolaris
(OVM/Ldom'sand
Zones/containers)CurrentlyI'm
workingasaStorageand
VirtualisationConsultantin
Sydney,Australia.
Viewmycompleteprofile

ViewJibbyGeorge's
profileonLinkedIn
BLOGARCHIVE

2013(2)
2012(3)
2011(2)
2010(16)
November(2)
September(2)
August(3)
July(2)
June(3)
March(3)

Thescriptisasfollows

#!/usr/local/bin/expectf
#Thisistheexpectscriptwrotetoautomatethesftpprocesstopullfilesfromaserverasperthedateand
#thenpushthefilestoanotherserver.
settimeout1
setDATE[execdate"+%Y%m%d"]
log_file"/home/ftp/logs/sftp.log"
send_log"####StartingSFTPscript[execdate]\n"
send_log"Today'sdate:[execdate]\n"
send_log"Downloadingfilesthroughsftp\n"
spawn/usr/bin/[email protected]
expect"Password:"
#sleep5
send"jim123\n"
expect"sftp>"
send"cd/export/home/jim\r"
expect"sftp>"
send"ls\r"

January(1)
Automatingsftpwith
expectscript
2009(5)

SEARCHTHISBLOG

Search

FOLLOWERS
Jointhissite
withGoogleFriendConnect

Members(5)

Alreadyamember?Signin

expect"sftp>"
send"get*.$DATE\r"
expect"sftp>"
send"quit\r"
sleep5
send_log"Today'sdate:[execdate]\n"
send_log"Uploadingfilesthroughsftp\n"
spawn/usr/bin/[email protected]
expect"Password:"
send"jim123\n"
expect"sftp>"
send"cd/export/home/jim\r"
expect"sftp>"
send"put*.$DATE\r"
expect"sftp>"
send"quit\r"
#ENDofSCRIPT

Icanshellscriptfromexpectscript.Ihaveanotherrequirementtopullyesterdaysfilethroughsftpserver.
ThankgodthatwasaLinuxserverandtogetyesterdaysdateinLinuxiseasy:)
setDATE[exec/home/ftp/date.sh]
Theabovelinewillcallthedate.shscripttogetyesterdaysdate.
date.shscriptisasbelow
[root@]#catdate.sh
#!/bin/bash
echo$(datedate='1dayago'"+%Y%m%d")
#Endofscript

PostedbyJibbyGeorgeat8:11AM

11comments:
Deven July19,2011at1:12PM
HiJibbyGeorge,
Thanksalotforpostingthis.
Iwasreallyfedupgooglingthis,butfinallysticktoyoursiteandworksperfectly.
ThankyouMan.youaresospecial.youmadeitsoeasy.
ThankYou
Reply

JibbyGeorge

August25,2011at11:09AM

Galdthatithelpsyou
Reply

Anonymous September29,2011at1:44AM

HEYTHERE..

LiveTrafficFeed
AvisitorfromChennai,Tamil
Naduviewed"JibbyGeorge
UNIXandVirtualizationBlog:
Automatingsftpwithexpectscript"
AvisitorfromIndianapolis,
12minsago
Indianaviewed"JibbyGeorge
UNIXandVirtualizationBlog:
Automatingsftpwithexpectscript"
AvisitorfromEuropeviewed
35minsago
"JibbyGeorgeUNIXand
VirtualizationBlog:Nexus5020
VPCconfigurationwithNetapp
AvisitorfromMelbourne,Victoria
LACPVIF"1hr38minsago
viewed"JibbyGeorgeUNIXand
VirtualizationBlog:Enabling
JumboFramesonbgeinterface
V240/M4000andNetappFiler"2
AvisitorfromIndiaviewed"Jibby
hrs52minsago
GeorgeUNIXandVirtualization
Blog:Automatingsftpwithexpect
script"3hrs24minsago
AvisitorfromRishonLeZion,
HaMerkazviewed"JibbyGeorge
UNIXandVirtualizationBlog"3
hrs54minsago
AvisitorfromBangalore,
Karnatakaviewed"JibbyGeorge
UNIXandVirtualizationBlog:
CiscoUCSEnableTACACS+
AvisitorfromIstanbulviewed
authentication."5hrs39minsago
"JibbyGeorgeUNIXand
VirtualizationBlog:Enabling
Jumboframesonnxgeinterfaceor
AvisitorfromSeocho,Seoul
onT5140server"6hrs52minsago
tukpyolsiviewed"JibbyGeorge
UNIXandVirtualizationBlog:
Automatingsftpwithexpectscript"
AvisitorfromDallas,Texas
9hrs38minsago
viewed"JibbyGeorgeUNIXand
VirtualizationBlog:Automating
sftpwithexpectscript"12hrs5
RealtimeviewGetFeedjit

HiJibby,
I have a similar requirement. Your method of doing that is impressive. I tried to follow this. But, the
glitchhereisthatthesftpusernamehasaspecialcharacter$initsname.Whilerunningthescript
thespawn/expect/sendlibrariesaretreatingthetextafter$symbolasavariable.Isthereawaythat
youthinkoftogetawaywiththat.Itriedsinglequotes,doublequotesaroundtheusernamebutno
luck.
Thanksforyourhelp.
Naresh
Reply

JibbyGeorge

September29,2011at2:22AM

HINaresh,thanksforthat.ItsbeenawhileIworkedwithexpect.Thereshouldbeawaytodothat.
IfIfigureoutsomethingwillposthere.
Jibby
Reply

Anonymous September29,2011at2:26AM
Great!ThanksmuchforyourhelpJibby.
Naresh
Reply

Anonymous September29,2011at9:23PM
HiJibby,foundasolutiontogetawaywiththe$symbolusingexpect.WhenIplacethe\(backward
slash)infrontofthe$symbol,thenitworksfine.
Thanks!
Naresh
Reply

Anonymous December23,2011at12:52AM
HowcanIchecktoseeifthepasswordisinvalidorexpired?Ihaveasimilartasktoautomate.Nice
script.
Reply

Anonymous December23,2011at8:18PM
I was able to figure out my check for invalid or expired password. The system I was connecting to
does not tell you if the password is invalid, it just gives the password prompt over and over that is
why there is a second expect for the password prompt. I also put in a check for upper and lower
caseprompts.Afterdoingthespawnofthesftpcommanditgoeslikethis:
spawnsftpmylogonid@someserver
expectre"password|Password:"
send"mypasswd\r"
expectre"password|Password:"{
puts"Passwordwasnotaccepted\n"
exit2
}re"expired|Expired"{
puts"PasswordExpired\n"
exit3
}re"invalid|Invalid"{
puts"PasswordInvalid\n"
exit4
}"sftp>"{
send"pwd\r"
expect"sftp>"

send"putmyfilenameyourfilename\r"
expect"sftp>"
send"bye\r"
}
Reply
Replies
Joe March7,2012at12:21AM
IhaveasimplerequirementandtrytofigureouthowtodoitinEXPECT.
after sftp into a server. I want to cd into the latest directory (Note $HOME has directories
andfiles).Anyideaaeasywaytoachievethis??
Thanks
Reply

Anonymous June27,2012at4:11PM
Workedgreat!Thankyou!
Reply

Anonymous May6,2014at9:59PM
Hi
Ihaveproblemwithfiletransmission(file200MB+/50MB).
Thetrasmissionhadbeeninterruptedbeforefinisheddowloading.Restworksgood.Anyideas??
Reply

Enteryourcomment...

Commentas:

Publish

Unknown(Google)

Signout

Notifyme

Preview

Linkstothispost
CreateaLink

NewerPost

Home

OlderPost

Subscribeto:PostComments(Atom)

PictureWindowtemplate.PoweredbyBlogger.

You might also like