Reputation: 11
Trying to manage a network element which is not listed in netmiko and so using auto-detect. There is a expect_string parameter. I tried to use with a start pattern .* to mean any more characters and ending with one of # or $ or ?. This works fine when I do it directly. However when I assign the same to a variable, it is not working. I'm not sure what is the mistake and tried to see previous stackoverflow comments in netmiko and netmiko documentation and could not get an example.
While execution of the commands, this network element is to have a prompt which will start with the same prefix, but will have characters based on the command given in between and the ending of prompt can change for a $ or ? instead of a #. I do not want to use simply [$?#] as the same characters will be present in the output of some of the commands and may not be a necessary match of the prompt.
In not working case, I'm getting the error
netmiko.exceptions.ReadTimeout: Pattern not detected: 'INCHN34003.*[#?$]' in output.
I see that the variable is assigned correctly - SetExpectString is r"INCHN34003.*[#?$]"
Working code:
NW_Session.send_command ( command_string=current_command, expect_string = r"INCHN34003.*[#?$]"
Not working code:
NW_prompt = NW_Session.find_prompt()
if ('#' in NW_prompt):
Hash_location = NW_prompt.find('#')
SetExpectString = 'r"' + NW_prompt[:(Hash_location-1)] + '.*[#?$]'
print(f"# is in the location {Hash_location} and SetExpectString is {SetExpectString}")
with open(InputKargs['OLT_COMMAND_File'], 'r') as i_file:
while current_command := i_file.readline():
NW_Session.send_command ( command_string=current_command, expect_string = SetExpectString )
Upvotes: 0
Views: 2379
Reputation: 499
I would probably do:
# Drop the last character from the prompt
nw_prompt = nw_prompt[:-1]
# There might be regex special characters in the prompt (treat them literally)
base_pattern = re.escape(nw_prompt)
# Construct final regex pattern (I dropped the ? as I doubted it was needed)
pattern = rf"{base_pattern}.*[#$]"
Upvotes: 0