LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 09-04-2009, 10:00 AM   #1
DEF.
Member
 
Registered: Apr 2009
Posts: 96

Rep: Reputation: 23
automating scp


I want to automate some scp transfers in a bash script. However, scp requests a password.

I tried the following but the script still prompts for a password.

#!/bin/bash
scp folder/file user@1.2.3.4:'/another/folder' <<CREDENTIALS
password
CREDENTIALS

Any ideas?
 
Old 09-04-2009, 10:08 AM   #2
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Set up SSH keys. See, e.g. http://linuxproblem.org/art_9.html.
 
Old 09-04-2009, 10:09 AM   #3
uteck
Senior Member
 
Registered: Oct 2003
Location: Elgin,IL,USA
Distribution: Ubuntu based stuff for the most part
Posts: 1,173

Rep: Reputation: 501Reputation: 501Reputation: 501Reputation: 501Reputation: 501Reputation: 501
You need to set up a ssh key for the user doing the transfer.
Here is a howto that will help. Just don't use a password on the key if you are prompted for one when making it.
http://pkeck.myweb.uga.edu/ssh/
 
Old 09-06-2009, 06:45 AM   #4
DEF.
Member
 
Registered: Apr 2009
Posts: 96

Original Poster
Rep: Reputation: 23
Not considered setting up the ssh keys. I will give that a try.

However, I also wanted to know how to automate the script to paste in the password for me? The reason for this is I will have many different machines to log on to and need script this rather than log on to each and setup ssh keys.
 
Old 09-06-2009, 07:35 AM   #5
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,721

Rep: Reputation: 5914Reputation: 5914Reputation: 5914Reputation: 5914Reputation: 5914Reputation: 5914Reputation: 5914Reputation: 5914Reputation: 5914Reputation: 5914Reputation: 5914
You can use expect.
http://linux.com/archive/articles/56066

More information can be found by googling.
 
Old 09-06-2009, 07:55 AM   #6
uteck
Senior Member
 
Registered: Oct 2003
Location: Elgin,IL,USA
Distribution: Ubuntu based stuff for the most part
Posts: 1,173

Rep: Reputation: 501Reputation: 501Reputation: 501Reputation: 501Reputation: 501Reputation: 501
If you use the same password, you could pass the password as a variable in the script, but that is very insecure since if someone can read the script then they have the password.
There is a way to compile a script into a binary, but I think that can be decompiled.


There are some suggestions here that might help, but did not seem to work for me.
http://ant.apache.org/manual/OptionalTasks/scp.html
 
Old 09-13-2009, 04:37 PM   #7
DEF.
Member
 
Registered: Apr 2009
Posts: 96

Original Poster
Rep: Reputation: 23
Yes i'd like to put the password into the script so to automatically give the password when prompted. But how?
 
Old 09-13-2009, 04:57 PM   #8
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,646

Rep: Reputation: 7969Reputation: 7969Reputation: 7969Reputation: 7969Reputation: 7969Reputation: 7969Reputation: 7969Reputation: 7969Reputation: 7969Reputation: 7969Reputation: 7969
Quote:
Originally Posted by DEF. View Post
Yes i'd like to put the password into the script so to automatically give the password when prompted. But how?
Expect is the way to go, as was suggested by michaelk. But I'd strongly recommend against it. From a security standpoint, it's hideous....you're storing a plain-text password for another system, in (essentially), a text-file.

Setting up SSH keys is easier, too, and reduces your script to one line:
Code:
scp <file(s) to copy> <user id>@<remote system>:<destination directory>

like

scp -r /home/user myusername@111.222.333.444:/home/backups/user
 
Old 09-14-2009, 01:32 AM   #9
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,360

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Setup ssh keys and/or look at ssh-agent, which asks you the passwd once, then stores it in memory and your ssh tools prog (eg scp) get it from there until you logout of the local the session.

http://www.sshkeychain.org/mirrors/S...s-HOWTO-6.html
http://en.wikipedia.org/wiki/Ssh-agent
 
  


Reply



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
scp does not work and gives the following error message: scp: FATAL: Executing ssh1 i akay Linux - Networking 16 09-28-2008 11:41 PM
automating scp copy, how to enter password via script? stefaandk Programming 3 11-29-2006 09:57 AM
Automating Wireless mattp Linux - Wireless Networking 4 01-19-2006 11:13 AM
Automating things... DarkSTech Linux - Networking 1 03-06-2003 11:31 AM
automating console to x c0c0deuz Linux - General 2 11-01-2002 12:02 AM

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

All times are GMT -5. The time now is 05:03 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
Open Source Consulting | Domain Registration