-1

I have a script which goes and finds a username, password and IP address from a keepass database (kpcli) and have a variable $ssh_info that returns output to 3 lines like this

bobuser
bobpassword
bobip

what's the best way for me in a bash script to then say take the results of the $ssh_info

ssh bobuser@bobip and then use expect to send the password

Something like

echo "$2"  (bobpassword)
ssh $1@$3  (ssh bobuser@bobip)

Or even an expect script that passes the $2 (bobpassword) into the ssh $1@$3

Thanks for your help!

Paul Dawson
  • 1,332
  • 14
  • 27
  • maybe this is a duplicate of http://stackoverflow.com/questions/4780893/use-expect-in-bash-script-to-provide-password-to-ssh-command – Bertrand Martel Nov 27 '15 at 13:17
  • Are you asking how to split the variable into the various fields? Are you asking how to use `expect` to send the password to `ssh`? Are you asking something else? – Etan Reisner Nov 27 '15 at 13:47
  • Are you asking how to split the variable into the various fields? Yes :-) – Paul Dawson Nov 27 '15 at 14:31

1 Answers1

0

Perhaps this is what you're thinking of?

expect <<END_EXPECT
    lassign [split "$ssh_info" \n] user pw ip
    ssh \$user@\$ip
    expect "assword:"
    send -- "\$pw\r"
    interact
END_EXPECT

That uses the shell variable $ssh_info. The Tcl variables require a backslash to hide them from the shell.

If you want to split the multi-line string in the shell, I'd do one of

{ read user; read pw; read ip; } <<<"$ssh_info"
mapfile -t info <<<"$ssh_info"  # then use ${info[0]}, ${info[1]}, ${info[2]}
glenn jackman
  • 238,783
  • 38
  • 220
  • 352