5

I need to create a bash script that will remotely run another script on a batch of machines. To do so I am passing a script through SSH.

ssh -p$port root@$ip 'bash -s' < /path/to/script/test.sh

I thought it would use my RSA keys but I am getting error:

"Enter password: ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)"

I tried using sshpass to no avail. So my next solution was using expect. I have never used expect before and I'm positive my syntax is way off.

ssh -p$port root@$ip 'bash -s' < /path/to/script/test.sh
/usr/bin/expect <<EOD
expect "password"
send "$spass\n"
send "\n"
EOD

I have root access to all machines and ANY solution will do as long as the code remains within bash. Just keep in mind that this will be done in a loop with global variables ($spass, $ip, $port, etc) passed from a parent script.

4
  • 1
    Is your SSH public key in each of the machines' root's .ssh/authorized_keys file? I would think the best case scenario to shoot for here is to figure out why you are getting the smackdown when trying to do your ssh auth using a keypair. Commented Apr 14, 2016 at 18:09
  • You can try connecting manually with ssh -v {port} root@{ip} to get a better picture of what's going on. Commented Apr 14, 2016 at 18:48
  • 2
    Fixing the key issue is a much better solution than hacking around the password prompting. Commented Apr 14, 2016 at 19:31
  • @Jnevill and Etan I agree on both accounts. SSH works every where else except here and I can not for the life of me figure out why. Mike thanks for the tip. I'll have to try it when I get to work tomorrow. Commented Apr 15, 2016 at 0:14

2 Answers 2

4

You are doing it wrong in two means:

  1. If you want expect to interact with ssh, you need to start ssh from expect script and not before.

  2. If you put the script (/path/to/script/test.sh) to stdin of ssh, you can't communicate with the ssh process any more.

You should rather copy the script to remote host using scp and then run it.

Expect script might look like this:

/usr/bin/expect <<EOF
spawn ssh -p$port root@$ip
expect "password"
send "$Spass\r"
expect "$ "
send "/path/to/script/on/remote/server/test.sh\r"
expect "$ "
interact
EOF
Sign up to request clarification or add additional context in comments.

4 Comments

missing interact and EOF
@szpal Thanks. Fixed now.
Thanks, that certainly did the trick. Is there a way to make it wait for the other script to finish before it moves on? I usually use source, will that play nice with expect?
I have no idea what you mean. I would consult with manual page, understand what this code snippet do and then try to compose another question, if there will be something unclear.
0
    #!/usr/bin/expect
    #Replace with remote username and remote ipaddress
    spawn /usr/bin/ssh -o StrictHostKeyChecking=no username@IPAddress
    #Replace with remote username and remote ipaddress
    expect "username@IPAddress's password: "
    #Provide remote system password
    send "urpassword\n"
    #add commands to be executed. Also possible to execute bash scripts
    expect "$ " {send "pwd\n"} # bash command
    expect "$ " {send "cd mytest\n"}
    expect "$ " {send "./first.sh\n"} # bash scripts
    expect "$ " {send "exit\n"}
    interact

2 Comments

Please provide description of your answer
Expect tool can be used for automating interactive applications such as ssh,telnet, ftp, passwd, fsck, rlogin, tip, etc. Try some examples from below url. thegeekstuff.com/2010/10/expect-examples

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.