# 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:** 15
**Page:** 2

<div class="post-metadata">

### Author: ![lagapidis](https://cdn-forum.networklessons.com/user_avatar/forum.networklessons.com/lagapidis/32/4949_2.png) [@lagapidis](https://forum.networklessons.com/u/lagapidis)
#### Post date: [November 11, 2020, 6:35am UTC](https://forum.networklessons.com/t/python-ssh/10725/22 "2020-11-11T06:35:55Z")

</div>

Hello Ugo

I don’t have personal experience with Mac PCs, but I am under the impression that GNS3 does run well on Mac OS X. Take a look at this post on GNS3’s site concerning how to set that up:

[https://docs.gns3.com/docs/getting-started/installation/mac/](https://docs.gns3.com/docs/getting-started/installation/mac/)

There are various other options for Macs including EVE-NG which has [comprehensive API documentation](https://www.eve-ng.net/index.php/documentation/howtos/how-to-eve-ng-api/) and from what I read, it does support Mac environments.

The other tools necessary to get this to work is the use of an API Platform like [Postman](https://www.postman.com/), which is also available for Mac.

Once you get those running on your Mac, you can then view the configuration process and the code involved in logging on using SSH which can be found in the following lesson:

> **[Python SSH](https://networklessons.com/python/python-ssh)**
>
> This lesson explains how you can use Python and the Paramiko SSH library to connect to a Cisco router and retrieve the output of the routing table.

Let us know how you get along!

I hope this has been helpful!

Laz

---

<div class="post-metadata">

### Author: ![u.forte](https://cdn-forum.networklessons.com/user_avatar/forum.networklessons.com/u.forte/32/1191_2.png) [@u.forte](https://forum.networklessons.com/u/u.forte)
#### Post date: [November 17, 2020, 6:09pm UTC](https://forum.networklessons.com/t/python-ssh/10725/23 "2020-11-17T18:09:54Z")

</div>

thanks very very much

---

<div class="post-metadata">

### Author: ![mariosku799](https://cdn-forum.networklessons.com/letter_avatar_proxy/v4/letter/m/8baadc/32.png) [@mariosku799](https://forum.networklessons.com/u/mariosku799)
#### Post date: [January 26, 2022, 1:29pm UTC](https://forum.networklessons.com/t/python-ssh/10725/24 "2022-01-26T13:29:24Z")

</div>

Hello,  
In the below command used in the lesson:

> ssh\_stdin, ssh\_stdout, ssh\_stderr = ssh.exec\_command(command)

Can you please explain the “ssh\_stdin, ssh\_stdout, ssh\_stderr” part?

Are these new variables defined by us?  
Since in the code in the lesson i see only the ssh\_stdout used:

> output = ssh\_stdout.readlines()

Why do we need the ssh\_stdin and ssh\_stderr?  
Thanks.

---

<div class="post-metadata">

### Author: ![lagapidis](https://cdn-forum.networklessons.com/user_avatar/forum.networklessons.com/lagapidis/32/4949_2.png) [@lagapidis](https://forum.networklessons.com/u/lagapidis)
#### Post date: [January 28, 2022, 10:05am UTC](https://forum.networklessons.com/t/python-ssh/10725/25 "2022-01-28T10:05:55Z")

</div>

Hello Marios

When using Paramiko and the `ssh.exec_command` function, it contains what are known as three data streams. These are the input, the output, and any errors that may appear. This is known as a 3-tuple. The syntax is:

` stdin, stdout, stderr = ssh.exec_command("command")`

You can use any variable names you like in place of `stdin`, `stdout`, and `stderr`.

All three of these streams are returned whenever the `ssh.exec_command` function is executed and they contain the following information:

- stdin - the input stream. This contains the actual commands that were sent
- stdout - the output stream. This contains the output that the CLI returns in response to the command
- stderr - any errors that may have appeared are stored herre.

These streams act as standard Python object files and can be manipulated as such. That’s why in the lesson, you see a command such as `output = ssh_stdout.readlines()`.

Take a look at this information from Paramiko, and in particular, the explanation for the `exec_command`.

> **[Client — Paramiko documentation](https://docs.paramiko.org/en/stable/api/client.html)**

I hope this has been helpful!

Laz

---

<div class="post-metadata">

### Author: ![Aleksandr.Egorov](https://cdn-forum.networklessons.com/letter_avatar_proxy/v4/letter/a/ec9cab/32.png) [@Aleksandr.Egorov](https://forum.networklessons.com/u/Aleksandr.Egorov)
#### Post date: [May 5, 2022, 1:55pm UTC](https://forum.networklessons.com/t/python-ssh/10725/26 "2022-05-05T13:55:13Z")

</div>

Hello,

Could you please advise why I am getting empty output from stdout?

```auto
.......
&#35; Run command.
ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command("show systemip")
ssh_stdin.close()

output = ssh_stdout.readlines()
&#35; Close connection.
ssh.close()
print('command result: ', output)
.......

```

And as output I get:  
command result:

---

<div class="post-metadata">

### Author: ![lagapidis](https://cdn-forum.networklessons.com/user_avatar/forum.networklessons.com/lagapidis/32/4949_2.png) [@lagapidis](https://forum.networklessons.com/u/lagapidis)
#### Post date: [May 7, 2022, 5:38am UTC](https://forum.networklessons.com/t/python-ssh/10725/27 "2022-05-07T05:38:13Z")

</div>

Hello Aleksandr

I believe that the problem is that you’re issuing the command `show systemip`. However, this command doesn’t exist on a Cisco IOS device, so the result will be an error. That means that the output will be sent through the **ssh\_stderr** stream. Try looking at the output of **ssh\_stderr** to see if you get anything there.

Let us know your results!

I hope this has been helpful!

Laz

---

<div class="post-metadata">

### Author: ![Aleksandr.Egorov](https://cdn-forum.networklessons.com/letter_avatar_proxy/v4/letter/a/ec9cab/32.png) [@Aleksandr.Egorov](https://forum.networklessons.com/u/Aleksandr.Egorov)
#### Post date: [May 10, 2022, 7:23am UTC](https://forum.networklessons.com/t/python-ssh/10725/28 "2022-05-10T07:23:34Z")

</div>

Hello Lazaros, thank you for your reply 🙂

I should have mentioned this is not a Cisco device, sorry for such confusion.  
I have checked ssh\_stderr as well - it’s empty. Also, I tried to get output from the device by the Pexpect module and it works fine.  
I think Paramiko cannot help me here with my device (not sure what is exact difference b\w Paramiko and Pexpect).

---

<div class="post-metadata">

### Author: ![lagapidis](https://cdn-forum.networklessons.com/user_avatar/forum.networklessons.com/lagapidis/32/4949_2.png) [@lagapidis](https://forum.networklessons.com/u/lagapidis)
#### Post date: [May 12, 2022, 2:32pm UTC](https://forum.networklessons.com/t/python-ssh/10725/29 "2022-05-12T14:32:53Z")

</div>

Hello Aleksandr

Thanks for the clarification! Hmm, that does seem strange indeed. There should be some response, whether an error or not, but in any case, I’m not sure what the behavior is when connected to a non-cisco device.

Pexpect is a similar tool to Paramiko in that it allows for the automation of interactive applications such as SSH.

Paramiko is a Python implementation of SSH, and version 2 specifically, and can provide both client and server functionality. Pexpect on the other hand is a module that supports a much broader range of communication protocols including FTP, Telnet and others. For the most part, Pexpect is a superset of Paramiko but requires more parameterization.

I hope this has been helpful!

Laz

---

<div class="post-metadata">

### Author: ![botlik.mihaly](https://cdn-forum.networklessons.com/letter_avatar_proxy/v4/letter/b/ce73a5/32.png) [@botlik.mihaly](https://forum.networklessons.com/u/botlik.mihaly)
#### Post date: [July 6, 2022, 1:47pm UTC](https://forum.networklessons.com/t/python-ssh/10725/30 "2022-07-06T13:47:58Z")

</div>

Hello Rene, Hello Lazaros,  
I am using gns3 to try connect to my lab, but it does not work  
if I try to execute in pycharm:

```
C:\privat\connect_ssh_with_python\Scripts\python.exe C:/privat/connect_ssh_with_python/ssh.py
Process finished with exit code 0 
```

nothing happened. if I try to execute with cmd:

```
C:\privat\connect_ssh_with_python\Scripts\python.exe C:/privat/connect_ssh_with_python/ssh.py

Process finished with exit code 0 
```

as administrator, nothing happened.

I installed the Microsoft loopback adapter for KM-Test so that I can ping the IPs 172.16.1.100 and 172.16.1.101 from the cmd.

How can I execute the command right? Could you tell me how I can do it step by step?  
Many thanks in advance  
Mihaly

---

<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: [July 13, 2022, 2:18pm UTC](https://forum.networklessons.com/t/python-ssh/10725/31 "2022-07-13T14:18:44Z")

</div>

Hello @botlik.mihaly,

This is what you want to see:

```
Process finished with exit code 0 
```

It means the code ran without any errors. If it does have errors, it shows `exit code 1`.

You should however also see messages from when you use the `print` function in your code. It’s been a while since I used Pycharm, but by default, it should show whatever you “print” on your console.

You can try debugging your code:

> **[Python Integrated Development Environment (IDE)](https://networklessons.com/python/python-integrated-development-environment-ide#Debug)**
>
> This lesson explains what a python Integrated Development Environment (IDE) is with examples of Visual Studio Code, Online and Mobile IDEs.

This is an example for vscode but it works similarly on Pycharm. If you are new to Python, I would also recommend to start with vscode instead of Pycharm. Vscode is a light weight IDE and easier to become familiar with.

Rene

---

<div class="post-metadata">

### Author: ![botlik.mihaly](https://cdn-forum.networklessons.com/letter_avatar_proxy/v4/letter/b/ce73a5/32.png) [@botlik.mihaly](https://forum.networklessons.com/u/botlik.mihaly)
#### Post date: [July 14, 2022, 6:05am UTC](https://forum.networklessons.com/t/python-ssh/10725/32 "2022-07-14T06:05:54Z")

</div>

Hello Rene,  
many thanks, it works.  
Mihaly

---

<div class="post-metadata">

### Author: ![rips.naik1990](https://cdn-forum.networklessons.com/letter_avatar_proxy/v4/letter/r/87869e/32.png) [@rips.naik1990](https://forum.networklessons.com/u/rips.naik1990)
#### Post date: [September 17, 2022, 2:54am UTC](https://forum.networklessons.com/t/python-ssh/10725/33 "2022-09-17T02:54:59Z")

</div>

Hi Rene,

Would you mind adding topic to teach nornir framework in the course ? it seems getting popular day by day as a Framework the way it helps to manage larger inventory for hyperscale automation.

---

<div class="post-metadata">

### Author: ![lagapidis](https://cdn-forum.networklessons.com/user_avatar/forum.networklessons.com/lagapidis/32/4949_2.png) [@lagapidis](https://forum.networklessons.com/u/lagapidis)
#### Post date: [September 20, 2022, 5:02am UTC](https://forum.networklessons.com/t/python-ssh/10725/34 "2022-09-20T05:02:09Z")

</div>

Hello Ripal

Thanks for your suggestion! It would be a good idea to share your suggestion on the following Member Ideas page.

> **[Lesson Ideas - NetworkLessons.com](https://networklessons.com/lesson-ideas)**
>
> This page allows networklessons members to share what content they would like to see in the future.

There you will find that others may have suggested something similar, and you can add your voice to theirs. IN the meantime, take a look at this Cisco information about nornir:

> **[Exploring Nornir, the Python Automation Framework](https://blogs.cisco.com/developer/nornir-python-automation-framework)**
>
> In Norse mythology, Nornir spun the threads of fate that connected all beings. In a network environment, Nornir lets you keep track of your device inventory and run tasks against all devices handling data.

I hope this has been helpful!

Laz

---

<div class="post-metadata">

### Author: ![said.elime84](https://cdn-forum.networklessons.com/letter_avatar_proxy/v4/letter/s/eb8c5e/32.png) [@said.elime84](https://forum.networklessons.com/u/said.elime84)
#### Post date: [October 10, 2024, 9:40am UTC](https://forum.networklessons.com/t/python-ssh/10725/35 "2024-10-10T09:40:20Z")

</div>

Hello - Rene and dear staff - this is not a question - just an expression of gratitude  
After completing your Ptyhon course, i was able to incorporate many elements you taught us into the following script:

```auto
import paramiko

device_ip = input("Enter IP of device: ")
device_username = input("Enter your username: ")
device_password = input("Enter your password: ")
cli_command = input("Enter your command: ")

ssh = paramiko.SSHClient()

def connect_to_device(ip,username,password,command):
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    try:
        ssh.connect(ip, 
                 username=username, 
                 password=password,
                 look_for_keys=False )
        ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command(command)
        output = ssh_stdout.readlines()
        ssh.close()
        return output
    except:
        print("Unable to connect")
        
output = connect_to_device(device_ip,device_username,device_password,cli_command)

if output != None:
    for line in output:
        line=line.strip()
        print(line)

```

It dynamically asks for credentials /device information/command feed them into function and print a neat show output!

The script is also designed to handle errors

Im really thrilled to reach this point - thanks to your lessons !

ps - im not able to recreate the indentation in the post - but it works , next up: retrieve IPs from a file

---

<div class="post-metadata">

### Author: ![lagapidis](https://cdn-forum.networklessons.com/user_avatar/forum.networklessons.com/lagapidis/32/4949_2.png) [@lagapidis](https://forum.networklessons.com/u/lagapidis)
#### Post date: [October 12, 2024, 4:33am UTC](https://forum.networklessons.com/t/python-ssh/10725/36 "2024-10-12T04:33:20Z")

</div>

Hello Said

Thank you so much for sharing your results with us, and for your kind words. We’re thrilled that the lessons have been so useful to you! We hope that the site continues to be an important resource in your endeavors.

Laz

[Previous page](https://forum.networklessons.com/t/python-ssh/10725.md?page=1)
