[Tutor] pexpect interactive password entry for luks device

Adam Gold awg1 at gmx.com
Mon Nov 24 15:02:58 CET 2014

I'm trying to do something really simply (I think) with pexpect but must
be missing an obvious step.  I want to ssh from host to guest, establish
a CIFS connection on the guest to a share somewhere else on the local
network, open a luks loop device on the share by entering the password
(I don't want this to be non-interactive, either with a key file or by
including the password in the pexpect script) and then mounting the device.

I'm using the following code:

password = getpass.getpass('Enter passphrase for /opt/luks: ')
child = pexpect.spawn('ssh root at')
child.sendline('mount.cifs -o credentials=/root/cred_f //
child.sendline('losetup /dev/loop0 /opt/luks')
child.sendline('cryptsetup luksOpen /dev/loop0 l')
child.expect('Enter passphrase for /opt/luks: ')
child.sendline('mount /dev/mapper/l /opt')

The best that happens is the CIFS share gets mounted and the program
exits while leaving me in the child terminal.  I've tried varying the
placement of child.interact() but that makes no difference or replacing
the first line which uses getpass with a 'child.expect' line when the
password is required.

Could anyone help me?

More information about the Tutor mailing list