Python之路第一課Day9--隨堂筆記之一(Bastion Host執行個體以及資料庫操作)未完待續....

來源:互聯網
上載者:User

標籤:

一、Bastion Host前戲

開發Bastion Host之前,先來學習Python的paramiko模組,該模組機遇SSH用於串連遠程伺服器並執行相關操作

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=‘Jerry‘, password=‘123456‘)  # 執行命令stdin, stdout, stderr = ssh.exec_command(‘df‘)# 擷取命令結果result = stdout.read()print(result.decode())# 關閉串連ssh.close()
import paramikotransport = paramiko.Transport((‘hostname‘, 22))transport.connect(username=‘Jerry‘, password=‘123456‘)ssh = paramiko.SSHClient()ssh._transport = transportstdin, 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=‘192.168.1.6‘, port=22, username=‘Jerry‘, pkey=private_key) # 執行命令stdin, stdout, stderr = ssh.exec_command(‘df‘)# 擷取命令結果result = stdout.read() # 關閉串連ssh.close()
import paramikoprivate_key = paramiko.RSAKey.from_private_key_file(‘/home/auto/.ssh/id_rsa‘)transport = paramiko.Transport((‘hostname‘, 22))transport.connect(username=‘jerry‘, pkey=private_key)ssh = paramiko.SSHClient()ssh._transport = transportstdin, stdout, stderr = ssh.exec_command(‘df‘)transport.close()
SSHClient 封裝 Transport

SFTPClient

用於串連遠程伺服器並執行上傳下載

基於使用者名稱密碼上傳下載

import paramiko transport = paramiko.Transport((‘hostname‘,22))transport.connect(username=‘jerry‘,password=‘123456‘) sftp = paramiko.SFTPClient.from_transport(transport)# 將location.py 上傳至伺服器 /tmp/test.pysftp.put(‘/tmp/location.py‘, ‘/tmp/test.py‘)# 將remove_path 下載到本地 local_pathsftp.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=‘jerry‘, pkey=private_key ) sftp = paramiko.SFTPClient.from_transport(transport)# 將location.py 上傳至伺服器 /tmp/test.pysftp.put(‘/tmp/location.py‘, ‘/tmp/test.py‘)# 將remove_path 下載到本地 local_pathsftp.get(‘remove_path‘, ‘local_path‘) transport.close()
#!/usr/bin/env python# -*- coding:utf-8 -*-import paramikoimport uuidclass Haproxy(object):    def __init__(self):        self.host = ‘172.16.103.191‘        self.port = 22        self.username = ‘wupeiqi‘        self.pwd = ‘123‘        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()        self.rename()        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):        # 串連,上傳        file_name = self.create_file()        sftp = paramiko.SFTPClient.from_transport(self.__transport)        # 將location.py 上傳至伺服器 /tmp/test.py        sftp.put(file_name, ‘/home/wupeiqi/tttttttttttt.py‘)    def rename(self):        ssh = paramiko.SSHClient()        ssh._transport = self.__transport        # 執行命令        stdin, stdout, stderr = ssh.exec_command(‘mv /home/wupeiqi/tttttttttttt.py /home/wupeiqi/ooooooooo.py‘)        # 擷取命令結果        result = stdout.read()ha = Haproxy()ha.run()
Demo

 

Python之路第一課Day9--隨堂筆記之一(Bastion Host執行個體以及資料庫操作)未完待續....

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.