The Executable and Linkable Format (ELF) is a common binary standard file format for executable files for Linux systems, e.g. like Photon OS. It is easily possible to invoke this kind of executable via
Python subprocess management. This post shows an approach how to run an ELF file, which is embedded in a Zip container, in VCF Automation.
Invoke ELF Executable with Python
The following example shows how to invoke an ELF executable which is not available in Photon OS, here traceroute. The traceroute ELF executable is part of the zip container, which is imported as an action. traceroute is a tool that displays the path of a data packet from the calling source to a given destination.
Important steps are copying the executable file to the temporary directory and setting the access rights. These preparations are necessary so that the binary file from the zip container can actually be executed.
"""
@module de.stschnell
@author Stefan Schnell <mail@stefan-schnell.de>
@license MIT
@version 0.1.0
@runtime python:3.11
@inputType in_host {string}
@outputType Properties
"""
import json
import os
import shutil
import subprocess
import tempfile
def handler(context: dict, inputs: dict) -> dict:
result: dict = {}
outputs: dict = {}
try:
pathName: str = os.path.dirname(os.path.abspath(__file__))
exeName: str = pathName + "/traceroute"
# Copies the traceroute ELF executable from the zip container
# to the temporary directory.
shutil.copy(exeName, tempfile.gettempdir())
# Changes the mode of the traceroute so that it can be executed.
proc: subprocess.CompletedProcess = subprocess.run(
["chmod", "777", tempfile.gettempdir() + "/traceroute"],
encoding = "utf-8",
stdout = subprocess.PIPE
)
# Executes traceroute with the passed domain or IP address, with
# the using of the Internet Control Message Protocol (ICMP).
# ICMP can be used to avoid conflicts with firewall settings.
proc = subprocess.run(
[tempfile.gettempdir() + "/traceroute", "-I", inputs["in_host"]],
encoding = "utf-8",
stdout = subprocess.PIPE
)
print(proc.stdout)
result = {"result": proc.stdout}
outputs = {
"status": "done",
"error": None,
"result": result
}
except Exception as err:
outputs = {
"status": "incomplete",
"error": repr(err),
"result": result
}
return outputs
|
The following example shows traceroute-2.1.3-1.ph4 on VCF Automation 9.0.0.
Conclusion
Using binary executable files in Python VCF Automation runtime environment is very easy. This applies to executables that are available as well as those which are embedded in a zip container, wich is available as action. This opens up new possibilities for further functional perspectives.
References