paramiko 登錄linux主機后執行tail后返回數據不完整解決方法。


 1 def get_sql_log(host,port,user,password,key_words,out_put_filename):
 2     commond='cd crm-app/;./tailall.sh | grep %s'%key_words
 3     s = paramiko.SSHClient()
 4     s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
 5     s.connect(host,port,user,password)
 6     transport=s.get_transport()
 7     channel = transport.open_session()
 8     channel.get_pty()
 9     channel.exec_command(commond)
10     print 'command %s'%commond
11    # print '%s' % (str(host))
12     f=open(out_put_filename,'a+')
13 
14    # f.write(str(dir(s)))
15     while 1:
16         if channel.exit_status_ready():
17             break
18         try:
19             rl,wl,xl=select.select([channel],[],[],1)
20             #print rl
21             if len(rl)>0:
22                 recv=channel.recv(65536)
23                 print recv
24                 #print recv
25                 #f.seek(2)
26                 f.write(str(recv))
27                 f.flush()
28 
29         except KeyboardInterrupt:
30             print("Caught control-C")
         channel.send("\x03")#發送 ctrl+c
               channel.close()
               s.close()
41 exit(0)

之前沒有加入   channel.get_pty() ,這個一直返回的信息不完整。

官方文檔這樣描述:
get_pty(*args, **kwds)

Request a pseudo-terminal from the server. This is usually used right after creating a client channel, to ask the server to provide some basic terminal semantics for a shell invoked with invoke_shell. It isn’t necessary (or desirable) to call this method if you’re going to exectue a single command with exec_command.

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM