这几天在欺负docker兽,很是开心,但是没想到领导突然让我去殴打openstack兽,完全没有兴趣嘛,毕竟前一阵一直在吊打它,当然啦,对于愚蠢的人类而言openstack兽是十分强大的,不过对于北方来说就不是酱了老司机带你飞啊~~~过几天还要给愚蠢的人类讲云计算,我靠,这么简单的东西还需要讲?讲也不应该由北方大人来讲好不好,简单写点吧,刚才为了愚蠢的人类们写了2小时openstack的安装文档,累死萌萌了,下面该短暂的学习前端知识了
paramiko模块
直接完整版本
1 #!/usr/bin/env python 2 import paramiko 3 4 5 class SSHConnection(object): 6 def __init__(self, host=‘172.16.5.7‘, port=22, username=‘root‘,pwd=‘123qweASD‘): 7 """ 8 信息初始化 9 :return: 10 """ 11 self.host = host 12 self.port = port 13 self.username = username 14 self.pwd = pwd 15 self.__k = None 16 17 def run(self): 18 self.connect() 19 pass 20 self.close() 21 22 def connect(self): 23 """ 24 创建连接 25 :return: 26 """ 27 transport = paramiko.Transport((self.host,self.port)) 28 transport.connect(username=self.username,password=self.pwd) 29 self.__transport = transport 30 31 def close(self): 32 """ 33 关闭连接 34 :return: 35 """ 36 self.__transport.close() 37 38 def cmd(self, command): 39 """ 40 命令执行方法 41 :return: 42 """ 43 ssh = paramiko.SSHClient() 44 ssh._transport = self.__transport 45 stdin, stdout, stderr = ssh.exec_command(command) 46 result = stdout.read() 47 print(result.decode()) 48 49 def upload(self,local_path, target_path): 50 """ 51 上传操作方法 52 :return: 53 """ 54 sftp = paramiko.SFTPClient.from_transport(self.__transport) 55 sftp.put(local_path, target_path) 56 57 ssh = SSHConnection() 58 ssh.connect() 59 # ssh.cmd(‘df‘) 60 # ssh.upload(‘s2.py‘, "/root/sss.py") 61 ssh.cmd(‘ls -l‘) 62 ssh.close()
数据库的,从单表练习开始,再用一对多,最后多对多
1 #!/usr/bin/env python 2 from sqlalchemy import create_engine 3 from sqlalchemy.ext.declarative import declarative_base 4 from sqlalchemy import Column, Integer, String, ForeignKey, UniqueConstraint, Index 5 from sqlalchemy.orm import sessionmaker, relationship 6 engine = create_engine(‘mysql+pymysql://test:[email protected]/s13‘, max_overflow=5) 7 8 Base = declarative_base() 9 10 11 class HostToHostUser(Base): 12 """ 13 定义host_to_host_user表的类 14 """ 15 __tablename__ = ‘host_to_host_user‘ 16 nid = Column(Integer, primary_key=True, autoincrement=True) 17 host_id = Column(Integer, ForeignKey(‘host.nid‘)) 18 host_user_id = Column(Integer, ForeignKey(‘host_user.nid‘)) 19 20 host = relationship(‘Host‘, backref=‘h‘) 21 host_user = relationship(‘HostUser‘, backref=‘u‘) 22 23 24 class Host(Base): 25 """ 26 定义host表的类 27 """ 28 __tablename__ = ‘host‘ 29 nid = Column(Integer, primary_key=True, autoincrement=True) 30 hostname = Column(String(32)) 31 port = Column(String(32)) 32 ip = Column(String(32)) 33 34 host_user = relationship(‘HostUser‘, secondary=HostToHostUser.__table__, backref=‘h‘) 35 36 37 class HostUser(Base): 38 """ 39 定义host_user表的类 40 """ 41 __tablename__ = ‘host_user‘ 42 nid = Column(Integer, primary_key=True, autoincrement=True) 43 username = Column(String(32)) 44 45 46 def init_db(): 47 """ 48 表创建函数 49 :return: 50 """ 51 Base.metadata.create_all(engine) 52 53 54 def drop_db(): 55 """ 56 表删除函数 57 :return: 58 """ 59 Base.metadata.drop_all(engine) 60 61 # init_db() 62 # drop_db() 63 64 Session = sessionmaker(bind=engine) # 建立会话保持连接 65 session = Session() 66 67 # 插入相关数据 68 # session.add_all([ 69 # Host(hostname=‘c1‘, port=‘22‘, ip=‘1.1.1.1‘), 70 # Host(hostname=‘c2‘, port=‘22‘, ip=‘1.1.1.2‘), 71 # Host(hostname=‘c3‘, port=‘22‘, ip=‘1.1.1.3‘), 72 # Host(hostname=‘c4‘, port=‘22‘, ip=‘1.1.1.4‘), 73 # Host(hostname=‘c5‘, port=‘22‘, ip=‘1.1.1.5‘), 74 # HostUser(username=‘root‘), 75 # HostUser(username=‘nb‘), 76 # HostUser(username=‘db‘), 77 # HostUser(username=‘sb‘), 78 # HostToHostUser(host_id=1, host_user_id=1), 79 # HostToHostUser(host_id=1, host_user_id=2), 80 # HostToHostUser(host_id=1, host_user_id=3), 81 # HostToHostUser(host_id=2, host_user_id=2), 82 # HostToHostUser(host_id=2, host_user_id=3), 83 # HostToHostUser(host_id=2, host_user_id=4), 84 # ]) 85 # 86 # session.commit() 87 # host_obj = session.query(Host).filter(Host.hostname == ‘c1‘).first() 88 # host_2_host_user = session.query(HostToHostUser.host_user_id).filter(HostToHostUser.host_id == host_obj.nid).all() 89 # r = zip(*host_2_host_user) 90 # users = session.query(HostUser.username).filter(HostUser.nid.in_(list(r)[0])).all() 91 # print(users) 92 93 host_obj = session.query(Host).filter(Host.hostname == ‘c1‘).first() 94 # for i in host_obj.h: 95 # print(i.host_user.username) 96 97 print(host_obj.host_user)
时间: 2024-10-14 10:32:33