Python_oldboy_ automatic operation and Maintenance Road _paramiko,mysql (12)

Source: Internet
Author: User
Tags stdin

The content of this section:

    1. Paramiko
    2. Mysql
1.paramiko

Http://www.cnblogs.com/wupeiqi/articles/5095821.html

Paramiko is a module, socket, and SSH protocol that enables remote server operations with Python code using this module

Ansible the bottom is the module used

Function:

A. Use username password: command, file

B. Using the user name key: command, File

C. Executing the Create session

1. Connect based on user name password:

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 ())#connecting to a serverSsh.connect (hostname='c1.salt.com', Port=22, Username='Wupeiqi', password='123')  #Execute Commandstdin, stdout, stderr = Ssh.exec_command ('DF')#Get command Resultsresult =Stdout.read ()#Close ConnectionSsh.close ()
Paramiko
b'anaconda-ks.cfg\ninstall.log\ninstall.log.syslog\npackage\n\xe5\x85\xac\xe5\x85\xb1\xe7\x9a\x84 \n\xe6\xa8\xa1\xe6\x9d\xbf\n\xe8\xa7\x86\xe9\xa2\x91\n\xe5\x9b\xbe\xe7\x89\x87\n\xe6\x96\x87\xe6\xa1\xa3\n\xe4 \xb8\x8b\xe8\xbd\xbd\n\xe9\x9f\xb3\xe4\xb9\x90\n\xe6\xa1\x8c\xe9\x9d\xa2\n'
Output

If you log in with a normal user

#-*-coding:utf-8-*-#blog:http://www.cnblogs.com/linux-chenyang/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 ())#connecting to a serverSsh.connect (hostname='10.236.62.37', Port=22, Username='Root', password='123456')#Execute Commandstdin, stdout, stderr = Ssh.exec_command ('sudo ls') Stdin.write ('123456') Stdin.flush ()#Get command Resultsresult =Stdout.read ()Print(str (result))#Close ConnectionSsh.close ()
Paramiko

2. Based on the user name secret key connection

ImportParamiko Private_key= Paramiko. Rsakey.from_private_key_file ('/home/auto/.ssh/id_rsa') #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='c1.salt.com', Port=22, Username='Wupeiqi', key=Private_key)#Execute Commandstdin, stdout, stderr = Ssh.exec_command ('DF')#Get command Resultsresult =Stdout.read ()#Close ConnectionSsh.close ()
Paramiko

Python_oldboy_ automatic operation and Maintenance Road _paramiko,mysql (12)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.