I have search on this site and multiple other locations but I have been unable to resolve my problem of connecting and maintaining ssh session after one command. Below is my
The above program does not work when I need to configuration mode because ssh session is not reused
Your ssh session will be reused once you move the connect and close outside of the loop, but each exec_command() happens in a new shell (through a new channel), and are unrelated. You will need to format your commands so that they don't require any state from the shell.
If I remember correctly, some Cisco devices only allow a single exec, and then close the connection. In that case, you will need to use invoke_shell(), and work interactively using the pexpect module (which you already have imported, but aren't using).