zoukankan      html  css  js  c++  java
  • python SQLAlchemy 堡垒机

    SQLALchemy

      ORM

        db first 数据库操作类

        code first 类操作数据库

          1、自定义生成表 class 类(base):

             列1

             列2

          根据类去创建表

                     2、使用类操作表

        以后通过类和对象操作数据库

    pramiko

    堡垒机

    ORM

        
        连表
            一对多
            
            1、创建表,主动指定外键约束
            2、操作
                    类:repr
                   单表
                        
                   连表
                        session.query(表1).join(表2).all()
            
            
            多对多
                1、创建表,额外的关系表
                2、filter()
                        ==
                        int_( 都可以是另外一个查询)
                3、relationship
                
                    A
                    AB ==> fk, 关系
                    B
                4、简单
                    
                    A   关系(B,ABTable对象)
                    AB ==>fk, 
                    B
                    操作时,简单
                
                    Table对象:
                        
                        A   关系(B,Table对象方法)
                        Table对象方法 AB ==>fk, 
                        B
                        操作时,简单
                
                
                    
                        A   关系(B,AB.__table__)
                        AB ==>fk, 
                        B
                        操作时,简单
                
        1、创建表
        2、操作表
            #单表
            
            #连表
                .join
                关系:
                    一对多
                        fk,关系
                    多对多,
                        多一张表 fk,fk
                        1、 关系表:关系
                        
                        2、A:关系,(B,AB)
    View Code
    #定义数据库
    from sqlalchemy.ext.declarative import declarative_base #创建base 后面类里要继承base
    #创建连接
    
    from sqlalchemy import Column,Integer,String,ForeignKey,UniqueConstraint,Index #表类型
    from sqlalchemy.orm import sessionmaker, relationship
    from sqlalchemy import create_engine#创建连接
    engine = create_engine("mysql+pymysql://root:@127.0.0.1:3306/s13", max_overflow=5)#创建连接,连接到自己的数据库
    
    Base = declarative_base()#继承base类
    
    #一对多
    class Group(Base):
        __tablename__ = 'group'
        nid = Column(Integer, primary_key=True,autoincrement=True)
        caption = Column(String(50))
    
    
    class User(Base):
        __tablename__ = 'user'
        nid = Column(Integer, primary_key=True,autoincrement=True)
        username = Column(String(32))
        group_id = Column(Integer, ForeignKey("group.nid"))
        group = relationship("Group",backref = "uuu")#跟host组关联的所有用户
    
        def __repr__(self): #内部方法输出不再是对象
            temp = "%s %s %s"%(self.nid,self.username,self.group_id)
            return temp
    #多对多
    class Host(Base):
        __tablename__ = 'host'
        nid = Column(Integer, primary_key=True,autoincrement=True)
        hostname = Column(String(32))
        port = Column(String(32))
        ip = Column(String(32))
    
    class HostUser(Base):
        __tablename__ = 'host_user'
        nid = Column(Integer, primary_key=True,autoincrement=True)
        username = Column(String(32))
    
    
    
    class HostToHostUser(Base):
        __tablename__ = 'host_to_host_user'
        nid = Column(Integer, primary_key=True, autoincrement=True)
    
        host_id = Column(Integer, ForeignKey('host.nid'))
        host_user_id = Column(Integer, ForeignKey('host_user.nid'))
    
        host = relationship("Host",backref='h')
        host_user = relationship("HostUser",backref='u')
    
    def init_db():
        Base.metadata.create_all(engine)
    
    def drop_db():
        Base.metadata.drop_all(engine)
    
    #init_db()
    
    Session = sessionmaker(bind=engine)
    session = Session()
    
    # session.add(Group(caption='dba'))
    # session.add(Group(caption='ddd'))
    # session.commit()
    
    # session.add_all([
    #    # User(username='alex1',group_id=1),
    #     User(username='alex2', group_id=2)
    #     #User(username='alex2',gorup_id=2)
    # ])
    # session.commit()
    
    
    
    session.add_all([
        Host(hostname='c1',port='22',ip='1.1.1.1'),
        Host(hostname='c2',port='22',ip='1.1.1.2'),
        Host(hostname='c3',port='22',ip='1.1.1.3'),
        Host(hostname='c4',port='22',ip='1.1.1.4'),
        Host(hostname='c5',port='22',ip='1.1.1.5'),
    ])
    #session.commit()
    
    
    session.add_all([
        HostUser(username='root'),
        HostUser(username='db'),
        HostUser(username='nb'),
        HostUser(username='sb'),
    ])
    #session.commit()
    
    session.add_all([
        HostToHostUser(host_id=1,host_user_id=1),
        HostToHostUser(host_id=1,host_user_id=2),
        HostToHostUser(host_id=1,host_user_id=3),
        HostToHostUser(host_id=2,host_user_id=2),
        HostToHostUser(host_id=2,host_user_id=4),
        HostToHostUser(host_id=2,host_user_id=3),
    ])
    # session.commit()
    
    """
    #原始方法,不加relationship之前
    ret = session.query(User).join(Group)
    #ret = session.query(User).join(Group,isouter=True)
    #print(ret) #查看生成的sql语句
    
    ret1 = session.query(User).join(Group).all() #表示查询两张表放在一起把数据全部查到
    #相当于 select * from user left join group on user.group_id = group.nid
    
    #print(ret1)
    #ret2 = session.query(User).join(Group,isouter=True).all() #以left方式
    #session.query(User,Group).join(Group,isouter=True).all() #以left方式把两个表中的所有数据拿出来
    #session.query(User.username,Group.caption).join(Group,isouter=True).all() #拿出相应的值
    """
    
    
    """
    #新方式反向查询
    obj = session.query(Group).filter(Group.caption == "DBA").first()
    
    print(obj.nid)
    print(obj.caption)
    print(obj.uuu) #反向查找
    """
    
    
    """
    host_obj = session.query(Host).filter(Host.hostname == 'c1').first()
    # host_obj.nid
    session.query()
    session.query(Host.nid).filter(Host.hostname == 'c1')
    
    所有用户ID
    host_obj = session.query(Host).filter(Host.hostname == 'c1').first()
    host_2_host_user = session.query(HostToHostUser.host_user_id).filter(HostToHostUser.host_id == host_obj.nid).all()
    #print(host_2_host_user)
    #[(1,),(2,),(2,)]
    r = zip(*host_2_host_user)
    #print(list(r)[0])
    #[1,2,3,]
    users = session.query(HostUser.username).filter(HostUser.nid.in_(list(r)[0])).all()
    print(users)
    """
    # 原始方式
    # session.query(HostUser.name).filter(HostUser.nid.in_(session.query(HostToHostUser.host_user_id).filter(HostToHostUser.host_id == session.query(Host.nid).filter(Host.hostname == 'c1'))))
    
    """
    host_obj = session.query(Host).filter(Host.hostname=='c1').first()
    print(host_obj.nid)
    print(host_obj.hostname)
    # 第三表对应的对象
    print(host_obj.h)
    # 循环获取的第三表对应的对象
    for item in host_obj.h:
        print(item.host_user,item.host_user.nid,item.host_user.username)
    """
    
    host_obj = session.query(Host).filter(Host.hostname=='c1').first()
    #item 代指user表的每一行数据
    for item in host_obj.h:
        print(item.host_user.username)
    

    paramiko

    paramiko模块,基于SSH用于连接远程服务器并执行相关操作。

    安装

    pip3 install paramiko
    

    使用

    SSHClient

    用于连接远程服务器并执行基本命令

    基于用户名密码连接:

    import paramiko
       
    # 创建SSH对象
    ssh = paramiko.SSHClient()
    # 允许连接不在know_hosts文件中的主机
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    # 连接服务器
    ssh.connect(hostname='c1.salt.com', port=22, username='wupeiqi', password='123')
       
    # 执行命令
    stdin, stdout, stderr = ssh.exec_command('ls')
    # 获取命令结果
    result = stdout.read()
       
    # 关闭连接
    ssh.close()
    
    import paramiko
    
    transport = paramiko.Transport(('hostname', 22))
    transport.connect(username='wupeiqi', password='123')
    
    ssh = paramiko.SSHClient()
    ssh._transport = transport #ssh里的_transport=None,我们定义为自己写的transport
    
    stdin, stdout, stderr = ssh.exec_command('df')
    print stdout.read()
    
    transport.close()
    
    SSHClient 封装 Transport
    

     基于公钥密钥连接:

    import paramiko
      
    private_key = paramiko.RSAKey.from_private_key_file('/home/auto/.ssh/id_rsa')
      
    # 创建SSH对象
    ssh = paramiko.SSHClient()
    # 允许连接不在know_hosts文件中的主机
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    # 连接服务器
    ssh.connect(hostname='c1.salt.com', port=22, username='wupeiqi', key=private_key)
      
    # 执行命令
    stdin, stdout, stderr = ssh.exec_command('df')
    # 获取命令结果
    result = stdout.read()
      
    # 关闭连接
    ssh.close()
    

     自定义的transport

    import paramiko
    
    private_key = paramiko.RSAKey.from_private_key_file('/home/auto/.ssh/id_rsa')
    
    transport = paramiko.Transport(('hostname', 22))
    transport.connect(username='wupeiqi', pkey=private_key)
    
    ssh = paramiko.SSHClient()
    ssh._transport = transport
    
    stdin, stdout, stderr = ssh.exec_command('df')
    
    transport.close()
    
    SSHClient 封装 Transport
    

    SFTPClient

    用于连接远程服务器并执行上传下载

    基于用户名密码上传下载:

    import paramiko
      
    transport = paramiko.Transport(('hostname',22))
    transport.connect(username='wupeiqi',password='123')
      
    sftp = paramiko.SFTPClient.from_transport(transport)
    # 将location.py 上传至服务器 /tmp/test.py
    sftp.put('/tmp/location.py', '/tmp/test.py')
    # 将remove_path 下载到本地 local_path
    sftp.get('remove_path', 'local_path')
      
    transport.close()
    

     基于公钥密钥上传下载:

    import paramiko
      
    private_key = paramiko.RSAKey.from_private_key_file('/home/auto/.ssh/id_rsa')
      
    transport = paramiko.Transport(('hostname', 22))
    transport.connect(username='wupeiqi', pkey=private_key )
      
    sftp = paramiko.SFTPClient.from_transport(transport)
    # 将location.py 上传至服务器 /tmp/test.py
    sftp.put('/tmp/location.py', '/tmp/test.py')
    # 将remove_path 下载到本地 local_path
    sftp.get('remove_path', 'local_path')
      
    transport.close()
    

    既可以发送命令也可以上传文件

    import paramiko
    import uuid
    
    class SSHConnection(object):
    
        def __init__(self, host='192.168.11.61', port=22, username='alex',pwd='alex3714'):
            self.host = host
            self.port = port
            self.username = username
            self.pwd = pwd
            self.__k = None
    
        def run(self):
            self.connect()
            pass
            self.close()
    
        def connect(self):
            transport = paramiko.Transport((self.host,self.port))
            transport.connect(username=self.username,password=self.pwd)
            self.__transport = transport
    
        def close(self):
            self.__transport.close()
    
        def cmd(self, command):
            ssh = paramiko.SSHClient()
            ssh._transport = self.__transport
            # 执行命令
            stdin, stdout, stderr = ssh.exec_command(command)
            # 获取命令结果
            result = stdout.read()
            return result
    
        def upload(self,local_path, target_path):
            # 连接,上传
            sftp = paramiko.SFTPClient.from_transport(self.__transport)
            # 将location.py 上传至服务器 /tmp/test.py
            sftp.put(local_path, target_path)
    
    ssh = SSHConnection()
    ssh.connect()
    r1 = ssh.cmd('df')
    ssh.upload('s2.py', "/home/alex/s7.py")
    ssh.close()
    
    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    import paramiko
    import uuid
    
    class SSHConnection(object):
    
        def __init__(self, host='172.16.103.191', port=22, username='wupeiqi',pwd='123'):
            self.host = host
            self.port = port
            self.username = username
            self.pwd = pwd
            self.__k = None
    
        def create_file(self):
            file_name = str(uuid.uuid4())
            with open(file_name,'w') as f:
                f.write('sb')
            return file_name
    
        def run(self):
            self.connect()
            self.upload('/home/wupeiqi/tttttttttttt.py')
            self.rename('/home/wupeiqi/tttttttttttt.py', '/home/wupeiqi/ooooooooo.py)
            self.close()
    
        def connect(self):
            transport = paramiko.Transport((self.host,self.port))
            transport.connect(username=self.username,password=self.pwd)
            self.__transport = transport
    
        def close(self):
    
            self.__transport.close()
    
        def upload(self,target_path):
            # 连接,上传
            file_name = self.create_file()
    
            sftp = paramiko.SFTPClient.from_transport(self.__transport)
            # 将location.py 上传至服务器 /tmp/test.py
            sftp.put(file_name, target_path)
    
        def rename(self, old_path, new_path):
    
            ssh = paramiko.SSHClient()
            ssh._transport = self.__transport
            # 执行命令
            cmd = "mv %s %s" % (old_path, new_path,)
            stdin, stdout, stderr = ssh.exec_command(cmd)
            # 获取命令结果
            result = stdout.read()
    
        def cmd(self, command):
            ssh = paramiko.SSHClient()
            ssh._transport = self.__transport
            # 执行命令
            stdin, stdout, stderr = ssh.exec_command(command)
            # 获取命令结果
            result = stdout.read()
            return result
            
    
    
    ha = SSHConnection()
    ha.run()
    
    Demo
    View Code
    # 对于更多限制命令,需要在系统中设置
    /etc/sudoers
      
    Defaults    requiretty
    Defaults:cmdb    !requiretty
    View Code

    堡垒机 

    堡垒机执行流程:

    1. 管理员为用户在服务器上创建账号(将公钥放置服务器,或者使用用户名密码)
    2. 用户登陆堡垒机,输入堡垒机用户名密码,现实当前用户管理的服务器列表
    3. 用户选择服务器,并自动登陆
    4. 执行操作并同时将用户操作记录

    注:配置.brashrc实现ssh登陆后自动执行脚本,如:vim /home/admin/.brashrc  /usr/bin/python /home/admin/menu.py logout

    /usr/bin/python /home/admin/menu.py
    logout
    

    import paramiko
    import sys
    import os
    import socket
    import select
    import getpass
     
    tran = paramiko.Transport(('10.211.55.4', 22,))
    tran.start_client()
    tran.auth_password('wupeiqi', '123')
     
    # 打开一个通道
    chan = tran.open_session()
    # 获取一个终端
    chan.get_pty()
    # 激活器
    chan.invoke_shell()
     
    #########
    # 利用sys.stdin,肆意妄为执行操作
    # 用户在终端输入内容,并将内容发送至远程服务器
    # 远程服务器执行命令,并将结果返回
    # 用户终端显示内容
    #########
     
    chan.close()
    tran.close()
    

    终端输入

    肆意妄为(一)

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    # Author:QL
    import paramiko
    import sys
    import os
    import socket
    import select
    import getpass
    from paramiko.py3compat import u #python2.7需要把这行注释
    
    tran = paramiko.Transport(('10.211.55.4', 22,))
    tran.start_client()
    tran.auth_password('wupeiqi', '123')
    
    # 打开一个通道
    chan = tran.open_session()
    # 获取一个终端
    chan.get_pty()
    # 激活器
    chan.invoke_shell()
    
    while True:
        # 监视用户输入和服务器返回数据
        # sys.stdin 处理用户输入
        # chan 是之前创建的通道,用于接收服务器返回信息
        readable, writeable, error = select.select([chan, sys.stdin, ],[],[],1)
        if chan in readable:
            try:
                #x = chan.recv(1024) #python2.7需要改成这样
                x = u(chan.recv(1024))
                if len(x) == 0:
                    print('
    *** EOF
    ')
                    break
                sys.stdout.write(x)
                sys.stdout.flush()
            except socket.timeout:
                pass
        if sys.stdin in readable:
            inp = sys.stdin.readline()
            chan.sendall(inp)
    
    chan.close()
    tran.close()
    
    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    
    import paramiko
    import sys
    import os
    import socket
    import select
    import getpass
    from paramiko.py3compat import u
    
    
    default_username = getpass.getuser()
    username = input('Username [%s]: ' % default_username)
    if len(username) == 0:
        username = default_username
    
    
    hostname = input('Hostname: ')
    if len(hostname) == 0:
        print('*** Hostname required.')
        sys.exit(1)
    
    tran = paramiko.Transport((hostname, 22,))
    tran.start_client()
    
    default_auth = "p"
    auth = input('Auth by (p)assword or (r)sa key[%s] ' % default_auth)
    if len(auth) == 0:
        auth = default_auth
    
    if auth == 'r':
        default_path = os.path.join(os.environ['HOME'], '.ssh', 'id_rsa')
        path = input('RSA key [%s]: ' % default_path)
        if len(path) == 0:
            path = default_path
        try:
            key = paramiko.RSAKey.from_private_key_file(path)
        except paramiko.PasswordRequiredException:
            password = getpass.getpass('RSA key password: ')
            key = paramiko.RSAKey.from_private_key_file(path, password)
        tran.auth_publickey(username, key)
    else:
        pw = getpass.getpass('Password for %s@%s: ' % (username, hostname))
        tran.auth_password(username, pw)
    
    
    
    # 打开一个通道
    chan = tran.open_session()
    # 获取一个终端
    chan.get_pty()
    # 激活器
    chan.invoke_shell()
    
    while True:
        # 监视用户输入和服务器返回数据
        # sys.stdin 处理用户输入
        # chan 是之前创建的通道,用于接收服务器返回信息
        readable, writeable, error = select.select([chan, sys.stdin, ],[],[],1)
        if chan in readable:
            try:
                x = u(chan.recv(1024))
                if len(x) == 0:
                    print('
    *** EOF
    ')
                    break
                sys.stdout.write(x)
                sys.stdout.flush()
            except socket.timeout:
                pass
        if sys.stdin in readable:
            inp = sys.stdin.readline()
            chan.sendall(inp)
    
    chan.close()
    tran.close()
    
    完整示例(一)
    完整示例一

    肆意妄为(二)

    import paramiko
    import sys
    import os
    import socket
    import select
    import getpass
    import termios
    import tty
    from paramiko.py3compat import u
     
    tran = paramiko.Transport(('10.211.55.4', 22,))
    tran.start_client()
    tran.auth_password('wupeiqi', '123')
     
    # 打开一个通道
    chan = tran.open_session()
    # 获取一个终端
    chan.get_pty()
    # 激活器
    chan.invoke_shell()
     
     
    # 获取原tty属性
    oldtty = termios.tcgetattr(sys.stdin)
    try:
        # 为tty设置新属性
        # 默认当前tty设备属性:
        #   输入一行回车,执行
        #   CTRL+C 进程退出,遇到特殊字符,特殊处理。
     
        # 这是为原始模式,不认识所有特殊符号
        # 放置特殊字符应用在当前终端,如此设置,将所有的用户输入均发送到远程服务器
        tty.setraw(sys.stdin.fileno())
        chan.settimeout(0.0)
     
        while True:
            # 监视 用户输入 和 远程服务器返回数据(socket)
            # 阻塞,直到句柄可读
            r, w, e = select.select([chan, sys.stdin], [], [], 1)
            if chan in r:
                try:
                    x = u(chan.recv(1024))
                    if len(x) == 0:
                        print('
    *** EOF
    ')
                        break
                    sys.stdout.write(x)
                    sys.stdout.flush()
                except socket.timeout:
                    pass
            if sys.stdin in r:
                x = sys.stdin.read(1)
                if len(x) == 0:
                    break
                chan.send(x)
     
    finally:
        # 重新设置终端属性
        termios.tcsetattr(sys.stdin, termios.TCSADRAIN, oldtty)
     
     
    chan.close()
    tran.close()
    
    import paramiko
    import sys
    import os
    import socket
    import select
    import getpass
    import termios
    import tty
    from paramiko.py3compat import u
    
    
    default_username = getpass.getuser()
    username = input('Username [%s]: ' % default_username)
    if len(username) == 0:
        username = default_username
    
    
    hostname = input('Hostname: ')
    if len(hostname) == 0:
        print('*** Hostname required.')
        sys.exit(1)
    
    tran = paramiko.Transport((hostname, 22,))
    tran.start_client()
    
    default_auth = "p"
    auth = input('Auth by (p)assword or (r)sa key[%s] ' % default_auth)
    if len(auth) == 0:
        auth = default_auth
    
    if auth == 'r':
        default_path = os.path.join(os.environ['HOME'], '.ssh', 'id_rsa')
        path = input('RSA key [%s]: ' % default_path)
        if len(path) == 0:
            path = default_path
        try:
            key = paramiko.RSAKey.from_private_key_file(path)
        except paramiko.PasswordRequiredException:
            password = getpass.getpass('RSA key password: ')
            key = paramiko.RSAKey.from_private_key_file(path, password)
        tran.auth_publickey(username, key)
    else:
        pw = getpass.getpass('Password for %s@%s: ' % (username, hostname))
        tran.auth_password(username, pw)
    
    # 打开一个通道
    chan = tran.open_session()
    # 获取一个终端
    chan.get_pty()
    # 激活器
    chan.invoke_shell()
    
    
    # 获取原tty属性
    oldtty = termios.tcgetattr(sys.stdin)
    try:
        # 为tty设置新属性
        # 默认当前tty设备属性:
        #   输入一行回车,执行
        #   CTRL+C 进程退出,遇到特殊字符,特殊处理。
    
        # 这是为原始模式,不认识所有特殊符号
        # 放置特殊字符应用在当前终端,如此设置,将所有的用户输入均发送到远程服务器
        tty.setraw(sys.stdin.fileno())
        chan.settimeout(0.0)
    
        while True:
            # 监视 用户输入 和 远程服务器返回数据(socket)
            # 阻塞,直到句柄可读
            r, w, e = select.select([chan, sys.stdin], [], [], 1)
            if chan in r:
                try:
                    x = u(chan.recv(1024))
                    if len(x) == 0:
                        print('
    *** EOF
    ')
                        break
                    sys.stdout.write(x)
                    sys.stdout.flush()
                except socket.timeout:
                    pass
            if sys.stdin in r:
                x = sys.stdin.read(1)
                if len(x) == 0:
                    break
                chan.send(x)
    
    finally:
        # 重新设置终端属性
        termios.tcsetattr(sys.stdin, termios.TCSADRAIN, oldtty)
    
    
    chan.close()
    tran.close()
    
    绝对不改版本
    完整示例二
    import paramiko
    import sys
    import os
    import socket
    import getpass
    
    from paramiko.py3compat import u
    
    # windows does not have termios...
    try:
        import termios
        import tty
        has_termios = True
    except ImportError:
        has_termios = False
    
    
    def interactive_shell(chan):
        if has_termios:
            posix_shell(chan)
        else:
            windows_shell(chan)
    
    
    def posix_shell(chan):
        import select
    
        oldtty = termios.tcgetattr(sys.stdin)
        try:
            tty.setraw(sys.stdin.fileno())
            tty.setcbreak(sys.stdin.fileno())
            chan.settimeout(0.0)
            log = open('handle.log', 'a+', encoding='utf-8')
            flag = False
            temp_list = []
            while True:
                r, w, e = select.select([chan, sys.stdin], [], [])
                if chan in r:
                    try:
                        x = u(chan.recv(1024))
                        if len(x) == 0:
                            sys.stdout.write('
    *** EOF
    ')
                            break
                        if flag:
                            if x.startswith('
    '):
                                pass
                            else:
                                temp_list.append(x)
                            flag = False
                        sys.stdout.write(x)
                        sys.stdout.flush()
                    except socket.timeout:
                        pass
                if sys.stdin in r:
                    x = sys.stdin.read(1)
                    import json
    
                    if len(x) == 0:
                        break
    
                    if x == '	':
                        flag = True
                    else:
                        temp_list.append(x)
                    if x == '
    ':
                        log.write(''.join(temp_list))
                        log.flush()
                        temp_list.clear()
                    chan.send(x)
    
        finally:
            termios.tcsetattr(sys.stdin, termios.TCSADRAIN, oldtty)
    
    
    def windows_shell(chan):
        import threading
    
        sys.stdout.write("Line-buffered terminal emulation. Press F6 or ^Z to send EOF.
    
    ")
    
        def writeall(sock):
            while True:
                data = sock.recv(256)
                if not data:
                    sys.stdout.write('
    *** EOF ***
    
    ')
                    sys.stdout.flush()
                    break
                sys.stdout.write(data)
                sys.stdout.flush()
    
        writer = threading.Thread(target=writeall, args=(chan,))
        writer.start()
    
        try:
            while True:
                d = sys.stdin.read(1)
                if not d:
                    break
                chan.send(d)
        except EOFError:
            # user hit ^Z or F6
            pass
    
    
    def run():
        tran = paramiko.Transport(('10.211.55.4', 22,))
        tran.start_client()
        tran.auth_password('wupeiqi', '123')
    
        # 打开一个通道
        chan = tran.open_session()
        # 获取一个终端
        chan.get_pty()
        # 激活器
        chan.invoke_shell()
    
        interactive_shell(chan)
    
        chan.close()
        tran.close()
    
    
    if __name__ == '__main__':
        run()
    
    打死也不改版本
    打死也不改版本
    import paramiko
    import sys
    import os
    import socket
    import getpass
    
    from paramiko.py3compat import u
    
    # windows does not have termios...
    try:
        import termios
        import tty
        has_termios = True
    except ImportError:
        has_termios = False
    
    
    def interactive_shell(chan):
        if has_termios:
            posix_shell(chan)
        else:
            windows_shell(chan)
    
    
    def posix_shell(chan):
        import select
    
        oldtty = termios.tcgetattr(sys.stdin)
        try:
            tty.setraw(sys.stdin.fileno())
            tty.setcbreak(sys.stdin.fileno())
            chan.settimeout(0.0)
            log = open('handle.log', 'a+', encoding='utf-8')
            flag = False
            temp_list = []
            while True:
                r, w, e = select.select([chan, sys.stdin], [], [])
                if chan in r:
                    try:
                        x = u(chan.recv(1024))
                        if len(x) == 0:
                            sys.stdout.write('
    *** EOF
    ')
                            break
                        if flag:
                            if x.startswith('
    '):
                                pass
                            else:
                                temp_list.append(x)
                            flag = False
                        sys.stdout.write(x)
                        sys.stdout.flush()
                    except socket.timeout:
                        pass
                if sys.stdin in r:
                    x = sys.stdin.read(1)
                    import json
    
                    if len(x) == 0:
                        break
    
                    if x == '	':
                        flag = True
                    else:
                        temp_list.append(x)
                    if x == '
    ':
                        log.write(''.join(temp_list))
                        log.flush()
                        temp_list.clear()
                    chan.send(x)
    
        finally:
            termios.tcsetattr(sys.stdin, termios.TCSADRAIN, oldtty)
    
    
    def windows_shell(chan):
        import threading
    
        sys.stdout.write("Line-buffered terminal emulation. Press F6 or ^Z to send EOF.
    
    ")
    
        def writeall(sock):
            while True:
                data = sock.recv(256)
                if not data:
                    sys.stdout.write('
    *** EOF ***
    
    ')
                    sys.stdout.flush()
                    break
                sys.stdout.write(data)
                sys.stdout.flush()
    
        writer = threading.Thread(target=writeall, args=(chan,))
        writer.start()
    
        try:
            while True:
                d = sys.stdin.read(1)
                if not d:
                    break
                chan.send(d)
        except EOFError:
            # user hit ^Z or F6
            pass
    
    
    def run():
        default_username = getpass.getuser()
        username = input('Username [%s]: ' % default_username)
        if len(username) == 0:
            username = default_username
    
    
        hostname = input('Hostname: ')
        if len(hostname) == 0:
            print('*** Hostname required.')
            sys.exit(1)
    
        tran = paramiko.Transport((hostname, 22,))
        tran.start_client()
    
        default_auth = "p"
        auth = input('Auth by (p)assword or (r)sa key[%s] ' % default_auth)
        if len(auth) == 0:
            auth = default_auth
    
        if auth == 'r':
            default_path = os.path.join(os.environ['HOME'], '.ssh', 'id_rsa')
            path = input('RSA key [%s]: ' % default_path)
            if len(path) == 0:
                path = default_path
            try:
                key = paramiko.RSAKey.from_private_key_file(path)
            except paramiko.PasswordRequiredException:
                password = getpass.getpass('RSA key password: ')
                key = paramiko.RSAKey.from_private_key_file(path, password)
            tran.auth_publickey(username, key)
        else:
            pw = getpass.getpass('Password for %s@%s: ' % (username, hostname))
            tran.auth_password(username, pw)
    
        # 打开一个通道
        chan = tran.open_session()
        # 获取一个终端
        chan.get_pty()
        # 激活器
        chan.invoke_shell()
    
        interactive_shell(chan)
    
        chan.close()
        tran.close()
    
    
    if __name__ == '__main__':
        run()
    骨灰级

     完整版

    import paramiko
    import sys
    import os
    import socket
    import getpass
    
    # from paramiko.py3compat import u
    
    # windows does not have termios...
    try:
        import termios
        import tty
        has_termios = True
    except ImportError:
        has_termios = False
    
    def interactive_shell(chan):
        if has_termios:
            posix_shell(chan)
        else:
            windows_shell(chan)
    
    
    def posix_shell(chan):
        import select
    
        oldtty = termios.tcgetattr(sys.stdin)
        try:
            tty.setraw(sys.stdin.fileno())
            tty.setcbreak(sys.stdin.fileno())
            chan.settimeout(0.0)
            f = open('handle.log','a+')
            tab_flag = False
            temp_list = []
            while True:
                r, w, e = select.select([chan, sys.stdin], [], [])
                if chan in r:
                    try:
                        x = chan.recv(1024)
                        if len(x) == 0:
                            sys.stdout.write('
    *** EOF
    ')
                            break
                        if tab_flag:
                            if x.startswith('
    '):
                                pass
                            else:
                                f.write(x)
                                f.flush()
                            tab_flag = False
                        sys.stdout.write(x)
                        sys.stdout.flush()
                    except socket.timeout:
                        pass
                if sys.stdin in r:
                    x = sys.stdin.read(1)
                    if len(x) == 0:
                        break
                    if x == '	':
                        tab_flag = True
                    else:
                        f.write(x)
                        f.flush()
                    chan.send(x)
    
        finally:
            termios.tcsetattr(sys.stdin, termios.TCSADRAIN, oldtty)
    
    
    def windows_shell(chan):
        import threading
    
        sys.stdout.write("Line-buffered terminal emulation. Press F6 or ^Z to send EOF.
    
    ")
    
        def writeall(sock):
            while True:
                data = sock.recv(256)
                if not data:
                    sys.stdout.write('
    *** EOF ***
    
    ')
                    sys.stdout.flush()
                    break
                sys.stdout.write(data)
                sys.stdout.flush()
    
        writer = threading.Thread(target=writeall, args=(chan,))
        writer.start()
    
        try:
            while True:
                d = sys.stdin.read(1)
                if not d:
                    break
                chan.send(d)
        except EOFError:
            # user hit ^Z or F6
            pass
    
    
    def run():
        # 获取当前登录用户
    
    
        host_list = [
            {'host': "192.168.11.139", 'username': 'oldboy', 'pwd': "123"},
            {'host': "192.168.11.138", 'username': 'oldboy', 'pwd': "123"},
            {'host': "192.168.11.137", 'username': 'oldboy', 'pwd': "123"},
        ]
        for item in enumerate(host_list, 1):
            print(item['host'])
    
        num = raw_input('序号:')
        sel_host = host_list[int(num) -1]
        hostname = sel_host['host']
        username = sel_host['username']
        pwd = sel_host['pwd']
        print(hostname,username,pwd)
    
        tran = paramiko.Transport((hostname, 22,))
        tran.start_client()
        tran.auth_password(username, pwd)
        # 打开一个通道
        chan = tran.open_session()
        # 获取一个终端
        chan.get_pty()
        # 激活器
        chan.invoke_shell()
    
        interactive_shell(chan)
    
        chan.close()
        tran.close()
    
    
    if __name__ == '__main__':
        run(
    

    数据库表

    from sqlalchemy import create_engine,and_,or_,func,Table
    from sqlalchemy.ext.declarative import declarative_base
    from sqlalchemy import Column, Integer, String,ForeignKey,UniqueConstraint,DateTime
    from  sqlalchemy.orm import sessionmaker,relationship
    
    Base = declarative_base() #生成一个SqlORM 基类
    
    # 程序登陆用户和服务器账户,一个人可以有多个服务器账号,一个服务器账号可以给多个人用
    UserProfile2HostUser= Table('userprofile_2_hostuser',Base.metadata,
        Column('userprofile_id',ForeignKey('user_profile.id'),primary_key=True),
        Column('hostuser_id',ForeignKey('host_user.id'),primary_key=True),
    )
    
    
    class Host(Base):
        __tablename__='host'
        id = Column(Integer,primary_key=True,autoincrement=True)
        hostname = Column(String(64),unique=True,nullable=False)
        ip_addr = Column(String(128),unique=True,nullable=False)
        port = Column(Integer,default=22)
    
        
    class HostUser(Base):
        __tablename__ = 'host_user'
        id = Column(Integer,primary_key=True)
        AuthTypes = [
            (u'ssh-passwd',u'SSH/Password'),
            (u'ssh-key',u'SSH/KEY'),
        ]
        auth_type = Column(String(64))
        username = Column(String(64),unique=True,nullable=False)
        password = Column(String(255))
    
        host_id = Column(Integer,ForeignKey('host.id'))
        
        host = relationship('Host',backref='uu')
        
        __table_args__ = (UniqueConstraint('host_id','username', name='_host_username_uc'),)
    
    
            
    #  
    # obj = session.query(HostUser.username,HostUser.password,Host.hostname,Host.port).join(Host).filter(HostUser.id == 1).first()
    # (用户名,密码,主机名,端口)
    
    class UserProfile(Base):
        __tablename__ = 'user_profile'
        id = Column(Integer,primary_key=True)
        username = Column(String(64),unique=True,nullable=False)
        password = Column(String(255),nullable=False)
        # 如果是一个人只能在一个组下
        group_id = Column(Integer,ForeignKey('group.id'))
        
        host_list = relationship('HostUser',secondary=UserProfile2HostUser,backref='userprofiles')
    
    """
    # 输入用户名和密码:
    # 
    obj = session.query(UserProfile).filter(username=输入的用户名, password=输入的密码).first()
    if not obj:
        # 堡垒机登录用户对象
        # 输入这个人的所有机器
        obj.host_list # 当前堡垒机登录用户,所有的服务器用户名
        # 
        for item  in obj.host_list:
            # item,是一个HostUser对象
            item.password,item.username, 
            # item.host 对象,host对象
            item.host.hostname,item.host.port
        # item 目标机器HostUser对象
        host_obj = input(:...)
        session.add(AuditLog(userprofile_id=obj.id,hostuser_id = host_obj.id, "ifconfig"))
    """
    
    class AuditLog(Base):
        __tablename__ = 'audit_log'
        id = Column(Integer,primary_key=True)
        userprofile_id = Column(Integer,ForeignKey('user_profile.id'))
        hostuser_id = Column(Integer,ForeignKey('host_user.id'))
    
        cmd = Column(String(255))
        date = Column(DateTime)
        
    
    """
    class Session:
        session = None
        def __init__():
            engine = create_engine("mysql+pymysql://root:123@127.0.0.1:3306/s13", max_overflow=5)
            ss = sessionmaker(bind=engine)
            obj = ss()
            Session.session = obj
            
        @classmethod    
        def instance(cls):
            if not cls.session:
                cls()
            return cls.session
    """
    View Code

     http://www.cnblogs.com/wupeiqi/articles/5699254.html

  • 相关阅读:
    css学习笔记1
    HTML学习笔记4
    Layui使用入门教程
    MVC _Layout
    C# MVC _viewstart.cshtml的作用
    用javascript提交表单
    form表单中的属性和标签
    <input>标签中id和name的作用和区别
    input type = button 的onclick属性调用函数
    form表单提交onclick和onsubmit
  • 原文地址:https://www.cnblogs.com/QL8533/p/5733907.html
Copyright © 2011-2022 走看看