我有一个python脚本,该脚本从服务器名称的用户输入或服务器名称的输入xlsx文件传递了一个参数。
大型数据结构,是一个字典,其中包含每个键的列表。列表中每个键可以包含很多项。参数传递给该函数以进行遍历,并且得到的结果不一致,这使我感到困惑。
我尝试了多种方法,例如将代码与脚本解耦以测试和测试我搜索到的其他建议。
我从远程linux服务器收集的-带有列表的字典片段:
{'ABC-OS-WIN': ['server1', 'server2', 'SERVER3', 'server4',....., 'server5', 'SERVER6', 'server7', 'SERVER8', 'SERVER9'], 'DEF-OS-ZFS': [], 'EFG-OS-UNX': ['server10', 'server11', 'SERVER12', 'server13',....., 'server14', 'SERVER15', 'server16', 'SERVER17', 'SERVER18']}
-解析来自xlsx文件的输入数据,该数据将用于检查上述数据结构。列表中的项目默认情况下为大写
server_list = ['SERVER1', 'SERVER2']
当我将大写的server_list传递给我的函数时,它将搜索第一个服务器,但找不到该服务器并停止循环。但是,当我将server_list名称更改为小写时,它将在我的数据结构中找到服务器,并循环浏览列表中的两个项目。
-读取输入文件并构建服务器集合以进行检查的代码片段:
def main():
#other code here
server_list = []
if len(infile_dict) == 0:
print ("No Backup Steps")
sys.exit()
else:
for i in infile_dict:
if i == "Hostname":
for j in infile_dict['Hostname']:
server_list.append(j)
print(server_list)
for srv in (server_list):
srv = srv #when i have this it loops first item in list
#srv = srv.lower() #if i uncomment this, then it loops through all
run_book(srv)
input("press any key to continue")
class ClientNotFoundError(Exception):
""" This exception can be rasied if client is not found in Backup """
pass
def run_book(hostname):
#if exist server, server name will be stored in client_alive
client_alive = check_marley_hostname(hostname)
print("clientalive ", client_alive)
print("...", client_alive)
if client_alive == None:
print("...not active client", hostname)
raise ClientNotFoundError(hostname, "Server not active backup client")
if not client_alive:
print("...elif not active client", hostname)
raise ClientNotFoundError(hostname, "Server not active backup client")
if client_alive == hostname:
print("inside clientalive[0]")
country_location = get_country_code(hostname)
print("countrylocation", country_location)
ssh_connect_host = get_nbu_master_server(country_location)
print("ssh_connect_host" , ssh_connect_host)
# all_active_policies = get_active_nbu_policies()
all_active_policies = get_active_nbu_policies(ssh_connect_host)
all_policies_and_clients = get_active_policies_client(ssh_connect_host, all_active_policies)
print("clients in policies", all_policies_and_clients)
active_client = check_host_in_policy(hostname, all_policies_and_clients)
print("active cleint", active_client)
-示例控制台执行结果显示它在第一台服务器之后停止
In [1]: run cray.py -I excelDoc.xlsx
excelDoc.xlsx
...Input file passed
..cwd D:\Test\Py
...Pathfile D:\Test\Py\infile\excelDoc.xlsx
['SERVER1', 'SERVER2']
... SERVER1
...Check if server exist in Inventory
HOSTmon* Command Line Interface
[
{
"hostname" : "server1",
"country" : "us"
}
]
empty list
clientalive None
... None
...not active client SERVER1
---------------------------------------------------------------------------
ClientNotFoundError Traceback (most recent call last)
D:\Test\Py\cray.py in <module>
382
383 if __name__ == "__main__":
--> 384 main()
D:\Test\Py\cray.py in main()
378 srv = srv
379 #srv = srv.lower()
--> 380 run_book(srv)
381 input("press any key to continue")
382
D:\Test\Py\cray.py in run_book(hostname)
254 if client_alive == None:
255 print("...not active client", hostname)
--> 256 raise ClientNotFoundError(hostname, "Server not active b
ackup client")
257 #sys.exit()
258 if not client_alive:
ClientNotFoundError: ('SERVER1', 'Server not active backup client')
从执行结果中,我看到它找不到服务器,因为我以大写形式传递了它。因此,为了解决这个问题,我可以将列表转换为小写形式,并且看起来可行。其次,更重要的是,为什么它在第一个服务器之后停止而不在第二个服务器中循环?以为我的班级期望会允许这一点,或者我无法正确理解。莫名其妙。