简体   繁体   中英

Using pexpect to control kpcli

I am trying to automate ssh connectivity using keepassdb and I am just starting out the script to interrogate the keypass kpcli shell. I want it to print out the result at the end.

# -*- coding: utf -*-
import os,sys
import pexpect
global str
db_kp='/media/sf_VM_shared/passwords.kdb'
pass_kp='KDBPASSWORD'
kp = pexpect.spawn('/usr/bin/kpcli')
kp.expect('>')
kp.sendline=('open /media/sf_VM_shared/passwords.kdb')
kp.expect=('Please provide the master password:')
kp.sendline=(pass_kp)
kp.expect('>')
kp.sendline=('cd General/Network/Firewalls/SSH/')
kp.expect=('kpcli:/General/Network/Firewalls/SSH>')
kp.sendline=("show -f 0")
print(kp.before)

I am getting the following when trying to run the file:

python3 ssh_firewall.py 
Traceback (most recent call last):
File "ssh_firewall.py", line 12, in <module>
kp.expect('>')
TypeError: 'str' object is not callable

On this line:

kp.expect=('Please provide the master password:')

You're replacing the expect function, rather than calling it. Take the = out:

kp.expect('Please provide the master password:')

You're doing the same thing with kp.sendline , so you'll need to fix that as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM