# Python SSH

**URL:** https://forum.networklessons.com/t/python-ssh/10725
**Category:** Lessons Discussion
**Created:** [April 14, 2020, 2:00pm UTC](https://forum.networklessons.com/t/python-ssh/10725 "2020-04-14T14:00:29Z")
**Posts on this page:** 1
**Showing post:** 18

<div class="post-metadata">

### Author: ![ReneMolenaar](https://cdn-forum.networklessons.com/user_avatar/forum.networklessons.com/renemolenaar/32/488_2.png) [@ReneMolenaar](https://forum.networklessons.com/u/ReneMolenaar)
#### Post date: [August 6, 2020, 8:45am UTC](https://forum.networklessons.com/t/python-ssh/10725/18 "2020-08-06T08:45:43Z")

</div>

Hi Azm,

Let’s see if I can help you out. First of all, I understand the confusion…I ran into the same issues when I learned Python for the first time.

I would advise becoming _very_ familiar with “basic” python scripts first. It’s so much easier when you only have to deal with your own code instead of third party libraries.

Try [creating a module](https://networklessons.com/python/python-modules#Create_and_Import_Module) on your own first that you import, that might help to understand things.

Having said that, let’s take a look.

> [@azmuddincisco](#):
>
> _from netmiko import ConnectHandler_  
> _from getpass import getpass_  
> However, I have watched some videos in youtube and some folks are only using _from netmiko import ConnectHandler_ and they are still able to establish a ssh session. What is the standard command to fire up Netmiko?

With these import lines, we import the “ConnectHandler” function from the netmiko library and the “getpass” function from the getpass library. Here’s an explanation with the differences between importing an entire module vs importing a function of a module:

> **[Python Modules](https://networklessons.com/python/python-modules)**
>
> Python modules are files with Python code that you import in your own Python code. This lesson explains how to create and import modules.

Now the big question is, what is the “ConnectHandler” function from netmiko? Looking at the examples:

> **[netmiko](https://ktbyers.github.io/netmiko/#tutorialsexamplesgetting-started)**
>
> Multi-vendor library to simplify Paramiko SSH connections to network devices

It seems that this is a function that establishes the connection through SSH. If you really wanted to know, you could dive into the netmiko code. Searching for “def ConnectHandler” I find this:

> <https://github.com/ktbyers/netmiko/blob/de1aae9bdefeea0bb94b3076c7420a220410c215/netmiko/ssh_dispatcher.py>

Looking at the code, we can see that the ConnectHandler function calls another function (ssh\_dispatcher). Now you could dive into netmiko further and further, you shouldn’t. This will be a rabbit hole with no end. The goal of using a library like netmiko is to abstract away the complexity of connecting with SSH, making it possible to connect to a device with only a few lines of code in your own script.

What about the getpass function? Let’s check the documentation:

> **[15.10. getpass — Portable password input — Python 2.7.18 documentation](https://docs.python.org/2/library/getpass.html)**

Their documentation explains it well:

The getpass module provides two functions:

**getpass.getpass([prompt[, stream]])**

_Prompt the user for a password without echoing. The user is prompted using the string prompt, which defaults to 'Password: '. On Unix, the prompt is written to the file-like object stream. stream defaults to the controlling terminal (/dev/tty) or if that is unavailable to sys.stderr (this argument is ignored on Windows)._

So when you see this in Python code, people use it so that when a user types the password, it doesn’t show up on your screen.

When you use any third-party libraries, I would always suggest checking the documentation. Popular libraries have good documentation with examples and explanations. Youtube videos can be useful but you still want to know what you are doing in your own code.

> [@azmuddincisco](#):
>
> Even I have tried to run this file from IDLE Python shell and it’s giving me some error.

There are two ways to execute Python code, there’s a detailed explanation here:

> **[Python Installation](https://networklessons.com/python/python-installation)**
>
> This lesson explains how to verify your Python version on MAC OS X, Linux, and Microsoft Windows. We also try the default IDE (IDLE).

1. You type Python code in the Interpreter which you recognize with the `>>>` symbols.
2. You run Python scripts from the command-line with “python .py”

The interpreter is useful when you want to test something quickly, otherwise, it’s so much easier to write a .py file and execute it.

> [@azmuddincisco](#):
>
> Is there any way I can establish ssh sessions from Netmiko to multiple cisco router and execute the same set of commands simultaneously? If so, please provide a sample script.

This example of netmiko is a good start:

> **[netmiko](https://ktbyers.github.io/netmiko/#tutorialsexamplesgetting-started)**
>
> Multi-vendor library to simplify Paramiko SSH connections to network devices

They execute this code for a single network device. You could use a Python for loop to execute this for multiple devices:

> **[Python For Loop](https://networklessons.com/python/python-for-loop)**
>
> The Python for loop can "loop" (iterate) through a sequence and execute code for each item in the sequence.This lesson explains everything.

I hope this helps!

Rene

---

_[View the full topic](https://forum.networklessons.com/t/python-ssh/10725)._
