Score:0

expect usage in bash script or bash command in expect script

np flag
JKC

I have a script below that works except for the expect portion:

#!/bin/bash
#
invdir=/home/john/inventory

for file in $invdir/$1
do
  if [ -z $1 ] || [ -z $2 ]
  then
     echo "You must enter a value: prod, dev, dr, or test AND the password of the env you entered"
     exit 0
  else
      for host in `cat $file`
      do
    ssh-copy-id -i ~/.ssh/id_rsa.pub $host <<-EOF
    expect "password:"
    send "$2\n" 
    EOF
      done
  fi
done

I found an expect script that does most of what I need:

#!/usr/bin/expect -f
spawn ssh-copy-id $argv
expect "password:"
send "your_password\n"
expect eof

to execute ./expect_script user@host1

My problem is that I don't know enough of either bash or expect to get these two to work under one bash script or expect script.

Thank you in advance....

Score:1
fo flag
#!/bin/bash
#
invdir=/home/john/inventory

for file in "$invdir"/"$1"
do
  if [[ -z "$1" ]] || [[ -z "$2" ]]
  then
    echo "You must enter a value: prod, dev, dr, or test AND the password of the env you entered"
    exit 0
  else
    while IFS= read -r host
    do
      export host
      export pw="$2"
      expect <<'EOF'

        spawn ssh-copy-id -i $env(HOME)/.ssh/id_rsa.pub $env(host)
        expect "password:"
        send "$env(pw)\n" 
        expect eof

EOF
    done < "$file"
  fi
done

Some notes:

  • take care to quote your shell variables, especially positional params
  • you need to launch the expect interpreter to run expect code
  • pass shell variables to expect via the environment
  • don't read the lines of a file with for
  • I don't use <<-EOF because it's too easy to use non-tabs for the EOF word.
  • but do use <<'EOF' to protect the expect variables
JKC avatar
np flag
JKC
hi Glenn, Thank you for the answer and suggestions. When I ran the new script, it's giving me the following errors: `./new-ssh-login.sh prod mypasswd` `invalid command name "ssh-copy-id" while executing "ssh-copy-id -i ~/.ssh/id_rsa.pub $env(host)"`
fo flag
D'oh! I forgot to use the crucial expect `spawn` command to launch the process we're going to control. Fixed.
fo flag
I'm not sure off the top of my head that expect understands `~` , so I used the HOME environment variable instead.
JKC avatar
np flag
JKC
Thanks Glenn. the scripts works now!!! I do have 2 questions. 1. how does the line `while IFS= read -r host` translate to $file (as this is what it looks like it's doing. 2. the line `done < "$file"` is this FEEDING the while loop Again, thank you so much.
fo flag
Yes that's right. We're redirecting the contents of the file into the while loop, and the `read` command then consumes the input line-by-line. Explained in detail at https://mywiki.wooledge.org/BashFAQ/001
JKC avatar
np flag
JKC
hi Glenn, One more favor. How can I ping the host and if it pings, go onto the spawn ssh-copy-id command else goto the next host. I'm just not sure how to put this in the script. Thanks again.
fo flag
Ask a separate question.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.