using expect with scp

Posted on 2000-03-07
Last Modified: 2013-12-26
I am wanting to use expect to automate the copying of files from one system to another with scp, the secure copy command.  Below is the syntax of my scp command:

% scp username@host_a:filename filename

This then gives me the following prompt:

username@host_a's password:

I want to use expect to automate the entering of the password, but I can't figure out how to do this.  I'm aware of some security flaws with this approach and am taking some measures to reduce the risks.  Below is the expect script I have written so far:

#!/usr/bin/expect -f

set timeout 3600
spawn /usr/local/SSH/bin/scp /tmp/

  -re "*password*"
send "password\r"

My understanding of expect is quite rudimentary, but I am hoping to be able to work this out.  A correction to this script, or a very similar script which leads me to the solution would be great.
Question by:ragnar
LVL 20

Expert Comment

Comment Utility
You don't say what the problem is, and I don't understand what you are trying to do with the
> expect
> {
>   -re "*password*"
> }

construction: For this purpose, you should be able to get away with:

#!/usr/bin/expect --
# What does "-f" do?

set timeout 3600
spawn /usr/local/SSH/bin/scp /tmp/

expect "password:"
send "my_password\r"

( This is cribbed from the O'Reilly book )

For debugging expect scripts, it may be easier to run the expect interpreter and feed it the commands one by one.

If scp encrypts the password before sending it and the file as it's being received, IMO the main security risk is if other people have permission to read or execute your script.

Hope this helps,

Expert Comment

Comment Utility
why do you need expect for this? scp uses ssh for transport. And ssh could be set up to allow you without password from another mashine, given:
1) you permited that by creating .shosts in you  remote home;
2) remote mashine has your client mashine's public_key;
3) administrator of remote did not block this possibility.

ssh's documentation on subject:-----------
          First, if the machine the user logs in from is listed in
          /etc/hosts.equiv or /etc/shosts.equiv on the remote machine,
          and the user names are the same on both sides, the user is
          immediately permitted to log in.  Second, if .rhosts or
          .shosts exists in the user's home directory on the remote
          machine and contains a line containing the name of the
          client machine and the name of the user on that machine, the
          user is permitted to log in.  This form of authentication
          alone is normally not allowed by the server because it is
          not secure.

          The second (and primary) authentication method is the rhosts
          or hosts.equiv method combined with RSA-based host
          authentication.  It means that if the login would be
          permitted by .rhosts, .shosts, /etc/hosts.equiv, or
          /etc/shosts.equiv, and additionally it can verify the
          client's host key (see $HOME/.ssh/known_hosts and
          /etc/ssh_known_hosts in the FILES section), only then login
          is permitted.  This authentication method closes security
          holes due to IP spoofing, DNS spoofing and routing spoofing.
          [Note to the administrator:  /etc/hosts.equiv, .rhosts, and
          the rlogin/rsh protocol in general, are inherently insecure          and should be disabled if security is desired.]

Good Look!
LVL 40

Expert Comment

Comment Utility
If you'll post an acopy of exactly what's on the screen when you do an interactive scp copy (names/passwd changed...), I'm fairly sure I could write an expect script that would get the job done. As a reference look at question

Accepted Solution

jyu_88 earned 150 total points
Comment Utility
ssh-keygen to generate a pair of keys for youself.

apppend your ~/.ssh/ to server1 at /home/user1/.ssh/authorized_keys.

to save the hassle of dealing interaction, use a null passphrase for your private key during key generation.

If you need passphrase, then you need to use ssh-agent to answer the question about passphrase.

with all this setup, you should be able to do
scp this.file user1@server:/tmp/that.file

with no question asked.
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).


Expert Comment

Comment Utility
ah, my description is for ssh1. If you are useing ssh2, the setup is slightly different. Let me know you are using ssh2 so that we can do it 100% SSH way.

Expert Comment

Comment Utility

I am using ssh2 and require solution.  I have just bought this answer and unfortunately I do not think this will work.

Please advise.

Thanks, M.

Expert Comment

Comment Utility
what exactly made you think it will not work?

ssh2 from Fsecure or ssh2 as in openssh?
the public key format is different; and authorization and authentication file are different. openssh's support for ssh2 works similar to ssh1, with ~/.ssh/authorized_keys2 instead of ~/.ssh/authorized_keys as in ssh1.
Fsecure's ssh2 use ~/.ssh2 and 'identification'

Expert Comment

Comment Utility
Thanks for the prompt reply.  I'll try it in the morning.

Expert Comment

Comment Utility
Hi all,

Thanks to jyu_88 for the guidance.

The version of secure shell I have downloaded uses two files called 'authorization' and 'identification'.

I have developed a solution.

A good resource to help me was a mirror of the's FAQs:

Regards, M.

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Unix / Linux grid computing 5 125
Replacement selected text 2 47
sum28 challenge 31 95
mapAB Challlenge 35 84
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now