Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/prompt options #53

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions includes/cisco.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,23 +74,24 @@ def recv(self,timeout=5):
result = False
while ( time.time() - startTime < timeout ):
if self.channel.recv_ready():
recvBuffer += jimi.helpers.replaceBackspaces(self.channel.recv(1024).decode())
recvBuffer += self.channel.recv(1024).decode().strip()
if recvBuffer.split('\n')[-1].endswith("--More--"):
self.channel.send(" ")
recvBuffer = recvBuffer[:-8]
elif recvBuffer.split('\n')[-1].lower().startswith(deviceHostname.lower()) or recvBuffer.split('\r')[-1].lower().startswith(deviceHostname.lower()):
result = True
break
break
time.sleep(0.1)
if result:
return recvBuffer
return False

def sendCommand(self,command):
self.channel.send("{0}{1}".format(command,"\n"))
time.sleep(0.5)
return True

def command(self, command, args=[], elevate=False, runAs=None, timeout=5):
def command(self, command, args=[], elevate=False, runAs=None, timeout=5,promptMatch=None,promptResponse=None):
if command == "enable":
enableResult = self.enable(self.enablePassword)
if enableResult:
Expand All @@ -108,16 +109,18 @@ def command(self, command, args=[], elevate=False, runAs=None, timeout=5):
return (None, "", "Unable to save config")
if args:
command = command + " " + " ".join(args)
if self.sendCommand(command):
returnedData = self.recv(timeout)
if returnedData:
returnedData = jimi.helpers.replaceBackspaces(returnedData)
if promptMatch:
returnedData = ""
self.sendCommand(command)
if self.awaitStringRecv(promptMatch):
self.sendCommand(promptResponse)
returnedData = self.recv(timeout)
return (0, returnedData, "")
else:
returnedData = "Failed to retrieve data"
maxLen = 40
if len(command) < maxLen:
maxLen = len(command)-1
if command[:maxLen] not in returnedData:
return (None, "", "Unable to find prompt - is the match correct?")
elif self.sendCommand(command):
returnedData = jimi.helpers.replaceBackspaces(self.recv(timeout))
if command not in returnedData:
return (None,returnedData,"Unable to send command")
else:
return (None,"","Unable to send command")
Expand Down
16 changes: 12 additions & 4 deletions models/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,9 @@ class _remoteCommand(action._action):
command = str()
arguments = list()
elevate = bool()
answerPrompt = bool()
promptMatch = str()
promptResponse = str()
runAs = str()
timeout = 60

Expand All @@ -262,22 +265,27 @@ def doAction(self,data):
if connection_id == "":
connection_id = data["flowData"]["var"]["remote_connection_id"]

command = helpers.evalString(self.command,{"data" : data["flowData"]})
arguments = helpers.evalList(self.arguments,{"data" : data["flowData"]})
command = helpers.evalString(self.command,{"data" : data["flowData"], "eventData" : data["eventData"], "conductData" : data["conductData"], "persistentData" : data["persistentData"] })
arguments = helpers.evalList(self.arguments,{"data" : data["flowData"], "eventData" : data["eventData"], "conductData" : data["conductData"], "persistentData" : data["persistentData"] })
promptMatch = helpers.evalString(self.promptMatch,{"data" : data["flowData"], "eventData" : data["eventData"], "conductData" : data["conductData"], "persistentData" : data["persistentData"] })
promptResponse = helpers.evalString(self.promptResponse,{"data" : data["flowData"], "eventData" : data["eventData"], "conductData" : data["conductData"], "persistentData" : data["persistentData"] })
try:
client = data["eventData"]["remote"][connection_id]["client"]
except KeyError:
return {"result" : False, "rc" : 255, "msg" : "Unable to load remote connection - connection_id='{0}'".format(connection_id)}
if client:
exitCode, output, errors = client.command(command,args=arguments,elevate=self.elevate,runAs=self.runAs,timeout=self.timeout)
if self.answerPrompt:
exitCode, output, errors = client.command(command,args=arguments,elevate=self.elevate,runAs=self.runAs,timeout=self.timeout,promptMatch=promptMatch,promptResponse=promptResponse)
else:
exitCode, output, errors = client.command(command,args=arguments,elevate=self.elevate,runAs=self.runAs,timeout=self.timeout)

if exitCode != None:
return {"result" : True, "rc" : exitCode, "msg" : "Command successful", "data" : output, "errors" : errors, "command" : command}
else:
return {"result" : False, "rc" : 255, "msg" : client.error, "data" : output, "errors" : errors, "command" : command}
else:
return {"result" : False, "rc" : 403, "msg" : "No connection found"}

class _remoteMultiCommand(action._action):
connection_id = str()
commands = str()
Expand Down
Loading