1

Can someone help me on my below request please. I'm trying to send password from a file to execute a script on remote machine, however it is giving error as

Password: can't read "sys_pw": no such variable while executing "send "echo $sys_pw\r""

here is my code..

#!/bin/bash

sys_pw=`cat /home/admin/scrp/sys.pw`

expect -c 'spawn ssh admin@<servername> -o StrictHostKeyChecking=no "df -Ph"; expect "Password:"; send "$sys_pw\r"; interact'

1
  • And for a simple question? What happens if you echo $sys_pw right there in the console? Say even before calling expect ? Jan 24, 2023 at 19:29

1 Answer 1

0

It is giving error

Password: can't read "sys_pw": no such variable while executing "send "echo $sys_pw\r""

Your script is broken:

$ shellcheck myscript
 
Line 3:
sys_pw=`cat /home/admin/scrp/sys.pw`
^-- SC2034 (warning): sys_pw appears unused. Verify use (or export if used externally).
       ^-- SC2006 (style): Use $(...) notation instead of legacy backticks `...`.

Did you mean: (apply this, apply all SC2006)
sys_pw=$(cat /home/admin/scrp/sys.pw)
 
Line 5:
expect -c 'spawn ssh admin@<servername> -o StrictHostKeyChecking=no "df -Ph"; expect "Password:"; send "$sys_pw\r"; interact'
          ^-- SC2016 (info): Expressions don't expand in single quotes, use double quotes for that.

$ 

Source: ShellCheck – shell script analysis tool

You must log in to answer this question.

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