This article and everyone to share is the python development in the use of Paramiko Connection server methods and procedures, hope that through this article, for everyone to learn and use Paramiko helpful.
SSH Connection Steps
1.ssh Server establishes Server public key, corresponding file /etc/ssh/ssh_host_* file
first, there is an SSH server, which establishes some public keys and then puts them in a local file.
2.SSH client makes connection request
this ssh client makes a connection request .
3.SSH server sends server public key to SSH client[ How does the client react? ]
The server sends the public key to the client.
4.SSH client compares server public keywhile computing its own client pubic/private key
After the client gets the public key to compare, the same is to make judgments, receive or reject, and save, the same time to calculate their own public keys and private keys.
5.SSH client sends client public key to SSH server
Then you send the public key to the server.
6. start connection, bidirectional plus decrypt
then we start to connect the two-way communication, which is the whole process of SSH connection.
Paraiko Connect to remote server
Premise:
1.remote Server open SSH service.
2. know its Username,password
show you want to connect the machine, it has opened the SSH service, the machine we are connecting today is my other LAN,ubuntu Machine, it has been opened, the default port is ssh service, and be aware of its username and password.
Method:
1. new paramiko. Sshclient
paramiko Connect the remote server, it's the process, The first is to create a new sshclient sshclient What is it, it is paramiko provided to us a api paramiko It is a based ssh A package of the Protocol, class library It provides some useful api for us to use. Let's do all kinds of things. This sshclient
2. set hot key mechanism
The other one is to set up its hot key, which is a mechanism for setting the public key andhandling the public keys as previously mentioned. Paramiko It gives us a more simple way to set up.
3. Call API Connect
and finally it's the connect API connection, so let's see how it's encoded.
we open Pycham, create a new Python Project, and introduce the Paramiko package.
Import Paramiko
then we use Paramiko 's sshclient creates a Client object within this.
Client = Paramiko. Sshclient ()
then set up its host_key mechanism, is generally the use of Paramiko automatic horizon mechanism.
Client.set_missing_host_key_policy (Paramiko. Autoaddpolicy ())
The third is to call its connect method, we can see that its connect method has a lot of parameters, there are IP, port, user name, password, There are a number of other parameters. We are using one of the more important parameters is that timeout is the time -out, that is, when we build this link, we want to let it, if how long no direct connection, return the exception.
Client.connect (Hostname=self.host, Port=self.port, Username=self.usr, Password=self.pwd, Timeout=self.timeout)
and then this connect us first, to see What happens if you don't set this host_key .
It will error, will throw a Sshexception This exception, that this server can not find, not in this known_hosts file, so that this is necessary.
Client = Paramiko. Sshclient ()
Client.set_missing_host_key_policy (Paramiko. Autoaddpolicy ())
Client.connect (Hostname=self.host, Port=self.port, Username=self.usr, Password=self.pwd, Timeout=self.timeout)
Let's take a look.
There will be no error. How do I know that this connection is normal?
because of this connect API It would throw a sshexception exception if, in case of a connection exception , and now it does not throw an exception to indicate that it is a normal connection.
original link:http://www.maiziedu.com/wiki/frame/query/
How do I connect to a server using Paramiko?