Sie sind auf Seite 1von 4

signup

login

tour

help

stackoverflowcareers

Signup

StackOverflowisaquestionandanswersiteforprofessionalandenthusiastprogrammers.It's100%free.

HowtorunthesftpcommandwithapasswordfromBashscript?
IneedtotransferalogfiletoaremotehostusingsftpfromaLinuxhost.Ihavebeenprovidedcredentialsforthesamefrommyoperations
group.However,sinceIdon'thavecontroloverotherhost,IcannotgenerateandshareRSAkeyswiththeotherhost.
Soisthereawaytorunthe

sftp

command(withtheusername/passwordprovided)frominsidetheBashscriptthroughacronjob?

IfoundasimilarStackOverflowquestion,SpecifypasswordtosftpinaBashscript,buttherewasnosatisfactoryanswertomyproblem.
bash shell unix ssh sftp

editedNov11'14at21:36
EricLeschinski
28.8k

9Answers

Youhaveafewoptionsotherthanusingpublickeyauthentication:
1. Usekeychain
2. Usesshpass (lesssecuredbutprobablythatmeetsyourrequirement)
3. Useexpect(leastsecuredandmorecodingneeded)
Ifyoudecidetogivesshpassachancehereisaworkingscriptsnippettodoso:
exportSSHPASS=yourpasswordhere
sshpassesftpoBatchMode=nobsftpuser@remotehost<<!
cdincoming
putyourlogfile.log
bye
!

editedJun27'14at17:19
AndrewMedico
14.7k

31

answeredMar22'11at3:54
anubhava

58

294k

27

103

177

1 ForusingitonMac:stackoverflow.com/questions/9102557/ anubhava Feb27'12at14:18


1 Noneedtoexport,Ithink. SSHPASS=passwordsshpasse... shoulddo.Jens Mar28'13at13:10
5 Iwasabletodoaonelinertodowloadalogfile: sshpassp"my_password"sftpoPort=9999
user@host:dir/file.log gorus Jun18'13at20:24
2 oBatchMode=no wasthecrucialmissingpieceforme.richardkmillerJun6'14at4:58
1 Youcanadd >~/file.out2>&1 toredirecttheoutputandstderrtoafile. anubhava Sep8at13:46

Anotherwaywouldbetouselftp:
lftpsftp://user:password@hoste"putlocalfile.name;bye"

ORthecoolmirroringmethod(canincludedeleteafterconfirmedtransfer'Remove
sourcefiles'):
lftpe'mirrorR/local/log/path//remote/path/'uuser,passwordsftp.foo.com

answeredJul24'13at6:20
Karassik
253

3 +1forsuggestinganalternativebutcanweavoidsupplyingpasswordoncommandline? anubhava Jul


24'13at6:57

17

150

askedMar22'11at3:40
anubhava

147

294k

27

103

177

Expectisagreatprogramtouse.
OnUbuntuinstallitwith:
sudoaptgetinstallexpect

OnaCentOSMachineinstallitwith:
yuminstallexpect

Letssayyouwanttomakeaconnectiontoasftpserverandthenuploadalocalfilefromyour
localmachinetotheremotesftpserver
#!/usr/bin/expect
spawnsftpusername@hostname.com
expect"password:"
send"yourpasswordhere\n"
expect"sftp>"
send"cdlogdirectory\n"
expect"sftp>"
send"put/var/log/file.log\n"
expect"sftp>"
send"exit\n"
interact

Thisopensasftpconnectionwithyourpasswordtotheserver.
Thenitgoestothedirectorywhereyouwanttouploadyourfile,inthiscase"logdirectory"
Thisuploadsalogfilefromthelocaldirectoryfoundat/var/log/withthefilesnamebeingfile.logto
the"logdirectory"ontheremoteserver
editedJun27'14at17:18
AndrewMedico
14.7k

31

answeredMar28'13at13:04
rezizter

58

1,065

18

Thanksforyouranswer,Ihadexpectasoneofmyoptionsforachievingthistask. anubhava Mar28


'13at13:09
Thankyou.Ifoundyouranswerthroughasearch,useditandbuiltascriptsuccessfullywithexpect.Just
postedthescriptforpoterity rezizterMar28'13at13:11
Thankyousomuchforyouanswer,Ihavealreadyupvotedit:) anubhava Mar28'13at13:12
Thisworkedflawlesslyforme.Thankyou!ScottFlemingDec1'14at16:40

Youcanuselftpinteractivelyinashellscriptsothepasswordnotsavedin.bash_historyor
similarbydoingthefollowing:
vitest_script.sh

Addthefollowingtoyourfile:
#!/bin/sh
HOST=<yourhostname>
USER=<someusername>
PASSWD=<yourpasswd>
cd<basedirectoryforyourputfile>
lftp<<END_SCRIPT
opensftp://$HOST
user$USER$PASSWD
putlocalfile.name
bye
END_SCRIPT

