In het geval u een oud legacysysteem heeft waarop nog Python 2 wordt gebruikt en u een script wilt maken om bepaalde commando’s uit te voeren in een client-servernetwerk.
import subprocess
def ssh_exec_command(hostname, username, private_key_path, command):
ssh_cmd = ['ssh', '-i', private_key_path, '-o', 'StrictHostKeyChecking=no', f'{username}@{hostname}', command]
ssh_process = subprocess.Popen(
ssh_cmd,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
output, error = ssh_process.communicate()
if error:
print("Error:", error)
else:
print("Output:", output)
# Vervang deze door uw werkelijke inloggegevens en commando
hostname = 'remote_host_address'
username = 'your_username'
private_key_path = '/path/to/your/private_key'
command = 'ls -l'
ssh_exec_command(hostname, username, private_key_path, command)
