make password-less ssh with expect

Discussion in 'General Linux' started by DKljuikov, Oct 29, 2013.

  1. DKljuikov

    DKljuikov New Member

    Messages:
    3
    Likes Received:
    1
    Trophy Points:
    3
    Hello!
    Im trying to write script wich will be send public ssh key to several hosts. And these actions should be executed without entering password.
    i.e. i need something like:
    >>>
    /usr/bin/expect -c 'eval spawn {cat ~/.ssh/id_rsa.pub | ssh ftj_dklj@oracle "mkdir -p ~/.ssh && cat - >> ~/.ssh/authorized_keys"}
    expect "assword:"
    send "*****\n"
    expect eof'
    <<<
    I know that pipe is not allowed in expect. Example above is just for understanding what i want) Also i've tried at least create .ssh directory. But these actions also were not successfull.

    Code:
    >>>
    expect -c 'spawn ssh ftj_dklj@oracle
    expect "assword:"{
    send "****\n"
    }
    expect "login"{
    send "mkdir -p ~/.ssh\n"
    }
    '
    <<<
    platform -
    Linux oracle.test.fts 2.6.9-55.0.0.0.2.ELhugemem #1 SMP Wed May 2 15:04:06 PDT 2007 i686 i686 i386 GNU/Linux
    Could u please advise what im doing wrong?
    Thx, Denis

    Last edited: Oct 29, 2013
    DevynCJohnson likes this.
  2. grim76

    grim76 Active Member Staff Writer

    Messages:
    177
    Likes Received:
    48
    Trophy Points:
    28
    Why not try using something like sshpass or similar to do this? I take it that you want to enter your password one time and not thirty times.
  3. DKljuikov

    DKljuikov New Member

    Messages:
    3
    Likes Received:
    1
    Trophy Points:
    3
    because i have no permissions to install sshpass. But we already have expect installed. so it would be good to use that we already have
  4. DKljuikov

    DKljuikov New Member

    Messages:
    3
    Likes Received:
    1
    Trophy Points:
    3
    Issue has been fixed here is script:
    Code:
    #!/usr/bin/expect -f
    set password "*********"
     
    spawn ./ssh-copy-id.sh -i /home/oracle/.ssh/id_rsa.pub ftj_dklj@oracle
    expect {
      "assword" {
        send "$password\r"
      }
    }
    expect eof
    exit 0
    But im not able to convert it to bash version.

    Code:
    export PASSWD="******"
     
    /usr/bin/expect  << EOF
    spawn ./ssh-copy-id.sh -i /home/oracle/.ssh/id_rsa.pub ftj_dklj@oracle
    expect "*?assword: "
    send "${PASSWD}\n"
    EOF
    This code is not working. Could u please correct me?

Share This Page