Results 1 to 10 of 10

Thread: Using 'expect' and scp - hiding prompts

  1. #1
    Join Date
    Feb 2007
    Beans
    67

    Using 'expect' and scp - hiding prompts

    I'm using a small "expect" script to automate password entry for some file copying.

    The automation works fine, but the "Password: " prompt still displays on the screen. How do I hide the prompts so the user doesn't see "Password: " on the console?

    Here's the relevant section of my script:

    Code:
    expect -c "spawn /usr/bin/scp -q $2 user@$1:$2
    		set timeout 60
    		expect {
    		    "*password:*" { send myPassword\n; interact }
    		    eof { exit }
    		    timeout { puts \n--TIMEOUT!--\n;exit}
    		}
    		exit
    	    "

  2. #2
    Join Date
    Sep 2008
    Beans
    551
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: Using 'expect' and scp - hiding prompts

    Try redirecting all i/o streams to /dev/null...

    /usr/bin/scp -q $2 user@$1:$2 &> /dev/null

  3. #3
    Join Date
    Feb 2007
    Beans
    67

    Re: Using 'expect' and scp - hiding prompts

    Thanks for the reply, but

    A) That doesn't work in the context of the "spawn" arguemnt to "expect"

    B) I'm pretty sure that if it did work, it would defeat the whole purpose of "expect" and prevent "expect" from receiving the "password:" prompt.

    I need to know how to get "expect" to stifle the display; not how to supress the message from being sent by the client.

  4. #4
    Join Date
    Sep 2008
    Beans
    551
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: Using 'expect' and scp - hiding prompts

    Code:
    log_user 0
    Set it to 1 to enable output again.

  5. #5
    Join Date
    Feb 2007
    Beans
    67

    Re: Using 'expect' and scp - hiding prompts

    Thanks!

    That did the trick. My only nit-pick is that I still get an extra blank line printed where the "Password:" prompt would have been. Do you have any idea how to suppress that extra line feed?

  6. #6
    Join Date
    Sep 2008
    Beans
    551
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: Using 'expect' and scp - hiding prompts

    Not sure as the doc I read said that should suppress all the output (got it from Exploring Expect by Oreilly btw)...

    Shouldnt that first case be this though?:
    Code:
    -re ".*password:.*" { send "myPassword\r"; interact }
    #edited newline with \r

  7. #7
    Join Date
    Feb 2007
    Beans
    67

    Re: Using 'expect' and scp - hiding prompts

    Yes, you're right...I forgot the second '.' before the '*'. Good catch! I thought it might help, but no luck.

    It still didn't fix the problem of the additional line feed. I've tried changing the end of the expression from ".*" to ".*\r", ".*\n", and ".*$", and nothing works.

    Anyone else have any idea why I'm getting the extra linefeed?

  8. #8
    Join Date
    Sep 2008
    Beans
    551
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: Using 'expect' and scp - hiding prompts

    See if the quoting within quotes is causing any issue by turning this into an expect script and running it that way, instead of inline...

    File test.exp:
    Code:
    #!/usr/bin/expect
    
    set hostName [lindex $argv 0]
    set filePath [lindex $argv 1]
    
    spawn /usr/bin/scp -q "$filePath" "user@$hostName:$filePath"
    set timeout 60
    expect {
     -re ".*password:.*" { send "myPassword\n"; interact }
      eof { exit }
      timeout { puts "\n--TIMEOUT!--\n";exit}
    }
    exit
    Then run:
    Code:
    ./test.exp host file
    Just curious to see if its kicking out those \n's somewhere because you are doing it inline or whether its coming from scp terminating...

  9. #9
    psusi is offline Ubuntu addict and loving it
    Join Date
    Sep 2005
    Location
    Orlando, FL
    Beans
    3,980
    Distro
    Ubuntu Development Release

    Re: Using 'expect' and scp - hiding prompts

    Putting your password in a script kind of defeats the purpose of having one. You should be using ssh-agent to cache the password so you only have to enter it once, rather than writing it down in a script where anyone can find it.

  10. #10
    Join Date
    Oct 2009
    Beans
    1

    Re: Using 'expect' and scp - hiding prompts

    I found this bit in the man pages that suggests interact may be the culprit.
    I have yet to find a way to circumvent this.

    During interact, previous use of log_user is ignored. In particular, interact will force its output to be logged (sent to the standard output) since it is presumed the user doesn't wish to interact blindly.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •