First, SSH introduction
SSH (Secure Shell) belongs to the user layer protocol running on the transport layer, which is more secure than Telnet.
Two, SSH remote connection
SSH remote connection has two ways, one is to log in directly with the user name and password, and the other is to log in with the key.
1, user name and password login
Spend 1 to log in on your own host to spend 2 of the computer, she can run the following code to achieve
import Paramikossh = Paramiko. Sshclient () Ssh.set_missing_host_key_policy (Paramiko. Autoaddpolicy ()) # Skip the remote connection and select ' Yes ' link, ssh.connect ( " ip " , " user name ", " password Span style= "COLOR: #800000" > " ) stdin, stdout, stderr = Ssh.exec_command ( Span style= "COLOR: #800000" > " df " Span style= "COLOR: #000000" >) print stdout.read ()
Here to use the Paramiko module, which is a third-party module, to self-import (to use the Paramiko module, but also to import the Pycrypto module before it can be used).
Tips: Download a pip (Super handy!!!) Perfectly avoid the inexplicable errors that will occur when importing the module), enter PIP install Paramiko a sentence to resolve
2. Key Login
Spend 1 to log in on their own host to spend 2 of the computer, spend 1 with the command ssh.keygen-t RSA generated public and private keys, she sends her public key to spend 2, using ssh-copy-id-i ~/ssh/id_rsa.pub [email protected] command
Then run the following code to implement the
ImportParamikoprivate_key_path='/home/auto/.ssh/id_rsa'Key=Paramiko. Rsakey.from_private_key_file (private_key_path) SSH=Paramiko. Sshclient () Ssh.set_missing_host_key_policy (Paramiko. Autoaddpolicy ()) Ssh.connect ('IP', 22,' User name',Key) stdin, stdout, stderr= Ssh.exec_command ('DF')PrintStdout.read ()
About key Login, everyone has a public key, a private key, the public key is for others, the private key is to keep, only their private key can unlock their public key encrypted files.
Spend 1 have a love letter to send flowers 2, you should download the public key to spend 2 to encrypt, so spend 2 can use their own private key to solve the love letter, get content.
If spend 2 to confirm whether it is spending 1 I send her love letter, go to download a flower 1 public key, randomly write some letters, with flower 1 public key encryption, sent to each other, the other side of the decryption sent back to Flower 2, if the spent 2 received the decrypted letter and the same, the other side is the flower 1 undoubtedly.
Python ssh telnet