# htdigest and expect [SOLVED]

## deboeck

Hi,

I'm trying to load a large number of username-password pairs in an Apache2 htdigest file. Unfortunately, the htdigest2 doesn't allow you to specify a password on the command line. So I'm using expect to automate the task. However, when doing the following:

```

# expect

expect1.1> spawn /usr/sbin/htdigest2 -c test.digest MyRealm MyUsername

spawn /usr/sbin/htdigest2 -c test.digest MyRealm MyUsername

3487

expect1.2> expect "password:"

Adding password for MyUsername in realm MyRealm.

New password: expect1.3> send "MyPassword\r"

expect1.4> expect "password:"

Re-type new password: expect1.5> send "MyPassword\r"

expect1.6> exit

```

I end up with an empty test.digest file. If I do the same thing but with 'interact' instead of 'send' and 'expect', it does work.

Has anyone run into a similar issue with expect ?

Thx in advance,

StevenLast edited by deboeck on Fri Jan 20, 2006 2:01 pm; edited 1 time in total

----------

## magic919

You can't just stick -c in an Expect script.  That's the problem here.  I don't know a solution to escape or similar but I'm sure you can work around it.

----------

## deboeck

 *magic919 wrote:*   

> You can't just stick -c in an Expect script.  That's the problem here.

 

I don't think that's the issue. I tried adding a new user to an existing htdigest file with expect, and that also doesn't work.

```

# expect

expect1.1> spawn /usr/sbin/htdigest2 test.digest MyRealm otherUser

spawn /usr/sbin/htdigest2 test.digest MyRealm otherUser

3637

expect1.2> expect "password:"

Adding user otherUser in realm MyRealm

New password: expect1.3> send "otherPassword\r"

expect1.4> expect "password:"

Re-type new password: expect1.5> send "otherPassword\r"

expect1.6> exit

```

I don't use the -c option now, but otherUser is not added to test.digest. Besides, the example with -c does work if I use 'interactive' after spawn, instead of 'expect' and 'send'. So there must be another issue.

----------

## magic919

Expect is going a bit too quickly according to my experiments.  This worked

```

#!/usr/bin/expect

spawn /usr/sbin/htdigest2 test.digest MyRealm MyTestname

expect "password:"

send "MyPassword\r"

expect "password:"

send "MyPassword\r"

expect "#"

exit

```

I think htdigest is seeing an EOF from Expect and baling.

----------

## deboeck

That seems to work, thanks a lot for the tip. I'll post the script I wrote here, in case someone is interested. The script adds username-password pairs from a text file to an Apache2 htdigest file (and creates the htdigest file if necessary).

```

#!/usr/bin/expect

#

set realm "Members area"

set digestFile "/etc/apache2/access/users.digest"

set htdigest "/usr/sbin/htdigest2"

if { [ llength $argv ] < 1 || [ llength $argv ] > 3 } {

    puts "Usage: htdigest-bulkload <input> <realm> <file>\n"

    puts "where <input> is a file containing all the username-password pairs"

    puts "              which have to be loaded in the htdigest file."

    puts "              The file must contain one username-password pair per"

    puts "              line, separated by a space.\n"

    puts "      <realm> is the realm to which the user belongs. The default"

    puts "              realm is "Members Area".

    puts "      <file>  is the file in which the usernames have to be entered."

    puts "              Default is /etc/apache2/access/users.digest.\n"

    exit

}

# -- Checking if the digestFile exists

if { [ llength $argv ] == 3 } {

    set digestFile [ lindex $argv 2 ]

}

if { [ file exists $digestFile ] } {

    set digestExists 1

} else {

    set digestExists 0

    puts "htdigest file does not exist yet, will be created...\n"

}

# -- Was the realm specified ?

if { [ llength $argv ] >= 2 } {

    set realm [ lindex $argv 1 ]

}

# -- Reading the input file

set inputFile [ lindex $argv 0 ]

if {![ file exists $inputFile ]} {

    puts "Error: no such file $inputFile. Exiting."

    exit

}

set fd [ open $inputFile r ]

set input [ read $fd ]

close $fd

set inputList [ split $input "\n" ]

foreach pair $inputList {

    set pairList [ split $pair " " ]

    set username [ lindex $pairList 0 ]

    set password [ lindex $pairList 1 ]

    if { $username != "" } {

        if { $digestExists } {

            spawn $htdigest $digestFile $realm $username

        } else {

            spawn $htdigest -c $digestFile $realm $username

            set digestExists 1

        }

        expect "password:"

        send "$password\r"

        expect "password:"

        send "$password\r"

        expect "#"

    }

}

```

----------

