LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-13-2018, 07:24 AM   #1
rockstar05
Member
 
Registered: Dec 2011
Posts: 68

Rep: Reputation: Disabled
Lightbulb Script to upload files via sftp on the remote server without asking password.


Hi All,

I'm trying to create one script which having aim of it would automatically upload the files which was given after running the script.
I tried with bash but I did not come to know how can I do it without password, I given password in script itself but it failed again.


Quote:
#!/bin/bash
HOST='172.15.61.10'
USER='test'
PASSWD='test123'
sftp -P 2216 $USER@$HOST
lcd /SFTP/files/
cd /store_files/out/
put *.doc
Can anyone check and what kind of change I need to do in above script which takes password automatically while running script.

Thanks
 
Old 03-13-2018, 07:59 AM   #2
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 3,341
Blog Entries: 3

Rep: Reputation: 1473Reputation: 1473Reputation: 1473Reputation: 1473Reputation: 1473Reputation: 1473Reputation: 1473Reputation: 1473Reputation: 1473Reputation: 1473
You'll first need a key so that the log in can be automated. The batch mode won't without it. I'd recommend an ed25519 key, unless you need backwards compatibility in which case use RSA.

Set up keys:

Code:
ssh-keygen -f ~/.ssh/testserver.ed25519 -t ed25519 -C "a key for the tested server"

ssh-copy-id -i ~/.ssh/testserver.ed25519 -p 2216 test@172.15.61.10
If you use a passphrase on the key, then you must first load it into your agent. If you left the passphrase blank, not usually recommended, then skip this step.

Code:
ssh-add ~/.ssh/testserver.ed25519
Test your keys manually.

Code:
sftp -i ~/.ssh/testserver.ed25519 -P 2216 test@172.15.61.10
All that only needs to be done once. After it is all in place, work on the actual script.

Code:
#!/bin/bash
HOST='172.15.61.10'
USER='test'
PASSWD='test123'
echo << EOT | sftp -b - -i ~/.ssh/testserver.ed25519 -P 2216 $USER@$HOST
lcd /SFTP/files/
cd /store_files/out/
put *.doc
EOT
There are several ways to read the batch file. Above shows using stdin which is represented by a dash ( - ) and it is fed via a pipe ( | ) from a here document but there are several other ways you could read the batch file.
 
1 members found this post helpful.
Old 03-13-2018, 09:07 AM   #3
rockstar05
Member
 
Registered: Dec 2011
Posts: 68

Original Poster
Rep: Reputation: Disabled
Hi,

Thanks for your explanation, it works with ssh-keygen.
But when I re-run the script, it gives me some error like below,

Quote:
Uploading test123.doc to /SFTP/files/test123.doc
remote open("/SFTP/files/test123.doc"): Failure

Uploading test234.doc to /SFTP/files/test234.doc
remote open("/SFTP/files/test234.doc"): Failure
And much more failure in that we have files to upload in the location.

Thanks
 
Old 03-13-2018, 01:47 PM   #4
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 3,341
Blog Entries: 3

Rep: Reputation: 1473Reputation: 1473Reputation: 1473Reputation: 1473Reputation: 1473Reputation: 1473Reputation: 1473Reputation: 1473Reputation: 1473Reputation: 1473
What does it say just after login about the "cd" action? The "lcd" seems to work otherwise it would not be finding the file to upload.

Also, you can add verbosity with a -v option on sftp to see more. If you read the output line by line to yourself it might give a hint about what is going wrong.
 
Old 03-13-2018, 02:35 PM   #5
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,229

Rep: Reputation: 711Reputation: 711Reputation: 711Reputation: 711Reputation: 711Reputation: 711Reputation: 711
Quote:
Uploading test123.doc to /SFTP/files/test123.doc
Something seems wrong if local dir is /SFTP/files (lcd) and remote dir is /store_files/out (cd)
 
Old 03-13-2018, 03:22 PM   #6
scasey
Senior Member
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.5
Posts: 1,612

Rep: Reputation: 527Reputation: 527Reputation: 527Reputation: 527Reputation: 527Reputation: 527
While cd would be relative to the remote user's $HOME, so /store/etc would actually be $HOME/store/etc,
Code:
lcd /SFTP/files/
is expecting an SFTP directory at root (/)...which seems unusual. Wonder if the SFTP directory is actually at the local user's $HOME as well?

But, as keefaz says, why is the error reporting /SFTP as the remote when it's defined as local??
 
Old 03-13-2018, 10:43 PM   #7
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,557

Rep: Reputation: 1092Reputation: 1092Reputation: 1092Reputation: 1092Reputation: 1092Reputation: 1092Reputation: 1092Reputation: 1092
Off: you might want to use variable-name 'User' instead of 'USER' -- The latter is predefined by the shell and can be read-only.
 
  


Reply

Tags
bash, binbash, script, shell script, shell scripting


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
script to sftp file to remote server OzTEXS Linux - Newbie 6 06-23-2013 05:33 PM
How to list files when connected to another server using SFTP in an automated script cmovva Linux - Newbie 3 02-11-2013 02:46 PM
[SOLVED] SFTP file upload bash script issue. moodah Programming 1 10-07-2010 10:55 PM
Bash script for batch upload files on server dotcom22 Linux - Server 7 07-28-2009 05:18 AM
Connection, login and upload files to ftp server via shell script Paulo Ges Linux - Networking 2 02-21-2004 01:01 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:31 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration