2016-01-18 64 views
0

我正在編寫一個腳本(Python 2.7.10)來登錄網絡設備並收集供應商可能要求的診斷信息。這很簡單,但我遇到了一個有趣的問題(至少對我來說)。Python函數運行速度太快?

我已經用盡了我對此的有限知識。

這是一塊調用函數以運行該代碼的:

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秒的睡眠時間,並且行爲如下。

謝謝,所有。

回答

2

它不起作用,因爲您必須使用CTRL+D才能退出sys.stdin.readlines(),後者會關閉流並進一步調用它不做任何操作(即返回空列表)。

+0

不知道EOF的工作是這樣的,我只是假定它會提示輸入另一組輸入。謝謝你,先生! –