LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Expect TCL Programming pProblem (https://www.linuxquestions.org/questions/programming-9/expect-tcl-programming-pproblem-4175455604/)

GNewbie 03-25-2013 11:34 PM

Expect TCL Programming pProblem
 
Hi All,

I'm developing TCL code to enter a list of passwords into ecryptfs in order to find which password is accurate. I know the base of the password, but I'm missing the cap status and the charcters for the last two digits, but I have the possible characters narrowed down. The bottom line is a I have about 200-250 password I need to feed into ecryptfs.

I coded the following and I will explain the resulting error at the end of the code

Code:

#!/usr/bin/expect

set timeout 10

#Set variables here – prefer incrementing through list

set pwlist[list Pass1 Pass2]     

foreach password $pwlist {

spawn sudo "ecryptfs-recover-private"

expect {
  timeout { send_user "\nTime Out Hit\n"; puts "$password\r"; exit 1 }
  eof { send_user "\nEnd of file hit, attempt terminated \n"; exit 1 }
 
  if {"Try to recover this directory? [Y/n]: "} {
    send "y\r"
  } elseif {"Do you know your LOGIN passphrase? [Y/n] "} {
    send "y\r"
  } elseif {"Passphrase: "} {
    send "$password\r"
  } else {
    puts "end of expect if statement"
  }

}

}

ecryptfs responds:

Try to recover this directory? [Y/n]:

The Expect response should be "Y"

Then ecryptfs asks:

Do you know your LOGIN passphrase? [Y/n]

The Expect response should be "Y"

Then it asks:

Passphrase:

The Expect response should be to enter the current password in the list.

The result:

"Try to recover this directory? [Y/n]: " is displayed in the terminal and nothing happens until it times out. If I enter the Y, nothing happens until it times out. The first password int he list is displayed in the terminal afer it times out.

I think the problem likely lies here:

"if {"Try to recover this directory? [Y/n]: "}"

My guess is it should be in the form:

"if {$return_var = "Try to recover this directory? [Y/n]: "}" or some such.

I can't find anything to guide my way, so any help is appreciated.

TIA...

GNewbie 03-27-2013 01:16 AM

Update - I used the autoexpect program to help me dial in the details of my code. I have a working version that cycles through ecryptfs password input and I will post it below.

First, though, I have three questions.

1. I want to create a list in TCL that isn't all on one line.

set pwlist[list item1 item2 --what goes here so that a new line isn't registered--
item3 item4]

The items in this list would be item1, item2, item3 and item4.

I tried a \ character, but that didn't work.

2. I don't know what output will come when the right password is entered. What code in TCL will abort the program once unidentified program output is encountered?

On to the code that is working as (1. as long as the list text is all on one line and 2. I don't know what will happen when the right password is input... will the program abort or will it continue on? I don't know)

3. Some of the passwords I tested had a $ character in them, but expect choked because it thought it was an unassigned variable. How can I fix this?

Code:

#!/usr/bin/expect -f
#
# This Expect script was generated by autoexpect on Wed Mar 27 04:45:46 2013
# Expect and autoexpect were both written by Don Libes, NIST.
#
# Note that autoexpect does not guarantee a working script.  It
# necessarily has to guess about certain things.  Two reasons a script
# might fail are:
#
# 1) timing - A surprising number of programs (rn, ksh, zsh, telnet,
# etc.) and devices discard or ignore keystrokes that arrive "too
# quickly" after prompts.  If you find your new script hanging up at
# one spot, try adding a short sleep just before the previous send.
# Setting "force_conservative" to 1 (see below) makes Expect do this
# automatically - pausing briefly before sending each character.  This
# pacifies every program I know of.  The -c flag makes the script do
# this in the first place.  The -C flag allows you to define a
# character to toggle this mode off and on.

set force_conservative 0  ;# set to 1 to force conservative mode even if
                          ;# script wasn't run conservatively originally
if {$force_conservative} {
        set send_slow {1 .1}
        proc send {ignore arg} {
                sleep .1
                exp_send -s -- $arg
        }
}

set timeout 30
spawn $env(SHELL)
match_max 100000

set pwlist[list test1 test2]

foreach password $pwlist {

spawn sudo "ecryptfs-recover-private"

expect -exact "Try to recover this directory? \[Y/n\]: "
send -- "y\r"

expect -exact "y\r
INFO: Found your wrapped-passphrase\r
Do you know your LOGIN passphrase? \[Y/n\] "
send -- "y\r"

expect -exact "y\r
INFO: Enter your LOGIN passphrase...\r
Passphrase: "
send -- "$password\r"

expect -exact "\r
Error: Unwrapping passphrase and inserting into the user session keyring failed \[-5\]\r
Info: Check the system log for more information from libecryptfs\r
\]0;xubuntu@xubuntu: ~xubuntu@xubuntu:~\$ "
puts $password

}


GNewbie 03-28-2013 10:59 PM

I've found solutions to two of the three problems:

1. I want to create a list in TCL that isn't all on one line.

set pwlist[list item1 item2 --what goes here so that a new line isn't registered--
item3 item4]

Answer:

set pwlist[list item1 item2 \
item3 item4]

The pwlist list includes item1, item2, item3 and item4.

The items in this list would be item1, item2, item3 and item4.


I tried a \ character, but that didn't work.

Update, the \ works. I received an error and assumed it was related to the \ but was probably related to not escaping the $ sign (like this: \$)

2. I don't know what output will come when the right password is entered. What code in TCL will abort the program once unidentified program output is encountered?

On to the code that is working as (1. as long as the list text is all on one line and 2. I don't know what will happen when the right password is input... will the program abort or will it continue on? I don't know)

No answer yet. Any guidance would be appreciated.

3. Some of the passwords I tested had a $ character in them, but expect choked because it thought it was an unassigned variable. How can I fix this?

Answer: The $ sign must be escaped with the \ character. Example: \$

GNewbie 03-28-2013 11:30 PM

Hi All,

Please note that the set assignment is displayed as:

"set pwlist[list Pass1 Pass2]"

Note their should be a space between "pwlist" and "[list Pass1 Pass2]"

I don't know how to make this space display correctly.


All times are GMT -5. The time now is 08:59 AM.