Python 全局变量不起作用
Python Global Variable does not work
目前,我正在开发一些功能来同时 SSH
访问多个远程设备。我遇到了如下问题。
Traceback (most recent call last):
File "/Volume/Projects/SSH_Conn.py", line 51, in <module>
SSH_Thread()
File "/Volume/Projects/SSH_Conn.py", line 43, in SSH_Thread
for ip in list_ip:
NameError: global name 'list_ip' is not defined
我很确定我已经在下面的代码中创建了全局参数:
def ip_file():
**global list_ip**
ip_list_file = open('ip.txt', 'r')
ip_list_file.seek(0)
list_ip = ip_list_file.readlines()
ip_list_file.close()
def ssh_conn(ip):
date_time = datetime.datetime.now().strftime("%Y-%m-%d")
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip, port=22, username='x', password='y', look_for_keys=False, timeout=None)
connection = ssh.invoke_shell()
connection.send("\n")
connection.send("ls -l\n")
time.sleep(2)
file_output = connection.recv(9999)
hostname = (re.search(r'(.+)$', file_output)).group().strip('$')
outFile = open(hostname + "-" + str(date_time) + ".txt", "w")
outFile.write(file_output)
def SSH_Thread():
threads_instance = []
for ip in list_ip:
ti = threading.Thread(target=ssh_conn, args=(ip,))
ti.start()
threads_instance.append(ti)
for ti in threads_instance:
ti.join()
SSH_Thread()
我的代码中是否需要使用任何其他参数?
只是 return 函数中的值,而不是处理全局变量。
def ip_file():
ip_list_file = open('ip.txt', 'r')
ip_list_file.seek(0)
list_ip = ip_list_file.readlines()
ip_list_file.close()
return list_ip # return the value, so others can use it.
然后,当你想使用它时,只需调用函数并保存结果:
def SSH_Thread():
threads_instance = []
list_ip = ip_file() # here is where you get the result back
for ip in list_ip:
ti = threading.Thread(target=ssh_conn, args=(ip,))
ti.start()
threads_instance.append(ti)
for ti in threads_instance:
ti.join()
您已经在 ip_file
函数中创建了全局参数 list_ip
,但是从未调用函数 。
所以快速修复是:
def SSH_Thread():
threads_instance = []
ip_file() # <--------------- generate the list
for ip in list_ip:
ti = threading.Thread(target=ssh_conn, args=(ip,))
ti.start()
threads_instance.append(ti)
for ti in threads_instance:
ti.join()
顺便说一下,最好 return
列表而不是将结果存储在全局变量中。
目前,我正在开发一些功能来同时 SSH
访问多个远程设备。我遇到了如下问题。
Traceback (most recent call last):
File "/Volume/Projects/SSH_Conn.py", line 51, in <module>
SSH_Thread()
File "/Volume/Projects/SSH_Conn.py", line 43, in SSH_Thread
for ip in list_ip:
NameError: global name 'list_ip' is not defined
我很确定我已经在下面的代码中创建了全局参数:
def ip_file():
**global list_ip**
ip_list_file = open('ip.txt', 'r')
ip_list_file.seek(0)
list_ip = ip_list_file.readlines()
ip_list_file.close()
def ssh_conn(ip):
date_time = datetime.datetime.now().strftime("%Y-%m-%d")
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip, port=22, username='x', password='y', look_for_keys=False, timeout=None)
connection = ssh.invoke_shell()
connection.send("\n")
connection.send("ls -l\n")
time.sleep(2)
file_output = connection.recv(9999)
hostname = (re.search(r'(.+)$', file_output)).group().strip('$')
outFile = open(hostname + "-" + str(date_time) + ".txt", "w")
outFile.write(file_output)
def SSH_Thread():
threads_instance = []
for ip in list_ip:
ti = threading.Thread(target=ssh_conn, args=(ip,))
ti.start()
threads_instance.append(ti)
for ti in threads_instance:
ti.join()
SSH_Thread()
我的代码中是否需要使用任何其他参数?
只是 return 函数中的值,而不是处理全局变量。
def ip_file():
ip_list_file = open('ip.txt', 'r')
ip_list_file.seek(0)
list_ip = ip_list_file.readlines()
ip_list_file.close()
return list_ip # return the value, so others can use it.
然后,当你想使用它时,只需调用函数并保存结果:
def SSH_Thread():
threads_instance = []
list_ip = ip_file() # here is where you get the result back
for ip in list_ip:
ti = threading.Thread(target=ssh_conn, args=(ip,))
ti.start()
threads_instance.append(ti)
for ti in threads_instance:
ti.join()
您已经在 ip_file
函数中创建了全局参数 list_ip
,但是从未调用函数 。
所以快速修复是:
def SSH_Thread():
threads_instance = []
ip_file() # <--------------- generate the list
for ip in list_ip:
ti = threading.Thread(target=ssh_conn, args=(ip,))
ti.start()
threads_instance.append(ti)
for ti in threads_instance:
ti.join()
顺便说一下,最好 return
列表而不是将结果存储在全局变量中。