2016-05-11 137 views
0

我正在設置遠程SSH連接到遠程服務器並運行特定命令來轉儲數據庫表。遠程服務器是具有自己shell的強化的linux操作系統。我正在運行遠程sql類型的命令來轉儲出大量數據。我的python腳本正在使用交互式SSH會話來執行此操作。正如你在下面看到的,我正在運行一個命令,讓它休眠5秒鐘,然後轉儲緩衝區。我已經爲「remote_conn.recv()」函數嘗試了許多不同的選項,但我無法獲得完整的輸出。輸出是非常大的,它是分頁(按空間更多)。我甚至沒有獲得第一頁的完整輸出。如果第一頁有50行,我只能得到前4個。Python Paramiko獲取完整輸出

有沒有更好的方法來做到這一點?我如何才能抓住完整的輸出?以下是我腳本的Paramiko部分。

# Create instance of SSHClient object 
remote_conn_pre = paramiko.SSHClient() 

# Automatically add untrusted hosts 
remote_conn_pre.set_missing_host_key_policy(
    paramiko.AutoAddPolicy()) 

# initiate SSH connection 
remote_conn_pre.connect(options.ip, username=options.userName, password=options.password) 

# Use invoke_shell to establish an 'interactive session' 
remote_conn = remote_conn_pre.invoke_shell() 

remote_conn.send("<remote sql command here>") 
time.sleep(5) 
output = remote_conn.recv(65535) 

print output 
remote_conn.close() 
+0

爲什麼不使用SQL客戶端direclty? – Maresh

+0

操作系統是帶有自己的外殼的強化的Linux。我必須從CLI運行它自己的命令。這不是標準的SQL調用,你不能遠程訪問它,或者我會走這條路。 – hiddenicon

+0

SCP允許在這臺機器上使用SCP嗎? – Maresh

回答

0

我是能夠通過抓住緩衝的小塊和concat'ing在一起,以獲得完整的輸出,直到我打在緩衝區某些字符串。

while True: 
    data = remote_conn.recv(500) 

    if "<string>" in data: 
     break 
    else: 
     finalOutput += data