2

I am trying to ssh to a test cisco router in a test environment using python paramiko, and run cisco commands in that test router.

Everything works great except for 1 small detail. After running the script I want the ssh session to remain open. (so I can run other commands manually). I want to keep the ssh session open until I type "exit" I found another link with a similar issue but I cant understand the solution. (See here Python ssh - keep connection open after script terminates)

I would appreciate if someone can help me out here

My code

import paramiko
import time

def ssh_session(ip):
    try:
        session = paramiko.SSHClient() #Open the session
        session.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        session.connect(ip, username = "ciscouser1", password = "password")
        connection = session.invoke_shell()

        ####Running Cisco IOS commands###
        connection.send("enable\n")
        connection.send("password1") #sending
        connection.send("\n")
        connection.send("configure terminal\n\n")
        time.sleep(1)
        connection.send("do show ip int brief\n")
        time.sleep(1)
    except paramiko.AuthenticationException:
        print "wrong credentials"
ssh_session("10.10.10.1")
1
  • Line 13 comment should be #sending enable secret password to router to move to global configuration mode Commented Aug 10, 2017 at 19:17

2 Answers 2

0

The session timeout would be controlled by the SSH server. To the best of my knowledge, the only way to keep your session alive on the client side is to not be inactive, which can be accomplished by sending null packets. As to how to do this specifically with paramiko I am not certain. Perhaps you could send some kind of dummy command (or maybe even an empty string?) every so often?

Sign up to request clarification or add additional context in comments.

2 Comments

thanks for the suggestion but it doesnt seem to be the case I am afraid. I am login into a Cisco router not a server but even still I get what you mean. I would just like the script to execute and then wait for more manual input from me until I choose to close the connection. Any ideas on how to do that by any chance?
I see what you are saying. The paramiko source code looks to have an example of emulating an interactive shell, might be helpful: github.com/paramiko/paramiko/blob/master/demos/interactive.py
0

One solution could be starting a daemon thread that keeps the connection alive. See the solution here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.