0

1.after executing expect('OPR>','show alef-users') it go to infinite means it run continuesly.

from fabric.api import *
from fabric.context_managers import settings
from ilogue.fexpect import expect, expecting, run

prompts = []
prompts += expect('Username:','kirti')
prompts += expect('Password:','kirti')
prompts += expect('OPR>','show users')
prompts +=expect('OPR>','exit')
env.password = "kirti@123"
with cd('/home/kirti/opr'):
with expecting(prompts):
run('./kirti', combine_stderr=False)
kirti kumbhar
  • 71
  • 3
  • 10
  • What's your current code? – Neil Nov 23 '17 at 06:23
  • when i run this code then it go in infinite means it go again an again expect('OPR>','show opr-users) it doesn't go to next expect('OPR>','exit') – kirti kumbhar Nov 23 '17 at 09:07
  • You should simply add your code to the body of your question, via the `edit` link. It's hard to make heads or tales when it's this malformed. – Neil Nov 23 '17 at 09:08

1 Answers1

1

Fabric >= 1.9 allows to handle multiple prompts at the same time, in a single with clause: you just have use Fabric's settings function, and feed it with a dict; the keys will be the questions, and the values will be the answers. In your case, it will look like :

with settings(prompts={'Username:':'admin', 
                       'Password:':'admin', 
                       'OPR>':'show alef-users', 
                       'OPR>':'exit'}):
    run('./go_opr_cli', combine_stderr=False)

My answer is inspired by that section of Fabric's official documentation. It worked very well for me, but make sure, when writing the dict's keys, that you respect the number of spaces between the last character of the question and the cursor, in the command line, otherwise it will fail.

Example: The last prompt is 'OPR>'. If you see in the regular Linux CLI, that there is one space between 'OPR>' and the cursor, then the key should be 'OPR> '

Ismaïl Mourtada
  • 452
  • 5
  • 11
  • small typo in your prompts dictionary, there should be a colon between the key and value – user3167654 Mar 16 '18 at 18:20
  • Two entries with the same key in the same dictionary? What is that supposed to mean? Only the last of these will actually exist in the dictionary. – Lutz Prechelt May 20 '22 at 15:01