netmiko icon indicating copy to clipboard operation
netmiko copied to clipboard

Best way to pause execution of script to visually inspect changes before user input to confirm?

Open waleedqq opened this issue 1 year ago • 6 comments

Is there any particularity method I can use to make sure that the code has executed correctly before confirming manually?

I am using changing a particular config on 100s of interfaces on Cisco IOS XR routers, I would love the ability to view the changes that will be committed to the router, before actually committing them.

command = "show commit changes diff"
result = ssh.send_command(command, read_timeout=300)
input("Press Enter to continue...")

Is the above code correct where it would give me 5 minutes to look at what is going to be committed? But I could push enter for it to proceed if if I was satisfied before 5 minutes as elapsed? Or is my logic all wrong?

waleedqq avatar Jul 06 '23 13:07 waleedqq

Not sure if Netmiko has a way of doing this natively. Are you wanting to see the output of the "show commit changes diff" command, verify it looks good, then follow up with another command to commit it?

You could make the standard input work to pause execution while you look at the show command output. Typer also has a pretty cool Confirm feature that's just a simple y/n answer you could use.

namiles avatar Jul 08 '23 05:07 namiles

Are you wanting to see the output of the "show commit changes diff" command, verify it looks good, then follow up with another command to commit it?

That is exactly what I want to check, just to visually make sure that it has behaved and executed as expected.

Can you elaborate on this please:

You could make the standard input work to pause execution You mean like a using the sleep() function?

waleedqq avatar Jul 10 '23 03:07 waleedqq

@namiles

I might have come up with a solution, its quite simple, in Python 3.0+ just using the input() method.

#show changes that were made but not yet commited to visually inspect them
result = ssh.send_command('show commit changes diff')
print(result)

#Waits for User to confirm before proceeding
input("Press Any Key to Confirm:............")

#commit the changes 
result_commit = ssh.commit()
print(result_commit)

My only concern here is the ssh timeout, how much time would I have by default to inspect the output before the ssh session is dropped?

waleedqq avatar Jul 10 '23 09:07 waleedqq

@waleedqq The SSH timeout is probably the idle timeout of your remote network devices (or of any intermediate firewalls including potentially a host firewall).

I don't think Netmiko/Parmiko would time this out.

ktbyers avatar Jul 10 '23 15:07 ktbyers

@waleedqq Yes - input() would work here, as execution would pause until you enter something.

@ktbyers Could a context manager be useful here to avoid timeout? I haven't ever had the need to keep an SSH session open for very long, so i'm curious about this myself

namiles avatar Jul 10 '23 15:07 namiles

@namiles A context manager wouldn't help with the timeout--it would automatically clean things up if there was a failure, but it wouldn't help prevent a failure.

ktbyers avatar Jul 10 '23 17:07 ktbyers