1

I am planning to take password from file instead of passing parameter to the script

set cmd1 {`cat passwdfile.txt | grep -w pj |  cut -d";" -f5`}
spawn ssh username@servername

expect "password: "
send "$cmd1\r"
expect "$ "
send "ps -ef |grep planning1\r"
expect "$ "
send "exit\r"

Error

    username@servername's password:
    Permission denied, please try again.

Why it is not taking password from the file ??

glenn jackman
  • 207,528
  • 33
  • 187
  • 305
Naresh
  • 39
  • 7
  • Also see https://stackoverflow.com/questions/1340366/how-to-make-ssh-receive-the-password-from-stdin. – steffen Feb 05 '18 at 22:43

1 Answers1

0

At the point where ssh is asking for a password, you don't have access to a shell, so the backticks and all the rest will be sent as plain characters as the password.

Assuming that file exists on your local machine, get the password before you spawn:

set pw [exec grep -w pj passwdfile.txt | cut -d\; -f5]
spawn ssh ...
expect "password: "
send "$pw\r"

Of course it's terribly insecure to store passwords in plain text. You should set up ssh keys to allow you to log in without having to type a password.

glenn jackman
  • 207,528
  • 33
  • 187
  • 305
  • thank u glenn.. i am storing password for time being .. i will delete the file immediately after my steps are completed.i will try this – Naresh Feb 06 '18 at 00:59
  • when i run this i am getting below error i am using expect ./scriptname cut: you must specify a list of bytes, characters, or fields Try `cut --help' for more information. while executing "exec grep -w pj passwdfile.txt | cut -d'" invoked from within "set pw [exec grep -w pj passwdfile.txt | cut -d';' -f5]" (file "./1.sh" line 4) – Naresh Feb 06 '18 at 01:26
  • it seems to be interpreting the semicolon as the end of the command. Oh, the single quotes are the problem: single quotes have no special meaning in Tcl, so the semicolon is not quoted or escaped. Try `cut -d\; -f5` – glenn jackman Feb 06 '18 at 02:07