I have a python code that needs to get file or directory information on a remote host that's Unix/Linux (OS names: HP-UX/RedHat/SunOS/AIX/Linux/etc).
The program SSH into the remote host (using paramiko library) and executes ls -l
or ls -ld
depending on if it's a file or a directory.
Information I need are:
- Permission (user/group/other)
- User owner
- Group owner
- Last Modified
- File name
However, the problems with ls
are:
- Output is different from platform so special handling is needed which makes code verbose using checks.
- File size unit may be different in the output depend on environment variable (for GNU coreutils, BLOCK_SIZE), or some may even not support this. This also requires platform-specific checks.
I am looking for a python library or simple portable executable if there is one.
Solutions I considered (but seems infeasible)
- Use regex to check format of the output and process if it matches konwn format. However, this seems to be error prone due to try-checks.
- Also try-check environment variables for the file size and found out the file size unit in the output. (e.g. echo few characters to a file and check the unit. If 4 char is written and size says 1 then I know for sure the unit is greater than 1 bytes. Repeat the steps). This seems to be also error prone
- Install cross-platform compiler on each host, compile and then execute. Can't do it since if the host OS is reinstalled or restored to the point that doesn't have compiler, this installation process needs to be repeated.
Any suggestions?