Search code examples
linuxbashexpect

There is a script which should authorize automatically, it works, but i want to take password from .txt file


Bash script (name: script.sh)

#!/bin/bash
rsync -avz username@ip:/fileineed /home

Expect script

#!/bin/expect
spawn ./script
expect “password:”
answer “pass” #i want to take answer from a file which name is password.txt
expect “$”

Solution

  • is an extension of the language

    To read a file, you need to open the file, read from the opened channel and then close the channel.

    read by default includes the trailing newline in the result. string trim can strip it off but read has an option to do it.

    set f [open password.txt r]
    set password [read -nonewline $f]
    close $f
    send -- "$password\r"