Andwrite/quitthevieditorafteryoueditthehost,user,pass,anddirectoryforyourputfiletyping
:wq .Thenmakeyourscriptexecutable chmod+xtest_script.sh andexecuteit
./test_script.sh .
answeredJul29'13at22:27

MikeS.
2,484

12

20

1 Tooverwriteexistingfile,add"setxfer:clobberon"afterlftp<<END_SCRIPTBalajiNatarajanDec17'13
at17:49

Iwasrecentlyaskedtoswitchoverfromftptosftp,inordertosecurethefiletransmission
betweenservers.WeareusingTectiaSSHpackage,whichhasanoption password topass
thepasswordonthecommandline.
example:

sftppassword="password""userid"@"servername"

Batchexample:
(
echo"
ascii
cdpub
lcddir_name
putfilename
close
quit
"
)|sftppassword="password""userid"@"servername"

IthoughtIshouldsharethisinformation,sinceIwaslookingatvariouswebsites,beforerunning
thehelpcommand( sftph ),andwasisurprisedtoseethepasswordoption.
editedJun27'14at17:18

answeredApr5'13at6:52

AndrewMedico
14.7k

31

Mahony
58

91

4 +1foryougoodsuggestion.Howeverthiswillrequireyoutopassthepasswordoncommandlineand
anybodyonthesystemdoing ps commandwouldbeabletoseeyourpassword. anubhava Apr5'13
at11:54

YoucanoverridebyenablingPasswordlessauthentication.Butyoushouldinstallkeys(pub,
priv)beforegoingforthat.
Executethefollowingcommandsatlocalserver.
Local$>sshkeygentrsa

PressENTERforalloptionsprompted.Novaluesneedtobetyped.
Local$>cd.ssh
Local$>scp.ssh/id_rsa.pubuser@targetmachine:
Promptsforpwd$>ENTERPASSWORD

Connecttoremoteserverusingthefollowingcommand
Local$>sshuser@targetmachine
Promptsforpwd$>ENTERPASSWORD

Executethefollowingcommandsatremoteserver
Remote$>mkdir.ssh
Remote$>chmod700.ssh
Remote$>catid_rsa.pub>>.ssh/authorized_keys
Remote$>chmod600.ssh/authorized_keys
Remote$>exit

Executethefollowingcommandatlocalservertotestpasswordlessauthentication.Itshouldbe
connectedwithoutpassword.
$>sshuser@targetmachine

editedNov11'14at22:15
EricLeschinski
28.8k

17

150

answeredFeb20'14at6:34
realspirituals

147

4,579

18

+1foryouranswerbutquestionwasaboutdoingitwithoutpublic/privatekeys. anubhava Feb20'14at

46

6:37
oops..Justsawtherestrictionsonkeysharing:)realspirituals Feb20'14at6:41
Outofcuriositywillthiscommand lftpp${port}u${login_id},${password}${ip_number} when
invokedfromashellscriptprintthemsomewhere?Howdidyousolveapartfromsshpass?realspirituals
Feb20'14at6:43
Idon'thave lftp andrelyon sshpass fromMacandLinuxboth. anubhava Feb20'14at6:47
Puregold.Thanks!:)NikolayTsenkov Mar13at16:32

Combinesshpasswithalockeddowncredentialsfileand,inpractice,it'sassecureasanything
ifyou'vegotrootontheboxtoreadthecredentialsfile,allbetsareoffanyway.
answeredDec12'11at11:39
RichHarding
125

Bashprogramtowaitforsftptoaskforapasswordthensenditalong:
#!/bin/bash
expectc"
spawnsftpusername@your_host
expect\"Password\"
send\"your_password_here\r\"
interact"

Youmayneedtoinstallexpect,changethewordingof'Password'tolowercase'p'tomatchwhat
yourpromptreceives.Theproblemshereisthatitexposesyourpasswordinplaintextinthefile
aswellasinthecommandhistory.Whichnearlydefeatsthepurposeofhavingapasswordinthe
firstplace.
answeredNov11'14at20:32
EricLeschinski
28.8k

17

150

147

+1butIthinkyoustateitclearlywhattheproblemsarewiththisapproach. anubhava Nov11'14at


20:57

Youcanusesshpassforit.Belowarethesteps
1. InstallsshpassForUbuntu

sudoaptgetinstallsshpass

2. AddtheRemoteIPtoyourknownhostfileifitisfirsttimeForUbuntu>sshuser@IP>
enter'yes'
3. giveacombinedcommandofscpandsshpassforit.Belowisasamplecodeforwarcoping
toremotetomcat sshpassp'#Password_For_remote_machine'scp
/home/ubuntu/latest_build/abc.war#user@#RemoteIP:/var/lib/tomcat7/webapps

answeredJun12at7:33
raviranjan
1,836

protectedbyMarkHallAug8'13at3:03
Thankyouforyourinterestinthisquestion.Becauseithasattractedlowqualityanswers,postingananswernowrequires10reputationonthissite.
Wouldyouliketoansweroneoftheseunansweredquestions instead?

Das könnte Ihnen auch gefallen