简体   繁体   中英

Console session terminates while trying to terminate script

I'm trying to write a Python code that is to check on network settings on a network switch. Expecting errors, I have included 'exit.sys()' commands in case an error or non-matching data shows up. However, while I want it to terminate and throw an exception in the script, it also terminates the CLI session (Putty). What would be the best command to be able to throw an exception and terminate only the script that is running on the switch without terminating Putty?

if isc_port.endswith('g'):
    isc_port = isc_port[:-1]
elif isc_port.endswith('G'):
    sys.exit("The ISC port is also configured as an MLAG port. Please correct.")
    #return ELM

Calling sys.exit() only exits the (python) process. It does not (directly) affect putty or any other terminal (xterm, gnome-terminal, etc.).

PuTTY terminates when the connection it has is closed by the remote service. Often, eg with ssh or telnet, this occurs when the program that is run by logging in terminates. Usually the program is a shell (eg /bin/sh, /bin/bash, etc.), which when run interactively does not terminate until you type 'exit' (or send it EOF, typically Ctrl-D). When a shell is run non-interatively it will usually terminate when the command it runs terminates.

Perhaps you are using your python program as the shell when you log in? In that case, don't exit when you don't want to exit. Perhaps you have a shell script that runs your program, then exits, when you log in? If so, change that script to not exit.

Also, I have usually seen PuTTY keep the window open, so all output is still visibile, until you acknowledge the notice that the network connection was lost. It is the Windows DOS/cmd.exe window that I have seen close immediately upon termination of the program that it runs.

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