stdin pipe to expect script

Posted on 2009-05-01
Last Modified: 2013-12-21
I want to feed a string to an expect script and have it stored in a variable and used as a password.
Needs to be invoked as:
echo "my_password" | ./myscript.exp

Anyone know how to do this?
Please see script example below:

#!/usr/bin/expect -f 

#### *** insert some command here to set $PASSWD to stdin ***

spawn ssh test@localhost

expect "*?password:*" { send "$PASSWD\n" }

expect "$ " 

send "exit\n"


Open in new window

Question by:amrox
    LVL 1

    Expert Comment

    You'd better run your script as ./myscript.exp "my password". Then you can create $PASSWD very simple: set PASSWD [lindex $argv 0]
    LVL 4

    Author Comment

    yes, I know how to use arguments, but this is not what I want.

    Is it possible to do what I've asked?
    LVL 4

    Accepted Solution

    I found the solution, here it is for those who want to know:
    while { !{eof stdin]} {
      gets stdin USERNAME
      gets stdin PASSWD

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
    I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension ( This reminded me of questions tha…
    The viewer will learn how to count occurrences of each item in an array.
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    25 Experts available now in Live!

    Get 1:1 Help Now