首页 > 代码库 > 简单的堡垒机模型

简单的堡垒机模型

刚学python,写了一个简单的堡垒机模型,功能自己添加


#!/usr/bin/env python

#coding:utf-8


import MySQLdb

import paramiko

#connect mysql

conn = MySQLdb.connect("218.197.119.18","zhangpan","845896787","baoleiji")

cur = conn.cursor()


sqlUser = "select * from User"

sqlHost = "select * from Host"


cur0 = cur.execute(sqlUser)


#get User rows


result1 = cur.fetchmany(cur0)


for row in result1:

    check_name = row[1]

    check_passwd = row[2]



host_list = []  

#login

while 1:

    

    username = raw_input(‘username:‘).strip()

    password = raw_input(‘password:‘).strip()

    

    if username==check_name and password == check_passwd:

        cur1=cur.execute(sqlHost)

        result2 = cur.fetchmany(cur1)


        for item in result2: host_list.append(item)

        

        

        print ‘all host list:\n‘

        for item1 in host_list:

            host_id = item1[0]

            host_name = item1[3]

            ip_address = item1[1]

            port = item1[2]

            print host_id,host_name,ip_address,port

        while 1:

            choose = raw_input("please input you want connect host:").strip()

            user_choose = int(choose)

            if user_choose<len(host_list):

                

                hostip = host_list[user_choose][1]

                username = host_list[user_choose][4]

                passwd = host_list[user_choose][5]

                port = int(host_list[user_choose][2])

                print hostip,username,passwd,port

                ssh = paramiko.SSHClient()

                ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

                ssh.connect(hostip,22,username,passwd)

                stdin,stdout,stderr = ssh.exec_command(‘mkdir python_study‘)

                ssh. close()

                

            else:

                print ‘input error host id !‘

            

            

                        

    else:

        print ‘Your Username or password is error‘


本文出自 “尘” 博客,请务必保留此出处http://zhangpan.blog.51cto.com/2818271/1924322

简单的堡垒机模型