1

Need to login to multiple host I am unable to decide how can we add hostname variable in this script array or list. could any one suggest.

And the second thing is that i am getting error while executing this script.

#!/usr/bin/env expect
set timeout 12
set date [exec date "+%d-%B-%Y"]
spawn sh -c "cd /backup/"

for ((i=0;i<8;i++))

do

spawn sh -c "ssh host001n < ./backup.py > /backup/dbbackup-$file-$date.txt"
expect "Enter passphrase for key '/root/.ssh/id_rsa':"
send "pass\r"
expect "Enter passphrase for key '/root/.ssh/id_rsa':"
send "pass\r"
expect "Enter passphrase for key '/root/.ssh/id_rsa':"
send "pass\r"
expect "Password:"
send "pass\r"
interact

done

After adding only one shebang here is the below error.

spawn sh -c cd /backup/
wrong # args: should be "for start test next command"
    while executing
"for ((i=0"
    (file "./backup.py" line 14)
5
  • For a start, I don't think the system knows that it's an expect script - you need a suitable shebang (or to run it explicitly with expect -f) Nov 14, 2017 at 3:02
  • Now i have edited the question with the shebang, do you want me to edit it as expect -f.
    – Shanker
    Nov 14, 2017 at 3:33
  • AFAIK you can't have two sebangs: your code is still being executed using bash (which explains the majority of the error messages - such as spawn: command not found and so on) Nov 14, 2017 at 3:35
  • So i have added only one shebang of expect, now the error is something different, edited the question.
    – Shanker
    Nov 14, 2017 at 3:39
  • 1
    That would be because you are trying to use a bash construct in expect I think. For the Tcl/expect equivalent, see for example wiki.tcl.tk/1015 Nov 14, 2017 at 3:52

1 Answer 1

1

Try this:

#!/usr/bin/env expect
set timeout 12
set date [timestamp -format "+%d-%B-%Y"]    ;# don't need to call out to date
cd /backup                                  ;# use the built-in cd command

# need to use Tcl syntax for the for loop, not shell syntax
for {set i 0} {$i < 8} {incr i} {
    spawn sh -c "ssh host001n < ./backup.py > /backup/dbbackup-$file-$date.txt"

    # more DRY
    expect {
        "Enter passphrase for key '/root/.ssh/id_rsa':" {
            send "pass\r"
            exp_continue
        }
        "Password:" {send "pass\r"}
        eof
    }
}
3
  • Also, I don't see where you set the $file variable. Nov 14, 2017 at 14:45
  • Actually while executing the command we give an argument that is called here as file.
    – Shanker
    Nov 15, 2017 at 1:57
  • set list {host001, host002, host003} is not working. calling it in this way. spawn sh -c "ssh $list < ./backup.py > /backup/dbbackup-$file-$date.txt"
    – Shanker
    Nov 15, 2017 at 2:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .