admin管理员组

文章数量:1296917

I am running the following script:

pxssh.PROMPT = "user@firewall>"
conn = pxssh.pxssh(options={
                "StrictHostKeyChecking": "no",
                "UserKnownHostsFile": "/dev/null"})
conn.login(ip, user, pw, auto_prompt_reset=False, login_timeout=10)
conn.prompt(timeout=1)
conn.sendline("set cli pager off\r")
conn.prompt(timeout=1)
print(int(time.time()))
conn.sendline("debug dataplane pow performance all\r")
conn.prompt(timeout=1)
output = conn.before
conn.logout()

The script completes and returns the correct data, but it was taking 30s plus for each step until I added the timeout for the prompt. Now the login takes 33s, but the prompts take a second or so.

The login and each step are successful even with the delays.

I have used pexpect in the past and don't remember having this issue.

I was expecting that each command sent and each prompt would also send a "\r" or similar and cause the command to be entered on the cli and the response should be immediately returned.

An interactive session from the same host has each step completes immediately with no delay.

What have I missed? Or is this just expected behavior?

Edit:

Here is the sanitized output of the logging from the script as per the suggestion below. I'm not quite sure how to interpret the $ or why the output of the commands echo back with a prompt between each word...

^M([email protected]) Password: xxxxxxxxxxxxxxxxxxx
^M$
Last login: Mon Feb 17 11:24:19 2025 from 10.0.10.10^M^M$
^M$
^M$
^M$
Number of failed attempts since last successful login: 0^M$
^M$
^M$
^M$
logan@Paloalto(passive)> $
^M$
logan@Paloalto(passive)> $
^M$
logan@Paloalto(passive)> $
^M$
logan@Paloalto(passive)> $
^M$
logan@Paloalto(passive)> set cli pager off^M$
set ^M^[[Klogan@Paloalto(passive)> set cli ^M^[[Klogan@Paloalto(passive)> set cli pager ^M^[[Klogan@Paloalto(passive)> set cli pager off^M$
logan@Paloalto(passive)> ^M$
logan@Paloalto(passive)> debug dataplane pow performance all^M$
debug ^M^[[Klogan@Paloalto(passive)> debug dataplane ^M^[[Klogan@Paloalto(passive)> debug dataplane pow ^M^[[Klogan@Paloalto(passive)> debug dataplane pow performance ^M^[[Klogan@Paloalto(passive)> debug dataplane pow performance all^M$
^M$
group                                 max-us   avg-us        count     total-us  ac-max-us  ac-avg-us         ac-count      ac-total-us^M$
flow_lookup                                0      0.0            0            0          0        0.0                0                0^M$
flow_fastpath                              0      0.0            0            0    1943467       45.8      11061157675     506606814217^M$
flow_slowpath                              0      0.0            0            0    1951485      121.8        519672471      63332379045^M$

Edit 2:

Just clarification - I realize that some of the output prompts are mismatched "(active)>" vs "(passive)>". It does not change the behavior whether they match or not at this point. I have since updated the script appropriately. There is a method to determine the active device which I intend to implement shortly, however this will not solve this issue until I am able to fix the larger issue with the prompt.

I am running the following script:

pxssh.PROMPT = "user@firewall>"
conn = pxssh.pxssh(options={
                "StrictHostKeyChecking": "no",
                "UserKnownHostsFile": "/dev/null"})
conn.login(ip, user, pw, auto_prompt_reset=False, login_timeout=10)
conn.prompt(timeout=1)
conn.sendline("set cli pager off\r")
conn.prompt(timeout=1)
print(int(time.time()))
conn.sendline("debug dataplane pow performance all\r")
conn.prompt(timeout=1)
output = conn.before
conn.logout()

The script completes and returns the correct data, but it was taking 30s plus for each step until I added the timeout for the prompt. Now the login takes 33s, but the prompts take a second or so.

The login and each step are successful even with the delays.

I have used pexpect in the past and don't remember having this issue.

I was expecting that each command sent and each prompt would also send a "\r" or similar and cause the command to be entered on the cli and the response should be immediately returned.

An interactive session from the same host has each step completes immediately with no delay.

What have I missed? Or is this just expected behavior?

Edit:

Here is the sanitized output of the logging from the script as per the suggestion below. I'm not quite sure how to interpret the $ or why the output of the commands echo back with a prompt between each word...

^M([email protected]) Password: xxxxxxxxxxxxxxxxxxx
^M$
Last login: Mon Feb 17 11:24:19 2025 from 10.0.10.10^M^M$
^M$
^M$
^M$
Number of failed attempts since last successful login: 0^M$
^M$
^M$
^M$
logan@Paloalto(passive)> $
^M$
logan@Paloalto(passive)> $
^M$
logan@Paloalto(passive)> $
^M$
logan@Paloalto(passive)> $
^M$
logan@Paloalto(passive)> set cli pager off^M$
set ^M^[[Klogan@Paloalto(passive)> set cli ^M^[[Klogan@Paloalto(passive)> set cli pager ^M^[[Klogan@Paloalto(passive)> set cli pager off^M$
logan@Paloalto(passive)> ^M$
logan@Paloalto(passive)> debug dataplane pow performance all^M$
debug ^M^[[Klogan@Paloalto(passive)> debug dataplane ^M^[[Klogan@Paloalto(passive)> debug dataplane pow ^M^[[Klogan@Paloalto(passive)> debug dataplane pow performance ^M^[[Klogan@Paloalto(passive)> debug dataplane pow performance all^M$
^M$
group                                 max-us   avg-us        count     total-us  ac-max-us  ac-avg-us         ac-count      ac-total-us^M$
flow_lookup                                0      0.0            0            0          0        0.0                0                0^M$
flow_fastpath                              0      0.0            0            0    1943467       45.8      11061157675     506606814217^M$
flow_slowpath                              0      0.0            0            0    1951485      121.8        519672471      63332379045^M$

Edit 2:

Just clarification - I realize that some of the output prompts are mismatched "(active)>" vs "(passive)>". It does not change the behavior whether they match or not at this point. I have since updated the script appropriately. There is a method to determine the active device which I intend to implement shortly, however this will not solve this issue until I am able to fix the larger issue with the prompt.

Share Improve this question edited Feb 17 at 21:39 meuh 12.3k2 gold badges33 silver badges47 bronze badges asked Feb 11 at 18:16 LoganLogan 93 bronze badges 7
  • The documentation of pxssh recommends using Paramiko instead. – Barmar Commented Feb 11 at 18:49
  • You shouldn't need \r in your sendline calls since an end-of-line character is implied by sendline. Probably not the issue, but could be. – kindall Commented Feb 11 at 19:19
  • @Barmar I did try Paramiko, it hangs indefinitely while trying to read from the ChannelFile. I did not find much answer for fixing that either. (I did combine stdout and stderr). Pexpect is at least working... – Logan Commented Feb 11 at 20:48
  • @kindall I have tried with \n, with \r, and with nothing but the command. No difference in any of them from a timing perspective. – Logan Commented Feb 11 at 21:06
  • The $ comes from cat -e and shows explicitly the end of the line. This ensures you see any space characters that might be there. Hence ^M$ means we got the usual end-of-line 2 chars \r\n, carriage-return line-feed. The repeated partial completion of the commands seems to be an attempt by the firewall to allow you to enter tab or a similar char to get a list of possible words that fit there, an inline help. (The ^M^[[K has ^[, ie escape, so tells the terminal emulator to goto end of line.) You might have a command, like set batch or set noninteractive to stop it doing this. – meuh Commented Feb 17 at 22:11
 |  Show 2 more comments

1 Answer 1

Reset to default 0

You are not setting the correct PROMPT attribute. It should be on the pxssh instance. Start the code with

conn = pxssh.pxssh(options={
                "StrictHostKeyChecking": "no",
                "UserKnownHostsFile": "/dev/null"})
conn.PROMPT = "user@firewall>"

本文标签: pythonSlow Execution of Pexpect Scriptseems to be hitting 30s timeout each stepStack Overflow