Python Forum
How to keep paramiko ssh session open after loggin in using python?
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to keep paramiko ssh session open after loggin in using python?
#1
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 https://stackoverflow.com/questions/3201...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 enable secret password to router to move to global configuration mode
        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")
Reply
#2
Then you don't want to use paramiko, as that doesn't leave connections open.  The answer given in the SO article you linked is to simply use subprocess to call the system's ssh command.  In your case, you'd probably supply stdin to subprocess.
Reply
#3
@nilamo,
Thanks for the reply but I am still new to python.
Can you point me to a guide or something to explain?
What is stdin?
Reply
#4
In general, streams (of which stdin is one), are unrelated to python: https://en.wikipedia.org/wiki/Standard_streams
With regards to subprocess, it's one of the arguments you can use to send data to the child process: https://docs.python.org/3.6/library/subp...rocess.run
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Paramiko SSH Session -> Authentification failed werwurm01 0 5,739 Mar-02-2021, 08:15 AM
Last Post: werwurm01
  Python Paramiko, continuing the interaction for a command in interactive shell aditya_g01 0 8,684 Mar-13-2018, 04:51 AM
Last Post: aditya_g01
  Python error handling Paramiko pythondiy 2 21,871 Aug-22-2017, 02:22 AM
Last Post: oldseven

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020