<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>keys on Notities</title><link>https://www.vandenboom.icu/en/tags/keys/</link><description>Recent content in keys on Notities</description><generator>Hugo -- gohugo.io</generator><language>en</language><lastBuildDate>Wed, 03 Apr 2024 20:20:39 +0000</lastBuildDate><atom:link href="https://www.vandenboom.icu/en/tags/keys/index.xml" rel="self" type="application/rss+xml"/><item><title>SSH in remote machine with SSH keys and run command in new Python 3</title><link>https://www.vandenboom.icu/en/blog/2024-ssh-in-remote-machine-with-ssh-keys-and-run-command-in-new-python-3/</link><pubDate>Wed, 03 Apr 2024 20:20:39 +0000</pubDate><guid>https://www.vandenboom.icu/en/blog/2024-ssh-in-remote-machine-with-ssh-keys-and-run-command-in-new-python-3/</guid><description>T﻿o make a script to run remote commands in a client server network.
import subprocess def ssh_exec_command(hostname, username, command): ssh_cmd = ['ssh', f'{username}@{hostname}', command] ssh_process = subprocess.Popen( ssh_cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True ) output, error = ssh_process.communicate() if error: print(&amp;quot;Error:&amp;quot;, error) else: print(&amp;quot;Output:&amp;quot;, output) # Replace these with your actual credentials and command hostname = 'remote_host_address' username = 'your_username' command = 'ls -l' ssh_exec_command(hostname, username, command)</description></item><item><title>SSH in remote machine with SSH keys and run command in old Python 2</title><link>https://www.vandenboom.icu/en/blog/2024-ssh-in-remote-machine-with-ssh-keys-and-run-command-in-old-python/</link><pubDate>Wed, 03 Apr 2024 20:12:58 +0000</pubDate><guid>https://www.vandenboom.icu/en/blog/2024-ssh-in-remote-machine-with-ssh-keys-and-run-command-in-old-python/</guid><description>I﻿n case you have an old legacy system with Python 2 still in use and want to make a script to run certain commands in a client server network.
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(&amp;quot;Error:&amp;quot;, error) else: print(&amp;quot;Output:&amp;quot;, output) # Replace these with your actual credentials and command 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)</description></item></channel></rss>