@@@ 文章 content Reference Old boys education Alex Golden Horn king, Wu Sir Silver Horn King @@@
One, Paramiko module installation
system Environment CentOS 7,python3.6
1, installation Python3.6
2. Yum Installation Openssl-devel
1 [[email protected] ~]#
3. Recompile the SSL module
1 cd Python-3.6.22 ./configure--with-SSL3make4 sudo make Install
4 Installing the Paramiko module with PIP
1 [[email protected] ~]# python3-m pip install Paramiko
Second, sshclient
Used to connect to a remote server and execute basic commands
Connect based on user name password:
1 ImportParamiko2 3 #To create an SSH object4SSH =Paramiko. Sshclient ()5 #allow connections to hosts that are not in the Know_hosts file6 Ssh.set_missing_host_key_policy (Paramiko. Autoaddpolicy ())7 #connecting to a server8Ssh.connect (hostname ='10.0.0.10', Port = $, username ='Root', password='12345')9 Ten #Execute Command OneStdin,stdout,stderr = Ssh.exec_command ('DF') A #Get command Results -result = Stdout.read ()orStderr.read () - Print(Result.decode ()) the - #Close Connection -Ssh.close ()
Connection based on public key
1 ImportParamiko2 3Private_key = Paramiko. Rsakey.from_private_key_file ('id_rsa_2048')4 5 #To create an SSH object6SSH =Paramiko. Sshclient ()7 #allow connections to hosts that are not in the Know_hosts file8 Ssh.set_missing_host_key_policy (Paramiko. Autoaddpolicy ())9 #connecting to a serverTenSsh.connect (hostname ='10.0.0.10', port = 22,username ='Root', pkey=Private_key) One A #Execute Command -Stdin,stdout,stderr = Ssh.exec_command ('DF') - #Get command Results theresult = Stdout.read ()orStderr.read () - Print(Result.decode ()) - - #Close Connection +Ssh.close ()Third, Sftpclient
For connecting to a remote server and performing an upload download
Upload and download based on user name password
1 ImportParamiko2 3Transport = Paramiko. Transport (('10.0.0.10', 22))4Transport.connect (username='Root', password='12345')5 6SFTP =Paramiko. Sftpclient.from_transport (transport)7 #uploading files to the server8Sftp.put ('socketserver concurrency. PY','/tmp/test.py')9 #download the file to a localTenSftp.get ('Python-3.6.2.tar','Python.tar') One ATransport.close ()
upload and download based on public key keys
1 ImportParamiko2 3Private_key = Paramiko. Rsakey.from_private_key_file ('id_rsa_2048')4 5Transport = Paramiko. Transport (('10.0.0.10', 22))6Transport.connect (username='Root', pkey=Private_key)7 8SFTP =Paramiko. Sftpclient.from_transport (transport)9 #uploading files to the serverTenSftp.put ('socketserver concurrency. PY','/tmp/test2.py') One #download the file to a local ASftp.get ('Python-3.6.2.tar','Python.tar') - -Transport.close ()
Python Basic 13-paramiko Module Learning