Python \n把我的剧本弄乱了

Python \n把我的剧本弄乱了,python,cisco,Python,Cisco,我有一个脚本,如果我在主函数中分配一个列表,它将成功运行,但我需要在100多台设备上运行这个脚本。我花了一些时间进行研究,我认为最好的方法是将变量存储在一个文本文件中并以这种方式访问它。(当然,如果有人知道更好的方法,我完全赞成!) 我现在的问题是,当我试图将现有代码转换为新的充满变量的文本文件时,我得到: TypeError:getaddrinfo()参数1必须是字符串或无“ 我很确定我看到这些是因为\n行中断,因为它将一直失败到最后一行,而这一行是有效的 到目前为止,我已经尝试了行.spli

我有一个脚本,如果我在主函数中分配一个列表,它将成功运行,但我需要在100多台设备上运行这个脚本。我花了一些时间进行研究,我认为最好的方法是将变量存储在一个文本文件中并以这种方式访问它。(当然,如果有人知道更好的方法,我完全赞成!)

我现在的问题是,当我试图将现有代码转换为新的充满变量的文本文件时,我得到:

TypeError:getaddrinfo()参数1必须是字符串或无“

我很确定我看到这些是因为
\n
行中断,因为它将一直失败到最后一行,而这一行是有效的

到目前为止,我已经尝试了
行.split
&将txt文件另存为.csv并将分隔符更改为
\n
,但两者都没有达到我预期的效果

以下是有效的脚本:

import sys, os, string, threading
import getpass
import paramiko
import time


cmd = "sh vl bri"
lanid = 'admin'
pwd = 'password'


outlock = threading.Lock()

def workon(host):
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(host, username=lanid, password=pwd)
    stdin, stdout, stderr = ssh.exec_command(cmd)
    #print stdout.read()
    stdin.flush()

    with open("output-" + host + ".txt", 'w+') as f:
        f.seek(0)
        f.write(stdout.read())

    with outlock:
        print stdout.readlines()
        #f.write(stdout)


def main():
    hosts = ['sw1', 'sw2', 'sw3'] # etc
    threads = []
    for h in hosts:
        t = threading.Thread(target=workon, args=(h,))
        t.start()
        threads.append(t)
    for t in threads:
        t.join()


main()
编辑1

import sys, os, string, threading
import getpass
import paramiko
import time


cmd = "sh vl bri"
#lanid = raw_input("Enter your uname: ")
lanid = 'admin'
pwd = 'password'


outlock = threading.Lock()

def workon(stripped_row):
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(stripped_row, username=lanid, password=pwd)
    stdin, stdout, stderr = ssh.exec_command(cmd)
    #print stdout.read()
    stdin.flush()

    with open("output-" + stripped_row + ".txt", 'w+') as f:
        f.seek(0)
        f.write(stdout.read())

    with outlock:
        print stdout.readlines()
        #f.write(stdout)



def main():
    my_file = open('10host.txt')
    threads = []
    for h in my_file:
        striped_row = h.strip()
        t = threading.Thread(target=workon, args=(h,))
        t.start()
        threads.append(t)
    for t in threads:
        t.join()


main()
问题在于:

for h in my_file:
    striped_row = h.strip()
    t = threading.Thread(target=workon, args=(h,))

您正在剥离当前行,但将未剥离的行传递给辅助函数。请尝试将
剥离行作为参数传递,而不是
h

是否可以在每一行上调用
.strip()
.replace(“\n”,”)
?我尝试了这两种方法,但仍然得到了相同的错误。可能是我放错了位置。我需要在上面的代码中的什么位置执行替换或剥离操作?我假设在for语句之后直接在for循环内部执行。我希望添加一个,例如,
my_file=open(“path_to_file”);对于我的_文件中的行:stripped_row=row.strip()#对stripped_row做点什么
?不起作用的代码和完整的堆栈跟踪可能也会有帮助。你有工作的代码,也有不起作用的代码……你希望我们通过向我们展示工作的代码来修复不起作用的代码。这不是玩这个游戏的方式!