Echo Password Into Bash Script

Home » CentOS » Echo Password Into Bash Script
CentOS 7 Comments

Hey guys,

I’m trying to echo my password into some commands inside of a bash script. But I think I’m going about it incorrectly.

Here’s the top part of my script:

#!/bin/bash pub=”~/.ssh/id_rsa.pub”
dps_pass=”my_pass”
ssh=”/usr/bin/ssh”
scp=”/usr/bin/scp”
for i in 10.10.10.2{5,6}
do echo “xfring key up”
echo $dps_pass | $scp $PUB digitalplatform@$i:

And here’s how it executes:

#bash -x deploy_key.sh
+ pub=’~/.ssh/id_rsa.pub’
+ dps_pass=’nbcuV01P!’
+ ssh=/usr/bin/ssh
+ scp=/usr/bin/scp
+ for i in 10.10.10.2{5.6}
+ echo ‘xfring key up’
xfring key up
+ echo ‘my_pass’
+ /usr/bin/scp /Users/my_user/.ssh/id_rsa.pub digitalplatform@10.10.10.25:
Password:

Can someone please let me know where I’m going wrong?

Thanks Tim

7 thoughts on - Echo Password Into Bash Script

  • I don’t think you can have utilities like ssh/scp take in passwd from an echo on the command line. I’d also suggest you change your password(s) based on the line above … from your original email.

    if you really want to do something like this, I’d suggest expect as the scripting language.

  • What are you trying to do? Copy your public key out to use it for future authentication? Use ssh-copy-id to set it up the first time? Or look at the package sshpass.

  • Don’t try to automate your password like this for scp or other ssh-related apps. Generate and use a public/private keypair instead and your script will then be able to connect without prompting for a password.

    Peter

  • Well, look at the lines in my script that I’m showing here. That’s exactly what I’m doing. Copying up my public key so that later in the script (which I didn’t show, no need to I think) is to cat the public key into place and make sure there are proper permissions etc on the .ssh directory on the remote machine.

    But Eero and other are right.. I’ll be much better off using expect to get this type of work done. It’s jut that I’m more familiar with bash so I
    thought that there might be a good way to do it with that also.