python獲取交互式ssh shell的方法
更新,最近在學unix環(huán)境編程,了解一下進程的創(chuàng)建過程,用最原始的方式實現了一個ssh命令的執(zhí)行。
#coding=utf8 ''' 用python實現了一個簡單的shell,了解進程創(chuàng)建 類unix 環(huán)境下 fork和exec 兩個系統(tǒng)調用完成進程的創(chuàng)建 ''' import sys, os def myspawn(cmdline): argv = cmdline.split() if len(argv) == 0: return program_file = argv[0] pid = os.fork() if pid < 0: sys.stderr.write("fork error") elif pid == 0: # child os.execvp(program_file, argv) sys.stderr.write("cannot exec: "+ cmdline) sys.exit(127) # parent pid, status = os.waitpid(pid, 0) ret = status >> 8 # 返回值是一個16位的二進制數字,高8位為退出狀態(tài)碼,低8位為程序結束系統(tǒng)信號的編號 signal_num = status & 0x0F sys.stdout.write("ret: %s, signal: %s\n" % (ret, signal_num)) return ret def ssh(host, user, port=22, password=None): if password: sys.stdout.write("password is: '%s' , plz paste it into ssh\n" % (password)) cmdline = "ssh %s@%s -p %s " % (user, host, port) ret = myspawn(cmdline) if __name__ == "__main__": host = '' user = '' password = '' ssh(host, user, password=password)
最近在做一個項目,需要在客戶端集成一個交互式ssh功能,大概就是客戶端跟服務器申請個可用的機器,服務端返回個ip,端口,密碼, 然后客戶端就可以直接登錄到機器上操做了。該程序基于paramiko模塊。
經查找,從paramiko的源碼包demos目錄下,可以看到交互式shell的實現,就是那個demo.py。但是用起來有些bug,于是我給修改了一下interactive.py(我把windows的代碼刪掉了,剩下的只能在linux下用)。代碼如下:
#coding=utf-8 import socket import sys import os import termios import tty import fcntl import signal import struct import select now_channel = None def interactive_shell(chan): posix_shell(chan) def ioctl_GWINSZ(fd): try: cr = struct.unpack('hh', fcntl.ioctl(fd, termios.TIOCGWINSZ,'aaaa')) except: return return cr def getTerminalSize(): cr = ioctl_GWINSZ(0) or ioctl_GWINSZ(1) or ioctl_GWINSZ(2) return int(cr[1]), int(cr[0]) def resize_pty(signum=0, frame=0): width, height = getTerminalSize() if now_channel is not None: now_channel.resize_pty(width=width, height=height) def posix_shell(chan): global now_channel now_channel = chan resize_pty() signal.signal(signal.SIGWINCH, resize_pty) # 終端大小改變時,修改pty終端大小 stdin = os.fdopen(sys.stdin.fileno(), 'r', 0) # stdin buff置為空,否則粘貼多字節(jié)或者按方向鍵的時候顯示不正確 fd = stdin.fileno() oldtty = termios.tcgetattr(fd) newtty = termios.tcgetattr(fd) newtty[3] = newtty[3] | termios.ICANON try: termios.tcsetattr(fd, termios.TCSANOW, newtty) tty.setraw(fd) tty.setcbreak(fd) chan.settimeout(0.0) while True: try: r, w, e = select.select([chan, stdin], [], []) except: # 解決SIGWINCH信號將休眠的select系統(tǒng)調用喚醒引發(fā)的系統(tǒng)中斷,忽略中斷重新調用解決。 continue if chan in r: try: x = chan.recv(1024) if len(x) == 0: print 'rn*** EOFrn', break sys.stdout.write(x) sys.stdout.flush() except socket.timeout: pass if stdin in r: x = stdin.read(1) if len(x) == 0: break chan.send(x) finally: termios.tcsetattr(sys.stdin, termios.TCSADRAIN, oldtty)
使用示例:
#coding=utf8 import paramiko import interactive #記錄日志 paramiko.util.log_to_file('/tmp/aaa') #建立ssh連接 ssh=paramiko.SSHClient() ssh.load_system_host_keys() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect('192.168.1.11',port=22,username='hahaha',password='********',compress=True) #建立交互式shell連接 channel=ssh.invoke_shell() #建立交互式管道 interactive.interactive_shell(channel) #關閉連接 channel.close() ssh.close()
interactive.py代碼中主要修復了幾個問題:
1、當讀取鍵盤輸入時,方向鍵會有問題,因為按一次方向鍵會產生3個字節(jié)數據,我的理解是按鍵一次會被select捕捉一次標準輸入有變化,但是我每次只處理1個字節(jié)的數據,其他的數據會存放在輸入緩沖區(qū)中,等待下次按鍵的時候一起發(fā)過去。這就導致了本來3個字節(jié)才能完整定義一個方向鍵的行為,但是我只發(fā)過去一個字節(jié),所以終端并不知道我要干什么。所以沒有變化,當下次觸發(fā)按鍵,才會把上一次的信息完整發(fā)過去,看起來就是按一下方向鍵有延遲。多字節(jié)的粘貼也是一個原理。解決辦法是將輸入緩沖區(qū)置為0,這樣就沒有緩沖,有多少發(fā)過去多少,這樣就不會有那種顯示的延遲問題了。
2、終端大小適應。paramiko.channel會創(chuàng)建一個pty(偽終端),有個默認的大?。╳idth=80, height=24),所以登錄過去會發(fā)現能顯示的區(qū)域很小,并且是固定的。編輯vim的時候尤其痛苦。channel中有resize_pty方法,但是需要獲取到當前終端的大小。經查找,當終端窗口發(fā)生變化時,系統(tǒng)會給前臺進程組發(fā)送SIGWINCH信號,也就是當進程收到該信號時,獲取一下當前size,然后再同步到pty中,那pty中的進程等于也感受到了窗口變化,也會收到SIGWINCH信號。
3、讀寫‘慢'設備(包括pipe,終端設備,網絡連接等)。讀時,數據不存在,需要等待;寫時,緩沖區(qū)滿或其他原因,需要等待。ssh通道屬于這一類的。本來進程因為網絡沒有通信,select調用為阻塞中的狀態(tài),但是當終端窗口大小變化,接收到SIGWINCH信號被喚醒。此時select會出現異常,觸發(fā)系統(tǒng)中斷(4, 'Interrupted system call'),但是這種情況只會出現一次,當重新調用select方法又會恢復正常。所以捕獲到select異常后重新進行select可以解決該問題。
以上這篇python獲取交互式ssh shell的方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
python爬蟲 正則表達式使用技巧及爬取個人博客的實例講解
下面小編就為大家?guī)硪黄猵ython爬蟲 正則表達式使用技巧及爬取個人博客的實例講解。小編覺得挺不錯的,現在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10