我正在编写一个脚本(Python 2.7.10)来登录网络设备并收集供应商可能要求的诊断信息。这很简单,但我遇到了一个有趣的问题(至少对我而言)。
我已经用尽了有限的知识。
这是调用要运行的函数的代码片段:
elif args.hostname and args.username and args.jtac and args.commands and args.shell:
print("RUN FOR SINGLE HOST w/ SHELL AND CLI COMMANDS")
open_ssh_session(args.hostname, args.username, password)
commands_and_iterations_cli(args.hostname, args.jtac, args.iterations, float(args.interval))
commands_and_iterations_shell(args.username, args.hostname, args.jtac, args.iterations, float(args.interval))
single_core_dump(args.hostname, args.username, password, args.jtac)
pull_files_from_juniper_device(args.hostname, args.username, password, args.jtac)
push_files_to_juniper_sftp(args.hostname, args.username, password, args.jtac)
所以我有两个功能:
def commands_and_iterations_cli(hostname, jtac, iterations, interval):
print("Enter each JUNOS CLI command on separate lines, press CTRL+D when finished.\n"
"NOTE: Valid CLI commands only, DO NOT input shell commands here!\n")
# Take user input, enter a command on each new line, send EOF to indicate that you're done.
cli_commands = sys.stdin.readlines()
# Instantiate user shell.
channel = client.invoke_shell()
# Take each line from given input, and iterate over hostname.
for line in cli_commands:
line = line.strip()
iter_counter = 0
print("Running {}, {} times, every {} seconds.".format(line, str(iterations), interval))
while iter_counter < iterations:
iter_counter = iter_counter + 1
channel.send(line +' | save "{}-{}-{}"\n'.format(hostname, jtac, line))
time.sleep(interval)
def commands_and_iterations_shell(username, hostname, jtac, iterations, interval):
print("Enter each shell command on separate lines, press CTRL+D when finished.\n"
"NOTE: Valid shell commands only, DO NOT input JUNOS CLI commands here!\n"
"** ENSURE COMMANDS ARE SAFE TO RUN IN PRODUCTION IF DOING SO! **\n")
# Take user input, enter a command on each new line, send EOF to indicate that you're done.
shell_commands = sys.stdin.readlines()
# Prompt for hostname's root password to enter root shell.
print("Enter root password for {}:\n".format(hostname))
rootpass = getpass.getpass()
# Instantiate user shell.
channel = client.invoke_shell()
# Start root shell.
channel.send("start shell user root\n")
# Let the prompt become available.
time.sleep(1)
# Send the password and let the root prompt return.
channel.send(rootpass+"\n")
time.sleep(1)
# Take each line from given input, and iterate over hostname.
for line in shell_commands:
line = line.strip()
print("Running {}, {} times, every {} seconds.".format(line, str(iterations), interval))
iter_counter = 0
while iter_counter != iterations:
channel.send(line +' >> "/var/home/{}/{}-{}-{}" \n'.format(username, hostname, jtac, line))
time.sleep(interval)
iter_counter = iter_counter + 1
print(iter_counter)
如果我独立运行commands_and_iterations_cli()
或commands_and_iterations_shell()
,代码就会完美运行。但是当我同时尝试两个(给出的例子)时,CLI函数将正确运行,然后当运行shell函数时,shell函数将打印其中的文本,并提示输入root密码,然后立即跳过之后的下一个功能,没有提示命令。我甚至尝试在shell函数运行之前给它一个30秒的睡眠时间,然后是行为。
谢谢,所有。
答案 0 :(得分:2)
它不起作用,因为您必须使用CTRL+D
退出关闭流的sys.stdin.readlines()
,并且进一步调用它不会做任何事情(即返回空列表)。