day13 paramiko、数据库表操作
Paramiko
paramiko 模塊官方網(wǎng)站:
http://www.paramiko.org/
paramiko 模塊功能概述:
Paramiko 是一個python(2.6+,3.3+)的實現(xiàn)SSHv2協(xié)議的模塊,它提供了客戶端和服務器端的功能。雖然利用Python C擴展為低水平加密(加密),Paramiko本身就是一個純Python接口在SSH網(wǎng)絡概念。
paramiko 模塊安裝方式:
pip3 install paramiko
sshclient
用于連接遠程服務器并執(zhí)行相應的命令
sshclien連接服務器的兩種方式:
1、基于用戶名密碼連接:
import paramiko # 創(chuàng)建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') # 執(zhí)行命令 
stdin, stdout, stderr = ssh.exec_command('ls') 
# 獲取命令結果 
result = stdout.read() # 關閉連接 
ssh.close() 
   1 import paramiko 2 3 # 創(chuàng)建SSH對象 4 ssh = paramiko.SSHClient() 5 # 允許連接不在know_hosts文件中的主機 6 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 7 # 連接服務器 8 ssh.connect(hostname='c1.salt.com', port=22, username='wupeiqi', password='123') 9 10 # 執(zhí)行命令 11 stdin, stdout, stderr = ssh.exec_command('ls') 12 # 獲取命令結果 13 result = stdout.read() 14 15 # 關閉連接 16 ssh.close()sshclient 封裝 Transport
2、基于公鑰秘鑰連接:
import paramiko private_key = paramiko.RSAKey.from_private_key_file('/home/auto/.ssh/id_rsa') # 創(chuàng)建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) # 執(zhí)行命令 
stdin, stdout, stderr = ssh.exec_command('df') 
# 獲取命令結果 
result = stdout.read() # 關閉連接 
ssh.close() 
   1 import paramiko 2 3 private_key = paramiko.RSAKey.from_private_key_file('/home/auto/.ssh/id_rsa') 4 5 transport = paramiko.Transport(('hostname', 22)) 6 transport.connect(username='wupeiqi', pkey=private_key) 7 8 ssh = paramiko.SSHClient() 9 ssh._transport = transport 10 11 stdin, stdout, stderr = ssh.exec_command('df') 12 13 transport.close()sshclient 封裝 Transport
