2016-12-23 16 views
-1

pythonスクリプト(sshClient.py)の出力をCSVファイルとして保存します。あなたは私にそれをどうやって助けてくれますか?前もって感謝します。コードと結果の出力は、ここであなたの理解を深めるために共有しています。pythonスクリプトを実行して出力をCSVファイルとして保存

import sys 
import time 
import select 
import paramiko 

host = '169.254.115.1' 
i = 1 

# 
# Try to connect to the host. 
# Retry a few times if it fails. 
# 
while True: 
    print ('Trying to connect to %s (%i/3)' % (host, i)) 

    try: 
     ssh = paramiko.SSHClient() 
     ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 
     ssh.connect(host, port=22, username='user', password='user') 
     print ("Connected to %s" % host) 
     break 
    except paramiko.AuthenticationException: 
     print ("Authentication failed when connecting to %s") % host 
     sys.exit(1) 
    except: 
     print ("Could not SSH to %s, waiting for it to start" % host) 
     i += 1 
     time.sleep(2) 

    # If we could not connect within time limit 
    if i == 3: 
     print ("Could not connect to %s. Giving up") % host 
     sys.exit(1) 

# Send the command (non-blocking) 
stdin, stdout, stderr = ssh.exec_command("cd /opt/cohda/test; sudo ./runtest_iperf_tx.sh") 

# Wait for the command to terminate 
while not stdout.channel.exit_status_ready(): 
    # Only print data if there is data to read in the channel 
    if stdout.channel.recv_ready(): 
     rl, wl, xl = select.select([stdout.channel], [], [], 0.0) 
     if len(rl) > 0: 
      # Print data from stdout 
      print (stdout.channel.recv(1024)), 
# 
# Disconnect from the host 
# 
print ("Command done, closing SSH connection") 
ssh.close() 

結果の出力:Throughput Vs Time

+0

あなたは '(stdout.channel.recvを(書くことができます1024)) 'を(バイナリモードで開いた)ファイルに出力する代わりに、 –

+0

サンプルを表示するためにコードを編集できますか? –

+0

あなたが行った出力の数行を、あなたのイメージのようにイメージとして表示することはできません。 –

答えて

-1

テストしますが、データがASCIIである場合は、この効果に何かが動作するはずない...

# Wait for the command to terminate 
csv_str = "" 
while not stdout.channel.exit_status_ready(): 
    # Only print data if there is data to read in the channel 
    if stdout.channel.recv_ready(): 
     rl, wl, xl = select.select([stdout.channel], [], [], 0.0) 
     if len(rl) > 0: 
      # Print data from stdout 
      print (stdout.channel.recv(1024)), 
      csv_str += str.split(str(stdout.channel.recv(1024)),",") 
# 
# Disconnect from the host 
# 
PathFileName = "/hard/path/filename.csv" 
f = open(PathFileName,'a') 
f.write(csv_str) 
f.close() 
+0

あなたの返信いただきありがとうございます。未解決の参照があるので、 'addrStr'を定義する必要があります。この場合、私は何をすべきですか? –

+0

申し訳ありません...スニペットを編集しました –

+0

ちょっとばかげた質問です。私が間違っていない場合は、特定の場所にファイルを保存するために、PathFileName = "/hard/path/filename.csv"というコマンドを使用します。 –

関連する問題