python ssh类集成

                        python安装MySQL单实例多实例ssh篇
import  paramiko
from paramiko import  AuthenticationException
from paramiko.client import SSHClient,AutoAddPolicy
from paramiko.ssh_exception import NoValidConnectionsError
import  logging
import  sys
import time
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)

class MySshClient():
    def __init__(self):
        self.ssh_client = SSHClient()

    def ssh_login(self, host_ip):
        try:
            self.ssh_client.set_missing_host_key_policy(AutoAddPolicy())
            self.ssh_client.connect(host_ip, port=22,timeout=10)
        except AuthenticationException:
            logger.error('username or password error')
            return 1001
        except NoValidConnectionsError:
            logger.error('connect time out')
            return 1002
        except:
            logger.error('unknow error')
            return 1003
        return 1000

    def execute_some_command(self, command):
        logger.info('running %s'%command)
        stdin, stdout, stderr = self.ssh_client.exec_command(command)
        #print(stdout.read().decode('utf-8'))
        return stdout.read().decode()

    def ssh_logout(self):
        logger.warning('will exit host')
        self.ssh_client.close()

    def ssh_interactive(self,host_ip,cmd):
        conn_status=self.ssh_login(host_ip)
        if conn_status==1000:
            logger.info('execute cmd is %s'%cmd)
            res=self.execute_some_command(cmd)
            logger.info('ssh connect %s success,execute result is:\n%s'%(host_ip,res))
            self.ssh_logout()
            return res
        else:
            logger.error('conn failed status  code is %s'%conn_status)
            return 'no vaild data'

if __name__=='__main__':
    socket='/storage/mysql3306/mysql3306.sock'
    my_ssh_client=MySshClient()
    #cmd = "/usr/local/mysql/bin/mysql -uroot -p'test123' -S %s --connect-expired-password -e \"CREATE USER 'dbadmin'@'%' IDENTIFIED BY 'dbadmin';ALTER USER 'dbadmin'@'%' IDENTIFIED BY 'dbadmin' PASSWORD EXPIRE NEVER;ALTER USER 'dbadmin'@'%' IDENTIFIED WITH mysql_native_password BY 'dbadmin';\"" % (socket)
    #cmd="mysql -uroot -p'test123' -S %s -e \"CREATE USER 'dbadmin'@'%%' IDENTIFIED BY 'dbadmin';ALTER USER 'dbadmin'@'%%' IDENTIFIED BY 'dbadmin' PASSWORD EXPIRE NEVER;ALTER USER 'dbadmin'@'%%' IDENTIFIED WITH mysql_native_password BY 'dbadmin';grant all privileges on *.* to  'dbadmin'@'%%';CREATE USER 'repl'@'192.168.56.%%'  identified by 'repl2data';GRANT replication slave,replication client ON *.* TO  'repl'@'192.168.56.%%';ALTER USER 'repl'@'192.168.56.%%' IDENTIFIED BY 'repl2data' PASSWORD EXPIRE NEVER;ALTER USER 'repl'@'192.168.56.%%' IDENTIFIED WITH mysql_native_password BY 'repl2data';flush privilegs\";ifconfig"%(socket)
    #print(cmd)
    cmd="/usr/local/mysql/bin/mysql  -udbadmin -pdbadmin -h 192.168.56.102 -P 3306 -e\"CHANGE MASTER TO master_host='192.168.56.104', master_port=3306, master_user='repl',master_password='repl2data', master_log_file='mysql-bin.000002', master_log_pos=156;start slave;\""
    print(cmd)
    res=my_ssh_client.ssh_interactive('192.168.56.104', cmd)









  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值