[英]Python functions running too fast?
我正在编写一个脚本(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)
所以我有2个功能:
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秒,并且行为如下。
谢谢大家
这是行不通的,因为您必须使用CTRL+D
退出sys.stdin.readlines()
,该操作会关闭流并进一步对其进行任何调用(即返回空列表)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.