Avatar of beer9
beer9
Flag for India asked on

How to take password using 'expect' from user ?

How can I take echo less password from the shell prompt and put it inside a variable in expect ?
Currently I am using $myscript.sh hostname "uname" mypassword

But it shows the password on the shell prompt, Can I make it echo less ? Like expect will ask me for my password and save in interval variable and also while taking password it would be echoless.

Please let me if there is a way


#!/usr/bin/expect -f
 
set timeout 2
set HOST [lindex $argv 0]
set CMD [lindex $argv 1]
set PASS [lindex $argv 2]
 
spawn ssh $HOST $CMD
match_max 100000
expect "yes/no" {send "yes\r"}
expect "password:"
send -- "$PASS\r"
expect eof

Open in new window

Shell ScriptingUnix OS

Avatar of undefined
Last Comment
beer9

8/22/2022 - Mon
omarfarid

try to use the interact command after

expect "password:"

this will turn control to user to enter password
beer9

ASKER
But I am using a for loop to login to more than 100 host, and I will have to type password for all of them.. :)
and also I do not want to keep my password inside the file for security risk.. Can not I assign my password to a variable at run time without echoing on the prompt ??
omarfarid

you may pass password as an argument to the script e.g. argument1, then in the expect script you do

set password [lindex $argv 1]
.
.
expect "password:"
send "$password\r"
expect "password:"
send "$password\r"
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
beer9

ASKER
I do not want to echo the password on prompt while providing as the argument to expect. Can't I take password like below script ?
It doesn't echo the password entered by user, it takes the input silently.
$ cat test.sh
#!/usr/bin/bash
 
echo Please, enter your Password
read -s pass
echo "your password is $pass"

Open in new window

omarfarid

You may do like this:

#!/usr/bin/bash
#
echo "Please enter password: "
read password
export password
(call expect script here)

in the expect script, put

expect "password:"
send "env(password)\r"
expect "password:"
send "env(password)\r"
Maciej S

Not exactly what you were asking for, but you may consider using ssh passwordless keys instead of just passwords.
As you wrote something about 100 hosts, it's probably some administration task you want to automate. Take a look at dsh (distributed shell) and Cluster SSH.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
beer9

ASKER
$cat /home/balbirs/script/bssh.sh


#!/bin/bash

echo -n "(this is bssh script talking)"
read -s pass
export password=$pass
/home/balbirs/script/expect.sh $password $1



$cat /home/balbirs/script/expect.sh



#!/usr/bin/expect -f
set timeout 9
set PASS [lindex $argv 0]
set SCRIPT [lindex $argv 1]

spawn "$SCRIPT"
match_max 100000
expect "yes/no" {send "yes\r"}
expect "password:"
send "$PASS\r"
expect "password:"
send "$PASS\r"
expect "password:"
send "$PASS\r"
expect eof
============================================


$ cat /tmp/2
for i in host1 host2 host3
do
scp /tmp/123 $i:/tmp
done


Now I am running a command like these
$bssh.sh /tmp/2

and it is working fine but for 3 host I had to mention "expect "password:" 3 times,
How can I make it(expect.sh) dynamic if I have to use 100+ host ?
omarfarid

you may expect different prompts and send different responses. e.g. you expect 2 times password: prompt and response by password value. for the 3rd expect you may do like:

expect "password:"  { send "$PASS\r" } \
      "$"   { send "exit\r" }

The $ might be replaced with system prompt like #
beer9

ASKER
I have 100+ host list with the "password:" expect, it is getting exited after third expect login only..
How can I make expect to match "password:" 100+ times ?
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
omarfarid

I could not understand what you mean by "make expect to match "password:" 100+ times"

logic should be

shell script to loop for 100+ hosts that calls expect script for each host, and each time expect should change the password
beer9

ASKER
Hi Omar,

My concern is if I every time recall the expect script then I will have to provide 100 times password manually which would ultimately pass the password to my ssh client asking for password, So i am making expect to handle it, like give my password only once and provide(pass) it to any expect with 'password:' irrespective of there number of match.

Thank you.
omarfarid

is it the same password on all the servers?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
beer9

ASKER
yes :)
omarfarid

OK, here the skeleton:

#!/usr/bin/bash
#
echo "Please enter password: "
read password
export password
for host in host1 host2 host3 host4 # till host100
do
   /path/to/expectscript
done

The expect script then should do

set HOST env(host)
set PASS env(password)
spawn ssh $HOST
(rest of expect script)
 spawn ssh
omarfarid

ignore last line in last posting
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
ASKER CERTIFIED SOLUTION
omarfarid

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
beer9

ASKER
Thanks Omar, It works fine with me! Appreciate your spoon feeding unexpected help in expect :)
omarfarid

Welcome, I hope you can customize it to meet your requirement.
beer9

ASKER
Thanks a lot Omar! :)
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.