-1

I am trying to create a bash script which uses kpcli in order to automate entries into a kdbx file. While searching over here I found out that you could use expect and send, however this does not seem to be working for me.

set timeout 10
spawn kpcli
match_max 100000000
expect  "kpcli:/>"
send    "open global.kdbx\n"
expect  "Please provide the master password:"
send    "mypassword"
expect  "kpcli:/>"
send    "cd Websites/"

while IFS=" " read -r domainname username password

do

expect  "kpcli:/Websites>"
send    "new "$domainname""
expect  "Username:"
send    ""$username""
expect  "Password:"
send    ""$password""
expect  "Retype to verify: "
send    "$password"
expect  "URL:"
send    ""$domainname""
expect  "Tags:" 
send    "\n"
expect  "Strings: (a)dd/(e)dit/(d)elete/(c)ancel/(F)inish?"
send     "F"
send     "\n"
expect "Database was modified. Do you want to save it now? [y/N]: "
send   "y"
send   "y"

done < sites.txt

Is this the way to do it or is there a better way?

1
  • 4
    Please don't make more work for other people by vandalizing your posts. By posting on the Stack Exchange (SE) network, you've granted a non-revocable right, under the CC BY-SA 3.0 license, for SE to distribute that content (i.e. regardless of your future choices). By SE policy, the non-vandalized version of the post is the one which is distributed. Thus, any vandalism will be reverted. If you want to delete your post, there should be a "delete" button just below the tags, on the left. You may need to use a browser, rather than the mobile app.
    – Makyen
    Jan 11 '19 at 9:00
2

The problem is you're mixing expect and bash syntax. It's possible to mix the two, but it's cleaner to just pick one. Here's your code in expect:

#!/usr/bin/expect
set timeout 10
match_max 100000000

spawn kpcli

expect  "kpcli:/>"
send    "open global.kdbx\r"
expect  "Please provide the master password:"
send -- "mypassword\r"
expect  "kpcli:/>"
send    "cd Websites/\r"

set filehandle [open sites.txt r]
while {[gets $filehandle line] != -1} {
    lassign [split $line] domainname username password

    expect  "kpcli:/Websites>"
    send    "new $domainname\r"
    expect  "Username:"
    send -- "$username\r"
    expect  "Password:"
    send -- "$password\r"
    expect  "Retype to verify: "
    send -- "$password\r"
    expect  "URL:"
    send -- "$domainname\r"
    expect  "Tags:" 
    send    "\r"
    expect  "Strings: (a)dd/(e)dit/(d)elete/(c)ancel/(F)inish?"
    send    "F\r"
    expect  "Database was modified. Do you want to save it now? [y/N]: "
    send    "y\r"
}
close $filehandle

I made some assumptions about how often you have to hit Enter (\r)

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.