1. On Linux we use the SCP command to implement the file transfer between hosts, remote login via SSH, such as the Xshell remote login tool We often used, is the Basic SSH Protocol Implementation window host telnet to the Linux host
The following simple in Python implementation of these features under the Paramiko module, this is not a python built-in module, I directly through the Pycharm download this module,
The first step is to implement a simple SSH login command code as follows:
ImportParamiko#To create an SSH objectSSH =Paramiko. Sshclient ()#allow connections to hosts that are not in the Know_hosts fileSsh.set_missing_host_key_policy (Paramiko. Autoaddpolicy ())#authentication information for the first time login#connecting to a serverSsh.connect (hostname='192.168.158.131', Port=22, Username='Root', password='Hadoop')#Execute Commandstdin, stdout, stderr = Ssh.exec_command (' ls/')#Get command ResultsRes,err =Stdout.read (), stderr.read () result= ResifResElseErrPrint(Result.decode ())#Close ConnectionSsh.close ()
Login and can achieve a simple command send and receive instruction results, the method of writing dead, but finally connected successfully, should start the virtual machine in learning iptables add a lot of rules, and on the server side of SSH modified the default port has been
Not even at all, after modifying the configuration file, set allow remote host remote connection, configure in/etc/ssh/sshd_config file
Then implement the SCP command to simulate Linux upload the download file, using the Paramiko Transport simple test Code as follows
ImportParamikotransport= Paramiko. Transport (('192.168.158.131', 22)) Transport.connect (username='Root', password='Hadoop') sftp=Paramiko. Sftpclient.from_transport (transport)#uploading location.py to the server/tmp/test.pySftp.put ('Fromlinux.txt','/tmp/test_from_win')#download Remove_path to local Local_path#sftp.get ('/root/oldgirl.txt ', ' fromlinux.txt ')transport.close ()
The same can be tested successfully, can achieve file upload and download
Finally notice the Paramiko. Rsakey This function, can think of a more powerful SSH password-free login, the core process is in Linux through the Ssh-keygen on the host to generate a pair of keys, the public key placed in the ~/.ssh/authorized_keys file, Copy the private key to the Windows Host Environment directory,
ImportParamikoprivate_key= Paramiko. Rsakey.from_private_key_file ('Id_rsa31.txt')#to place the private key here#To create an SSH objectSSH =Paramiko. Sshclient ()#allow connections to hosts that are not in the Know_hosts fileSsh.set_missing_host_key_policy (Paramiko. Autoaddpolicy ())#connecting to a serverSsh.connect (hostname='10.0.0.41', Port=22, Username='Fang', pkey=Private_key)#Execute Commandstdin, stdout, stderr = Ssh.exec_command ('Df;ifconfig') Result=Stdout.read ()Print(Result.decode ()) stdin, Stdout2, stderr= Ssh.exec_command ('ifconfig')#Get command ResultsRESULT2 =Stdout2.read ()Print(Result2.decode ())#Close ConnectionSsh.close ()
Successful implementation of the secret-free connection, code knowledge to do a simple test, to connect the main, not to consider other practical needs
These things played all afternoon, and it was quite magical,
Python for SSH and SFTP functions