Sie sind auf Seite 1von 2

Web

Search

LinuxBSDCentral.com

Home

MainMenu

SOFTLAYER

Home
LinuxArticles
FreeBSDArticles
ApacheArticles
PerlArticles
OtherArticles
ProgramDownloads
FreeBooks
News
TheWebLinks
ContactUs

CLOUD

A Private Cloud Environment That Is Truly Transparent & Secure.

AutomatingSFTPusingexpect

ContributedbyChadBrandt

Saturday,19June2004

YoumayneedtoautomatetransferringoffilesandtheonlyprotocolyouhaveavailabletoyouisSSH.Youcanaccomplish
thisusingexpectandSFTP

MostRead

Expect(http://expect.nist.gov/)isatoolforautomatinginteractiveprograms.Touseexpecttoautomatefiletransferwith
SFTP,wehavetofirstcreatetheexpectscript

AutomatingSFTP
usingexpect

#!/usr/local/bin/expect

FreeBSDPPTPVPN
SnortShorwall
UsingSnortAnd
ShorewallTogether
FreeBSDautomatic
updates

spawnsftpbcmdFileuser@yourserver.com
expect"password:"
send"shhh!\n"
interact
Thiswillspawnsftpinbatchmodeandpassinthepasswordshhh!totheprogram.SFTPwillthanexecuteallthecommadsin
cmdFile

ShorewallRouter
onLinux

cmdFile
lcd/home/ftp/test
cd/home/ftp/somedir
mput*.dat

Polls
FavoriteLinux/BSD
Fedora

lcd/home/recieving
cd/home/someotherdir
mget*.dat

Mandrake
Debian
Slackware
Gentoo

AnotheralternativewouldbetousePerltoautomatetheSCP.Youcoulddothiswiththefollowingexample

Suse

#!/usr/bin/perlw

FreeBSD
Other

useNet::SFTP
usestrict

Vote Results

my$host="host.ssh.com"
my%args=(
user=>'your_user_name,
password=>'your_password',
debug=>'true'
)

Syndicate

Latestnewsdirectto
yourdesktop

LoginForm

my$sftp=Net::SFTP>new($host,%args)
$sftp>get("/home/user/something.txt","/home/user/hey.txt")
$sftp>put("bar","baz")

Username
Password

Comments
Brillant!

Rememberme
Login

WrittenbyGueston2004081810:54:25

Forgottenyour
password?

thx!

MembersOnline
Members
(46451)

Nice...BUT???
#
Online

Guests

Users

OnlineUsers

WrittenbyGueston2004100714:22:05

Ihavefollowedtheexampleshere,butstillcannotgetthepasswordtoenter...triedexpectasnotedaboveandthefull
stringreturnedtothescriptandneitherworks...anyideas??
WrittenbyGueston2004100807:41:30

NoUsersOnline

didyouincludethenewlinecharacter\nattheendofthepassword

Statistics

sftp/expectexceptionconditions

OS:Linuxj
PHP:5.2.17
MySQL:5.5.4237.1log
Time:06:10
Members:46451
Hits:2921715
News:281
WebLinks:15

WrittenbyGueston2004102012:56:02

Ihaveaworkingexpectscriptforsftpanditworksfineunlessthereisanunexpectedresponseorerrorcondition.Ihave
notbeenabletodeterminehowtohandlethemanyexceptionconditionsthatmayoccur.Isthereagoodsourceforthis
typeofinformation?
Super!

WrittenbyGueston2004112814:43:28

itworksfine!
Idon'tgetpasswordprompt

WrittenbyGueston2005041520:50:39

WhenIrunsftpwithboptionitdoesnotseemtoallowinteractivepasswordentryatallsoexpectcannothelpme.Isthere
aworkaroundforthistoforceittopromptforthepasswordwheninbatchmode?
WrittenbyGueston2005072012:15:32

unixshellscriptforsftp...

WrittenbyGueston2005082602:20:56

Itsworkingfineinlinux...butwhenitriedtoexecuteinunix..Iamunabletofind"send,spawn,expect"utilityfiles...Isthere
anyotheralternativetosuppressinteractivepasswordduringtheexecutionofscript?...helpmeoutin
unixshellscript...
Installexpectonunix

WrittenbyGueston2005082607:10:31

expectisnotinstalledbydefault.Downloadtheprogramandinstallitontheunixmachine
WrittenbyGueston2008123000:44:09

Ibelieveitsnotworking
expectdoesnottranferentirefile
WrittenbyGueston2009030413:42:40

expectscriptdoesnottranferentirefile...hasanyonecomeaccrossthissituation
filenotfound

WrittenbyGueston2009030701:42:40

Whatiffileisnotfoundonthefileserverandwegeta"Filenotfound"message.howcanwehandlethissituation?
Paul

WrittenbyGueston2009092817:37:14

thanksforthistipIwasbeginningtogetfrustratedbecauseIcouldnotfindaneasyPHPwaytoretrievethefilesIneeded.
Thisinacronjobdoesmiracles!
Nicethanks.

WrittenbyGueston2009112318:43:48

CouldnotgetperlversionworkingduetomissingMath: ari(whichwastoocomplicatedformetofigureoutwithmy
limitedperlexperience).

expecthoweverwasalreadyinstalledonmyservers(AIX)andworkedwell.

Laterversionsofsftpdon'tdopasswordpromptingwhenbflagisgiven,hencethereasonIwassearchingforasolution.
Instead,getridofthebatchfileandsendthecommandsviaexpect/sendinstead.

spawnsftpuser@host
expect"password:"
send"nottelling\n"
expect"sftp>"
send"lcd/xxx\n"
expect"sftp>"
send"cd/yyy\n"
expect"sftp>"
send"ls\n"
expect"sftp>"
send"mget*\n"
expect"sftp>"
send"quit\n"
send_user"\ndone.\n"
Thankyou

WrittenbyGueston2010021819:16:02

Thenewconnectionwiththebelowdoesnotworkandperldoesnotcomplain.

However,stuffinginto%argsasdescribedaboveworkedfine.Thanks

my$sftp=Net::SFTP>new($sftpHost,$sftpUser,$sftpPass)orprint"Cannotconnectto$sftpHost:$@"
AQuestion

WrittenbyGueston2010032610:32:37

Ifweexecutethisscriptsimultaneouslybytwousersinthesameserver,erroriscoming.whattodo
hello

WrittenbyGueston2010032610:33:55

canigetasolution
worksnow!

WrittenbyGueston2010042804:58:44

Cheersguysveryusefulinfo.
Igotanissuewheremybatchfilewouldn'tgetprocessedifcalledfromascheduledjobalthoughrunningfromcommand
promptdidthetrick!chuckingthebatchfilerouteandusingindividualcommandsworkinglikeatreat!
haldletheretruncodes

WrittenbyGueston2010052004:09:03

insftphowtohandletheerrorcodes
Notabletoinstallsupprtmodules..plz
WrittenbyGueston2010070609:49:54

SFTPpbmiskeeponaskingmetoinstallsomemodules..likecrypt.butwasnotinstallingproperly.plzletmeknowwhat
arethesupportmodulesIneedtohavforSFTPandtheirlinksifpossible

Thankyou
Sri
Workswell

WrittenbyGueston2010072708:07:41

Trieditanditseemstoworkwell,thanks!FoundthisarticlefromGoogle
ThanksChad

WrittenbyGueston2010072809:11:58

Wowaftermonthsofonandoffsearching,yourexpectscriptworksbeautifully!!Iwastryinglftpetc..andcouldn'tgetitto
work.Thanksman!
Onlyregistereduserscanwritecomments.
Pleaseloginorregister.
PoweredbyAkoComment1.0beta2!

Das könnte Ihnen auch gefallen