Python SSH

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:

There are various other options for Macs including EVE-NG which has comprehensive API documentation 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, 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:

Let us know how you get along!

I hope this has been helpful!

Laz

thanks very very much

1 Like

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.

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.
https://docs.paramiko.org/en/stable/api/client.html

I hope this has been helpful!

Laz

1 Like

Hello,

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

.......
# Run command.
ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command("show systemip")
ssh_stdin.close()

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

And as output I get:
command result: []

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

Hello Lazaros, thank you for your reply :slight_smile:

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).

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

1 Like

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

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:

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

1 Like

Hello Rene,
many thanks, it works.
Mihaly

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.

Hello Ripal

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

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:

I hope this has been helpful!

Laz

1 Like