fgimian / paramiko-expect

A Python expect-like extension for the Paramiko SSH library which also supports tailing logs.
MIT License
204 stars 78 forks source link

Issues with paramiko-expect while interacting with prompt of a remote ssh server #92

Closed SolankiVBHV closed 1 year ago

SolankiVBHV commented 1 year ago

I am using paramiko and parmiko-expect to send commands to a remote machine. After sending a command, the interact.expect part is not working as intended. After interact.expect, instead of sending the next command, it is sending just one character of some other command /or no characters at all and throwing one error.

here is the code:

  remote_dir = "/export/M2"
  ssh = paramiko.SSHClient()
  ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  ssh.connect(node, username=uname, password=pwd)
  print("Connected to :", node)

  with SSHClientInteraction(ssh, timeout=20, display=True) as interact:
      interact.expect("admin:")
      interact.send("utils system upgrade dataexport cancel")
      interact.expect("admin:")
      interact.send("utils system upgrade dataexport initiate")
      # interact.expect([".*Initializing*", ".*Export Data Directory*"], timeout=50)
      interact.expect("Export Data Directory :", timeout=50)
      print(interact.last_match)
      if interact.last_match == "Export Data Directory :":
          print("sending path", remote_dir)
          interact.send(remote_dir)

Output:

admin:utils system upgrade dataexport cancel

Terminate data export operation......

System data export is not in progress
admin:utils system upgrade dataexport initiate

Warning: The data export process would take anywhere between a few minutes to about few hours to complete. This export timeThis export time depends on the data configuration and network speed. This Cli is recommended to be executes. Pressing Ctrl+d during off hours. Pressing
Ctrl+C will abort the data export process.
                                                                                                           fer to the Upgrad
Warning: Before upgrading the cluster Cisco recommends installing the latest Upgrade Readiness COP file. Refer to the Upgrade Guide on cisco.com for details.

Initializing...
Export Data Directory :
Export Data Directory :
sending path /export/M2

Input validation failed. Invalid SFTP directory. Please provide absolute path of the SFTP directory.
Export Data Directory :
EXCESS TIME RECV_READY TIMEOUT, did you expect() before a send()
sending sftp ip

Input validation failed. Invalid SFTP directory. Please provide absolute path of the SFTP directory.
Export Data Directory :
EXCESS TIME RECV_READY TIMEOUT, did you expect() before a send()
sending sftp loginid

Things I have tried

  1. interact.expect([".Initializing", ".Export Data Directory"], timeout=50) is able to get from the intereact.clean_output but still not sending the right path.
  2. debugging the script in vscode doesn't show the exact prompt the script receives.
  3. Checked possible examples over the internet and similar questions in stack overflow. I was not able to find possible solution ideas. Please advise how can I send the complete remote path in the shell and get this working!