0

I have a python script using paramiko to get some info from a couple of servers. My problem is that a couple of servers have a directory called Middleware (capital letter), and the rest of them are middleware.

Follow the command:

stdin, stdout, stderr = client.exec_command("ls -d /u01/app/oracle/Middleware/OPatch")

The problem of this command, is that it doesn't works when the server has middleware directory.

How can I run this command to access the both directory (Middleware or middleware) according to each server?

ps: I can't do this ls -d /u01/app/oracle/*/OPatch because there are other folders in this directory.

Tks

S_A
  • 411
  • 1
  • 7
  • 25
  • 1
    Use `[Mm]iddleware`. – jarmod Sep 08 '21 at 20:37
  • 1
    try `client.exec_command("ls -d /u01/app/oracle/[Mm]iddleware/OPatch")` ... though really, you should avoid doing that. I'm guessing that you really need something like [this](https://stackoverflow.com/a/8933290/1394729) – tink Sep 08 '21 at 20:37
  • @jarmod [Mm]iddleware doesn't works – S_A Sep 08 '21 at 20:41
  • Did you test it first outside of Paramiko? SSH to the machine, run the command. – jarmod Sep 08 '21 at 20:44
  • @jarmod I tried direct on shell script `ssh myUser@host ls -d /u01/app/oracle/[Mm]iddleware/OPatch/` and doesnt works – S_A Sep 08 '21 at 20:47
  • Interesting. It works fine here with both Mac and with CentOS-based Linux. Did you SSH onto the host then run the command to test? Are you doubly-sure this folder exists on that host? Were there any error messages? – jarmod Sep 08 '21 at 20:53
  • And ... confirmed that it also works with Paramiko, populating stdout with the full path of the Middleware folder. – jarmod Sep 08 '21 at 21:14
  • In the first place, do not use shell commands to work with remote files, use SFTP. – Martin Prikryl Sep 09 '21 at 05:43

2 Answers2

1

The following solution utilizing [Mm]iddleware works for me:

import paramiko

client = paramiko.SSHClient()
client.load_system_host_keys()

def main():
    client.connect('myhostname')
    stdin, stdout, stderr = client.exec_command("ls -d /home/myuser/[Mm]iddleware")

    for line in stdout.read().splitlines():
        print("stdout:", line)

    for line in stderr.read().splitlines():
        print("stderr:", line)


if __name__ == "__main__":
    main()

The output is as follows:

stdout: b'/home/myuser/Middleware'
jarmod
  • 71,565
  • 16
  • 115
  • 122
0

you can test which one exists first

if [ -d /u01/app/oracle/Middleware/OPatch ];then
   stdin, stdout, stderr = client.exec_command("ls -d /u01/app/oracle/Middleware/OPatch")
elif [ -d /u01/app/oracle/middleware/OPatch ];then
   stdin, stdout, stderr = client.exec_command("ls -d /u01/app/oracle/middleware/OPatch")
fi
Oren
  • 1
  • 3