Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
执行python脚本并将输出保存为CSV文件_Python_Csv_Ssh_Paramiko - Fatal编程技术网

执行python脚本并将输出保存为CSV文件

执行python脚本并将输出保存为CSV文件,python,csv,ssh,paramiko,Python,Csv,Ssh,Paramiko,我想将python脚本(sshClient.py)的输出保存为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 ('Tr

我想将python脚本(sshClient.py)的输出保存为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()

结果输出:

未测试,但如果数据是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()

您可以将
(stdout.channel.recv(1024))
写入文件(以二进制模式打开)代替打印。你能编辑我的代码来展示一个例子吗?你能发布几行你得到的文本而不是图像输出吗?我真的很抱歉。现在不能提供给你,因为我现在没有设备。谢谢你的回复。需要定义“addrStr”,因为它表示未解析的引用。在这种情况下我该怎么办?只是个愚蠢的问题。如果我没有错,那么您可以使用命令PathFileName=“/hard/path/filename.csv”将文件保存在特定位置。对吗?是的,它只是一个变量,表示路径和文件的字符串,也可以直接插入open()函数中。