RF工具自定义linux命令行命令执行代码及数据库访问

之前写了几次数据库连接和linux命令行执行的代码,在此保存下。

另考虑到python2的中文编码问题,注释等都用简单英文,见谅~ 

import paramiko


class Excsshcmd():
    ssh = paramiko.SSHClient()

    def __init__(self):
        pass

    def ssh_login(self, hostname, username='root', password='123456', port=22):
        """
        connect to spacial ssh service ip
        :param hostname: host ip
        :param username: login user
        :param password: login password
        :param port: connect port
        :return:
        """
        Excsshcmd.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

        sshconn = Excsshcmd.ssh.connect(hostname=hostname, port=port, username=username, password=password)
        return sshconn

    def ssh_logout(self):
        """
        close ssh connection
        :return:
        """
        print('Closing SSH connection!')
        if Excsshcmd.ssh != None:
            Excsshcmd.ssh.close()

    def exc_cmd_and_return(self, exccmd):
        """
        excute command line and return result
        :param exccmd: The excute command line
        :return: result
        """
        stdin, stdout, stderr = Excsshcmd.ssh.exec_command(exccmd)
        stdin.write("Y")  # Generally speaking, the first connection, need a simple interaction.
        Excsshcmd.ssh.exec_command(exccmd)

        rest = stdout.read()
        print rest
        return rest

    def exc_cmd(self, exccmd):
        """
        only excute commandline
        :param exccmd: The excute command line
        :return:
        """
        stdin, stdout, stderr = Excsshcmd.ssh.exec_command(exccmd)
        stdin.write("Y")  # Generally speaking, the first connection, need a simple interaction.
        Excsshcmd.ssh.exec_command(exccmd)

        rest = stdout.read()
        print rest

    def excmysql(self, sqlcmd='', sqlfile='', sqluser='root', sqlhost='10.1.75.69', dbname='payment',
                 sqlpassword='test123456', sqlport='3306'):
        """
        use linux commandline to connect userdb and excute sql.  sqlcmd or sqlfile used one of them!
        :param sqlcmd:
        :param sqlfile:
        :param sqluser:
        :param sqlhost:
        :param dbname:
        :param sqlpassword:
        :param sqlport:
        :return:
        """
        if len(sqlcmd) > 1:
            sql = sqlcmd
        else:
            with open(sqlfile, 'r') as fp:
                sql = fp.read()
        execmd = "mysql -u %s -p%s -h%s -P%s  -D%s -e '%s'" % (sqluser, sqlpassword, sqlhost, sqlport, dbname, sql)
        print(execmd)
        stdin, stdout, stderr = Excsshcmd.ssh.exec_command(execmd)
        rest = stdout.read()
        print rest


if __name__ == '__main__':
    esc = Excsshcmd()
    esc.ssh_login('1.1.1.1', username='root', password='123456')
    esc.exc_cmd_and_return('ls /usr/local')
    esc.excmysql(sqlcmd='show databases', sqlhost='1.1.1.1', sqlpassword='123456')
    esc.ssh_logout()

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值