?sftpclient:
用于連接遠程服務器并執(zhí)行上傳下載
1、基于用戶名密碼上傳下載:
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() 
  2、基于公鑰密鑰上傳下載:
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()
   1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 import paramiko 4 import uuid 5 6 class SSHConnection(object): 7 8 def __init__(self, host='172.16.103.191', port=22, username='wupeiqi',pwd='123'): 9 self.host = host 10 self.port = port 11 self.username = username 12 self.pwd = pwd 13 self.__k = None 14 15 def create_file(self): 16 file_name = str(uuid.uuid4()) 17 with open(file_name,'w') as f: 18 f.write('sb') 19 return file_name 20 21 def run(self): 22 self.connect() 23 self.upload('/home/wupeiqi/tttttttttttt.py') 24 self.rename('/home/wupeiqi/tttttttttttt.py', '/home/wupeiqi/ooooooooo.py) 25 self.close() 26 27 def connect(self): 28 transport = paramiko.Transport((self.host,self.port)) 29 transport.connect(username=self.username,password=self.pwd) 30 self.__transport = transport 31 32 def close(self): 33 34 self.__transport.close() 35 36 def upload(self,target_path): 37 # 連接,上傳 38 file_name = self.create_file() 39 40 sftp = paramiko.SFTPClient.from_transport(self.__transport) 41 # 將location.py 上傳至服務器 /tmp/test.py 42 sftp.put(file_name, target_path) 43 44 def rename(self, old_path, new_path): 45 46 ssh = paramiko.SSHClient() 47 ssh._transport = self.__transport 48 # 執(zhí)行命令 49 cmd = "mv %s %s" % (old_path, new_path,) 50 stdin, stdout, stderr = ssh.exec_command(cmd) 51 # 獲取命令結果 52 result = stdout.read() 53 54 def cmd(self, command): 55 ssh = paramiko.SSHClient() 56 ssh._transport = self.__transport 57 # 執(zhí)行命令 58 stdin, stdout, stderr = ssh.exec_command(command) 59 # 獲取命令結果 60 result = stdout.read() 61 return result 62 63 64 65 ha = SSHConnection() 66 ha.run()demo
# 對于更多限制命令,需要在系統(tǒng)中設置 
/etc/sudoers Defaults    requiretty 
Defaults:cmdb    !requiretty   1 import paramiko 2 import uuid 3 4 class SSHConnection(object): 5 6 def __init__(self, host='192.168.11.61', port=22, username='alex',pwd='alex3714'): 7 self.host = host 8 self.port = port 9 self.username = username 10 self.pwd = pwd 11 self.__k = None 12 13 def run(self): 14 self.connect() 15 pass 16 self.close() 17 18 def connect(self): 19 transport = paramiko.Transport((self.host,self.port)) 20 transport.connect(username=self.username,password=self.pwd) 21 self.__transport = transport 22 23 def close(self): 24 self.__transport.close() 25 26 def cmd(self, command): 27 ssh = paramiko.SSHClient() 28 ssh._transport = self.__transport 29 # 執(zhí)行命令 30 stdin, stdout, stderr = ssh.exec_command(command) 31 # 獲取命令結果 32 result = stdout.read() 33 return result 34 35 def upload(self,local_path, target_path): 36 # 連接,上傳 37 sftp = paramiko.SFTPClient.from_transport(self.__transport) 38 # 將location.py 上傳至服務器 /tmp/test.py 39 sftp.put(local_path, target_path) 40 41 ssh = SSHConnection() 42 ssh.connect() 43 r1 = ssh.cmd('df') 44 ssh.upload('s2.py', "/home/alex/s7.py") 45 ssh.close()demo
?堡壘機:
堡壘機模型:
堡壘機原理和執(zhí)行流程:
1、管理員為用戶在服務器上創(chuàng)建賬號(將公鑰放置服務器,或者使用用戶名密碼)
2、用戶登入堡壘機,輸入堡壘機用戶名密碼,實現(xiàn)當前用戶管理的服務器列表
3、用戶選擇服務器,并自動登入
4、執(zhí)行操作并同時將用戶操作記錄
實現(xiàn)過程:
前奏
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,肆意妄為執(zhí)行操作 
# 用戶在終端輸入內容,并將內容發(fā)送至遠程服務器 
# 遠程服務器執(zhí)行命令,并將結果返回 
# 用戶終端顯示內容 
######### chan.close() 
tran.close()
  2、終極版本:
import paramiko 
import sys 
import os 
import socket 
import select 
import getpass 
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() while True: # 監(jiān)視用戶輸入和服務器返回數(shù)據(jù) # sys.stdin 處理用戶輸入 # chan 是之前創(chuàng)建的通道,用于接收服務器返回信息 readable, writeable, error = select.select([chan, sys.stdin, ],[],[],1) if chan in readable: try: x = u(chan.recv(1024)) if len(x) == 0: print('\r\n*** EOF\r\n') breaksys.stdout.write(x) sys.stdout.flush() except socket.timeout: passif sys.stdin in readable: inp = sys.stdin.readline() chan.sendall(inp) chan.close() 
tran.close()   1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 4 import paramiko 5 import sys 6 import os 7 import socket 8 import select 9 import getpass 10 from paramiko.py3compat import u 11 12 13 default_username = getpass.getuser() 14 username = input('Username [%s]: ' % default_username) 15 if len(username) == 0: 16 username = default_username 17 18 19 hostname = input('Hostname: ') 20 if len(hostname) == 0: 21 print('*** Hostname required.') 22 sys.exit(1) 23 24 tran = paramiko.Transport((hostname, 22,)) 25 tran.start_client() 26 27 default_auth = "p" 28 auth = input('Auth by (p)assword or (r)sa key[%s] ' % default_auth) 29 if len(auth) == 0: 30 auth = default_auth 31 32 if auth == 'r': 33 default_path = os.path.join(os.environ['HOME'], '.ssh', 'id_rsa') 34 path = input('RSA key [%s]: ' % default_path) 35 if len(path) == 0: 36 path = default_path 37 try: 38 key = paramiko.RSAKey.from_private_key_file(path) 39 except paramiko.PasswordRequiredException: 40 password = getpass.getpass('RSA key password: ') 41 key = paramiko.RSAKey.from_private_key_file(path, password) 42 tran.auth_publickey(username, key) 43 else: 44 pw = getpass.getpass('Password for %s@%s: ' % (username, hostname)) 45 tran.auth_password(username, pw) 46 47 48 49 # 打開一個通道 50 chan = tran.open_session() 51 # 獲取一個終端 52 chan.get_pty() 53 # 激活器 54 chan.invoke_shell() 55 56 while True: 57 # 監(jiān)視用戶輸入和服務器返回數(shù)據(jù) 58 # sys.stdin 處理用戶輸入 59 # chan 是之前創(chuàng)建的通道,用于接收服務器返回信息 60 readable, writeable, error = select.select([chan, sys.stdin, ],[],[],1) 61 if chan in readable: 62 try: 63 x = u(chan.recv(1024)) 64 if len(x) == 0: 65 print('\r\n*** EOF\r\n') 66 break 67 sys.stdout.write(x) 68 sys.stdout.flush() 69 except socket.timeout: 70 pass 71 if sys.stdin in readable: 72 inp = sys.stdin.readline() 73 chan.sendall(inp) 74 75 chan.close() 76 tran.close()完整示例
?3、終極版本二
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設備屬性: #   輸入一行回車,執(zhí)行 #   CTRL+C 進程退出,遇到特殊字符,特殊處理。 # 這是為原始模式,不認識所有特殊符號 # 放置特殊字符應用在當前終端,如此設置,將所有的用戶輸入均發(fā)送到遠程服務器 tty.setraw(sys.stdin.fileno()) chan.settimeout(0.0) while True: # 監(jiān)視 用戶輸入 和 遠程服務器返回數(shù)據(jù)(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('\r\n*** EOF\r\n') breaksys.stdout.write(x) sys.stdout.flush() except socket.timeout: passif sys.stdin in r: x = sys.stdin.read(1) if len(x) == 0: breakchan.send(x) finally: # 重新設置終端屬性 termios.tcsetattr(sys.stdin, termios.TCSADRAIN, oldtty) chan.close() 
tran.close()
   1 import paramiko 2 import sys 3 import os 4 import socket 5 import select 6 import getpass 7 import termios 8 import tty 9 from paramiko.py3compat import u 10 11 12 default_username = getpass.getuser() 13 username = input('Username [%s]: ' % default_username) 14 if len(username) == 0: 15 username = default_username 16 17 18 hostname = input('Hostname: ') 19 if len(hostname) == 0: 20 print('*** Hostname required.') 21 sys.exit(1) 22 23 tran = paramiko.Transport((hostname, 22,)) 24 tran.start_client() 25 26 default_auth = "p" 27 auth = input('Auth by (p)assword or (r)sa key[%s] ' % default_auth) 28 if len(auth) == 0: 29 auth = default_auth 30 31 if auth == 'r': 32 default_path = os.path.join(os.environ['HOME'], '.ssh', 'id_rsa') 33 path = input('RSA key [%s]: ' % default_path) 34 if len(path) == 0: 35 path = default_path 36 try: 37 key = paramiko.RSAKey.from_private_key_file(path) 38 except paramiko.PasswordRequiredException: 39 password = getpass.getpass('RSA key password: ') 40 key = paramiko.RSAKey.from_private_key_file(path, password) 41 tran.auth_publickey(username, key) 42 else: 43 pw = getpass.getpass('Password for %s@%s: ' % (username, hostname)) 44 tran.auth_password(username, pw) 45 46 # 打開一個通道 47 chan = tran.open_session() 48 # 獲取一個終端 49 chan.get_pty() 50 # 激活器 51 chan.invoke_shell() 52 53 54 # 獲取原tty屬性 55 oldtty = termios.tcgetattr(sys.stdin) 56 try: 57 # 為tty設置新屬性 58 # 默認當前tty設備屬性: 59 # 輸入一行回車,執(zhí)行 60 # CTRL+C 進程退出,遇到特殊字符,特殊處理。 61 62 # 這是為原始模式,不認識所有特殊符號 63 # 放置特殊字符應用在當前終端,如此設置,將所有的用戶輸入均發(fā)送到遠程服務器 64 tty.setraw(sys.stdin.fileno()) 65 chan.settimeout(0.0) 66 67 while True: 68 # 監(jiān)視 用戶輸入 和 遠程服務器返回數(shù)據(jù)(socket) 69 # 阻塞,直到句柄可讀 70 r, w, e = select.select([chan, sys.stdin], [], [], 1) 71 if chan in r: 72 try: 73 x = u(chan.recv(1024)) 74 if len(x) == 0: 75 print('\r\n*** EOF\r\n') 76 break 77 sys.stdout.write(x) 78 sys.stdout.flush() 79 except socket.timeout: 80 pass 81 if sys.stdin in r: 82 x = sys.stdin.read(1) 83 if len(x) == 0: 84 break 85 chan.send(x) 86 87 finally: 88 # 重新設置終端屬性 89 termios.tcsetattr(sys.stdin, termios.TCSADRAIN, oldtty) 90 91 92 chan.close() 93 tran.close()滅絕師太1
1 import paramiko 2 import sys 3 import os 4 import socket 5 import getpass 6 7 from paramiko.py3compat import u 8 9 # windows does not have termios... 10 try: 11 import termios 12 import tty 13 has_termios = True 14 except ImportError: 15 has_termios = False 16 17 18 def interactive_shell(chan): 19 if has_termios: 20 posix_shell(chan) 21 else: 22 windows_shell(chan) 23 24 25 def posix_shell(chan): 26 import select 27 28 oldtty = termios.tcgetattr(sys.stdin) 29 try: 30 tty.setraw(sys.stdin.fileno()) 31 tty.setcbreak(sys.stdin.fileno()) 32 chan.settimeout(0.0) 33 log = open('handle.log', 'a+', encoding='utf-8') 34 flag = False 35 temp_list = [] 36 while True: 37 r, w, e = select.select([chan, sys.stdin], [], []) 38 if chan in r: 39 try: 40 x = u(chan.recv(1024)) 41 if len(x) == 0: 42 sys.stdout.write('\r\n*** EOF\r\n') 43 break 44 if flag: 45 if x.startswith('\r\n'): 46 pass 47 else: 48 temp_list.append(x) 49 flag = False 50 sys.stdout.write(x) 51 sys.stdout.flush() 52 except socket.timeout: 53 pass 54 if sys.stdin in r: 55 x = sys.stdin.read(1) 56 import json 57 58 if len(x) == 0: 59 break 60 61 if x == '\t': 62 flag = True 63 else: 64 temp_list.append(x) 65 if x == '\r': 66 log.write(''.join(temp_list)) 67 log.flush() 68 temp_list.clear() 69 chan.send(x) 70 71 finally: 72 termios.tcsetattr(sys.stdin, termios.TCSADRAIN, oldtty) 73 74 75 def windows_shell(chan): 76 import threading 77 78 sys.stdout.write("Line-buffered terminal emulation. Press F6 or ^Z to send EOF.\r\n\r\n") 79 80 def writeall(sock): 81 while True: 82 data = sock.recv(256) 83 if not data: 84 sys.stdout.write('\r\n*** EOF ***\r\n\r\n') 85 sys.stdout.flush() 86 break 87 sys.stdout.write(data) 88 sys.stdout.flush() 89 90 writer = threading.Thread(target=writeall, args=(chan,)) 91 writer.start() 92 93 try: 94 while True: 95 d = sys.stdin.read(1) 96 if not d: 97 break 98 chan.send(d) 99 except EOFError: 100 # user hit ^Z or F6 101 pass 102 103 104 def run(): 105 tran = paramiko.Transport(('10.211.55.4', 22,)) 106 tran.start_client() 107 tran.auth_password('wupeiqi', '123') 108 109 # 打開一個通道 110 chan = tran.open_session() 111 # 獲取一個終端 112 chan.get_pty() 113 # 激活器 114 chan.invoke_shell() 115 116 interactive_shell(chan) 117 118 chan.close() 119 tran.close() 120 121 122 if __name__ == '__main__': 123 run()周芷若
1 import paramiko 2 import sys 3 import os 4 import socket 5 import getpass 6 7 from paramiko.py3compat import u 8 9 # windows does not have termios... 10 try: 11 import termios 12 import tty 13 has_termios = True 14 except ImportError: 15 has_termios = False 16 17 18 def interactive_shell(chan): 19 if has_termios: 20 posix_shell(chan) 21 else: 22 windows_shell(chan) 23 24 25 def posix_shell(chan): 26 import select 27 28 oldtty = termios.tcgetattr(sys.stdin) 29 try: 30 tty.setraw(sys.stdin.fileno()) 31 tty.setcbreak(sys.stdin.fileno()) 32 chan.settimeout(0.0) 33 log = open('handle.log', 'a+', encoding='utf-8') 34 flag = False 35 temp_list = [] 36 while True: 37 r, w, e = select.select([chan, sys.stdin], [], []) 38 if chan in r: 39 try: 40 x = u(chan.recv(1024)) 41 if len(x) == 0: 42 sys.stdout.write('\r\n*** EOF\r\n') 43 break 44 if flag: 45 if x.startswith('\r\n'): 46 pass 47 else: 48 temp_list.append(x) 49 flag = False 50 sys.stdout.write(x) 51 sys.stdout.flush() 52 except socket.timeout: 53 pass 54 if sys.stdin in r: 55 x = sys.stdin.read(1) 56 import json 57 58 if len(x) == 0: 59 break 60 61 if x == '\t': 62 flag = True 63 else: 64 temp_list.append(x) 65 if x == '\r': 66 log.write(''.join(temp_list)) 67 log.flush() 68 temp_list.clear() 69 chan.send(x) 70 71 finally: 72 termios.tcsetattr(sys.stdin, termios.TCSADRAIN, oldtty) 73 74 75 def windows_shell(chan): 76 import threading 77 78 sys.stdout.write("Line-buffered terminal emulation. Press F6 or ^Z to send EOF.\r\n\r\n") 79 80 def writeall(sock): 81 while True: 82 data = sock.recv(256) 83 if not data: 84 sys.stdout.write('\r\n*** EOF ***\r\n\r\n') 85 sys.stdout.flush() 86 break 87 sys.stdout.write(data) 88 sys.stdout.flush() 89 90 writer = threading.Thread(target=writeall, args=(chan,)) 91 writer.start() 92 93 try: 94 while True: 95 d = sys.stdin.read(1) 96 if not d: 97 break 98 chan.send(d) 99 except EOFError: 100 # user hit ^Z or F6 101 pass 102 103 104 def run(): 105 default_username = getpass.getuser() 106 username = input('Username [%s]: ' % default_username) 107 if len(username) == 0: 108 username = default_username 109 110 111 hostname = input('Hostname: ') 112 if len(hostname) == 0: 113 print('*** Hostname required.') 114 sys.exit(1) 115 116 tran = paramiko.Transport((hostname, 22,)) 117 tran.start_client() 118 119 default_auth = "p" 120 auth = input('Auth by (p)assword or (r)sa key[%s] ' % default_auth) 121 if len(auth) == 0: 122 auth = default_auth 123 124 if auth == 'r': 125 default_path = os.path.join(os.environ['HOME'], '.ssh', 'id_rsa') 126 path = input('RSA key [%s]: ' % default_path) 127 if len(path) == 0: 128 path = default_path 129 try: 130 key = paramiko.RSAKey.from_private_key_file(path) 131 except paramiko.PasswordRequiredException: 132 password = getpass.getpass('RSA key password: ') 133 key = paramiko.RSAKey.from_private_key_file(path, password) 134 tran.auth_publickey(username, key) 135 else: 136 pw = getpass.getpass('Password for %s@%s: ' % (username, hostname)) 137 tran.auth_password(username, pw) 138 139 # 打開一個通道 140 chan = tran.open_session() 141 # 獲取一個終端 142 chan.get_pty() 143 # 激活器 144 chan.invoke_shell() 145 146 interactive_shell(chan) 147 148 chan.close() 149 tran.close() 150 151 152 if __name__ == '__main__': 153 run()張無忌
?
轉載于:https://www.cnblogs.com/huaijunliang/p/5735770.html
總結
以上是生活随笔為你收集整理的day13 paramiko、数据库表操作的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: 雪莉实验机一天是28号结束,还是28号是
 - 下一篇: 模板 读入挂!