Silmulate keystrokes in bash script

Posted on 2006-05-11
Last Modified: 2008-02-01
Hi experts,

I was wondering if someone could help me out with a bash script.

I have script that simply does an scp

scp *.js root@

Is there a way to add the password info and enter key to the bash script so I don't have enter it everytime I run the script?

Question by:DanRaposo
    LVL 43

    Expert Comment

    First of all try to omit password
    openssh tries as hard as it can to avoid reading password from non terminal input.
    So You could try expect script instead ...
    LVL 24

    Expert Comment

    just providing the link for expect
    LVL 3

    Accepted Solution

    Hi DanRaposo,

    I was checking in my linux box and as "scp" does not provide an option to put the password, I can suggest you to use: "expect".

    I created a very simple script that you can use, check below:
    #!/usr/bin/expect -f

    set timeout -1
    spawn scp test_file.txt root@localhost:/
    match_max 100000
    expect -exact "root@localhost's password: "
    send -- "passxxword\r"
    expect eof

    Above I put my script, and for yours would be something like:
    #!/usr/bin/expect -f

    set timeout -1
    spawn scp *.js root@
    match_max 100000
    expect -exact "root@'s password: "
    send -- "passxxword\r"
    expect eof

    The scripts above are really simple and will not handle if you have a problem during the execution of "scp" like the "problem" below:
    The autenticity of host 'Hostname or Ip(ip address)' can't be established.
    RSA Key fingerprint is xxxxxxxxxxxxxx.
    Are you sure you want to continue connecting (yes/no)?

    You can change the script to whatever you need and if you are not familiar with "expect", the "autoexpect" command can help you, just: "man autoexpect".

    I hope it helps you. =0)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
    The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!

    759 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