审计系统---堡垒机python下ssh的使用
堡壘機python下ssh的使用
【堡壘機更多參考】http://www.cnblogs.com/alex3714/articles/5286889.html
【paramiko的Demo實例】https://github.com/paramiko/paramiko
Win7下paramiko的Demo遠程登錄執(zhí)行交互命令:
【下載Demo文件】??? ?https://github.com/paramiko/paramiko
【paramiko更多參考】paramiko模塊學習
本機[win7]登錄遠程Linux服務器
????????? Win7本機IP:? 192.168.2.102
????????? 遠程服務器IP: 192.168.2.105
關于Win7下執(zhí)行原代碼報錯問題的解決:
錯誤現(xiàn)象:TypeError: write() argument must be str, not bytes
問題解決:F:\Django\paramiko-demo\paramiko-master\demos\interactive.py
?
Linux下paramiko的Demo遠程登錄執(zhí)行交互命令:
下載Demo文件
https://github.com/paramiko/paramiko
上傳文件到本機Linux服務器:
omc@omc-virtual-machine:~$ cd paramiko_demo/ omc@omc-virtual-machine:~/paramiko_demo$ llLinux登錄其他的Linux服務器
? Linxu本機IP:? 192.168.25.110
? 遠程服務器IP: 192.168.25.133
omc@omc-virtual-machine:~/paramiko_demo$ python3 demo.py Hostname: 192.168.25.133 *** Unable to open host keys file *** WARNING: Unknown host key! Username [omc]: root Auth by (p)assword, (r)sa key, or (d)ss key? [p] p Password for root@192.168.25.133: *** Here we go! Last login: Tue May 1 07:53:03 2018 from 192.168.25.110 [root@localhost ~]# omc@omc-virtual-machine:~/paramiko_demo$ ssh root@192.168.25.133 The authenticity of host '192.168.25.133 (192.168.25.133)' can't be established. RSA key fingerprint is SHA256:+v73ij2IHBzxee8o9n5rYkBJPwD96SaEBtxkuGBBCqg. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '192.168.25.133' (RSA) to the list of known hosts. root@192.168.25.133's password: Last login: Tue May 1 07:44:47 2018 from 192.168.25.1 [root@localhost ~]# logout Connection to 192.168.25.133 closed. omc@omc-virtual-machine:~/paramiko_demo$ python3 demo.py Hostname: 192.168.25.133 *** Host key OK. Username [omc]: root Auth by (p)assword, (r)sa key, or (d)ss key? [p] p Password for root@192.168.25.133: *** Here we go!注意:區(qū)別于第一次登錄,第二次登錄可以獲取的133服務器的信息,沒有了告警
paramiko的Demo分析以及改進
demo.py
interactive.py
paramiko的interactive改進:
import socket import sys import time from paramiko.py3compat import u # windows does not have termios... try:import termiosimport ttyhas_termios = True except ImportError:has_termios = False def interactive_shell(chan): # chan應該是個連接的實例if has_termios: # 判斷win還是Linuxposix_shell(chan) # posix是Linux下的協(xié)議標準else:windows_shell(chan)def posix_shell(chan): # chan 就是我們建立的連接實例import select # IO多路復用,獲取事件時會一個個的進行進行搜尋,直到找到那個事件oldtty = termios.tcgetattr(sys.stdin)try:tty.setraw(sys.stdin.fileno())tty.setcbreak(sys.stdin.fileno())chan.settimeout(0.0)cmd = []f = open('cmd.log', 'a')while True: # select循環(huán)監(jiān)測r, w, e = select.select([chan, sys.stdin], [], []) # 3個參數(shù)分別為輸入,輸出,錯誤信息if chan in r: # 如果遠程有返回命令的結果,進行結果輸出try:x = u(chan.recv(1024)) # 每次接收1KB的長度if len(x) == 0: # 長度為0,表示沒有接收到sys.stdout.write('\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) # read()函數(shù),輸入一個讀取一個發(fā)送一個[回車代表命令輸入完成可以執(zhí)行任務]if(x == '\r'): # Linux下的回車是\r# print("".join(cmd))cmd_log_format = "%s-%s-%s\r" % (time.ctime(time.time()), 'root', "".join(cmd))f.write(cmd_log_format)cmd = [] # 情況作為下次使用else:cmd.append(x)if len(x) == 0:breakchan.send(x) # 如果讀到了輸入內容,則發(fā)送到遠程進行操作finally:termios.tcsetattr(sys.stdin, termios.TCSADRAIN, oldtty)# thanks to Mike Looijmans for this code def windows_shell(chan):import threadingsys.stdout.write("Line-buffered terminal emulation. Press F6 or ^Z to send EOF.\r\n\r\n")def writeall(sock):while True:data = sock.recv(256)if not data:sys.stdout.write('\r\n*** EOF ***\r\n\r\n')sys.stdout.flush()breaksys.stdout.write(data.decode("utf-8"))sys.stdout.flush()writer = threading.Thread(target=writeall, args=(chan,))writer.start()try:while True:d = sys.stdin.read(1)if not d:breakchan.send(d)except EOFError:# user hit ^Z or F6pass注:記錄了文件,但是由點小bug,就是文件會記錄下左右移動的操作[此時會轉換為二進制的內容]
轉載于:https://www.cnblogs.com/ftl1012/p/9458889.html
總結
以上是生活随笔為你收集整理的审计系统---堡垒机python下ssh的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 0x53 区间DP
- 下一篇: MyBatis的几个重要概念和工作流程