現在、いくつかの機能を開発しており、同時に複数のリモートデバイスへのアクセスをSSH
で行っています。私は以下のような問題に遭遇しました。Pythonグローバル変数が機能しません
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()
は、私は自分のコードで使用する必要がある他のパラメータはありますか?
ありがとうございます、それはうまく動作します – nanto