{
	"id": "1b5b25b9-a0ed-4303-943a-f3b879c70fc3",
	"created_at": "2026-04-06T00:09:18.905019Z",
	"updated_at": "2026-04-10T03:24:29.928483Z",
	"deleted_at": null,
	"sha1_hash": "77492042e0e8133e8a4491ff55cf1816de09f283",
	"title": "How to Execute Shell Commands in a Remote Machine in Python - The Python Code",
	"llm_title": "",
	"authors": "",
	"file_creation_date": "0001-01-01T00:00:00Z",
	"file_modification_date": "0001-01-01T00:00:00Z",
	"file_size": 597419,
	"plain_text": "How to Execute Shell Commands in a Remote Machine in Python -\r\nThe Python Code\r\nBy Abdeladim Fadheli\r\nPublished: 2019-11-11 · Archived: 2026-04-05 22:43:47 UTC\r\n  Abdeladim Fadheli · 4 min read · Updated may 2024 · Ethical Hacking\r\nGet a head start on your coding projects with our Python Code Generator. Perfect for those times when you need\r\na quick solution. Don't wait, try it today!\r\nHave you ever wanted to quickly execute certain commands remotely on your Linux machine? or do you want to\r\nroutinely execute some lines of code in your server to automate stuff? In this tutorial, you will learn how to write a\r\nsimple Python script to remotely execute shell commands on your Linux machine.\r\nRELATED: How to Brute-Force SSH Servers in Python.\r\nWe will be using the paramiko library; let's install it:\r\npip3 install paramiko\r\nDefining some connection credentials:\r\nimport paramiko\r\nhostname = \"192.168.1.101\"\r\nusername = \"test\"\r\npassword = \"abc123\"\r\nIn the above code, I've defined the hostname, username, and password, this is my local Linux box, you need to\r\nedit these variables for your case, or you may want to make command-line argument parsing using the argparse\r\nhttps://www.thepythoncode.com/article/executing-bash-commands-remotely-in-python\r\nPage 1 of 6\n\nmodule as we usually do in such tasks.\r\nNote that it isn't safe to connect to SSH using credentials like that. You can configure your SSH listener daemon to\r\naccept only public authentication keys instead of using a password. However, for demonstration purposes, we will\r\nbe using a password.\r\nExecuting Shell Commands\r\nNow, let's create a list of commands you wish to execute on that remote machine:\r\ncommands = [\r\n \"pwd\",\r\n \"id\",\r\n \"uname -a\",\r\n \"df -h\"\r\n]\r\nIn this case, simple commands output useful information about the operating system.\r\nThe below code is responsible for initiating the SSH client and connecting to the server:\r\n# initialize the SSH client\r\nclient = paramiko.SSHClient()\r\n# add to known hosts\r\nclient.set_missing_host_key_policy(paramiko.AutoAddPolicy())\r\ntry:\r\n client.connect(hostname=hostname, username=username, password=password)\r\nexcept:\r\n print(\"[!] Cannot connect to the SSH Server\")\r\n exit()\r\nNow let's iterate over the commands we just defined and execute them one by one:\r\n# execute the commands\r\nfor command in commands:\r\n print(\"=\"*50, command, \"=\"*50)\r\n stdin, stdout, stderr = client.exec_command(command)\r\n print(stdout.read().decode())\r\n err = stderr.read().decode()\r\n if err:\r\n print(err)\r\nHere are my results:\r\nhttps://www.thepythoncode.com/article/executing-bash-commands-remotely-in-python\r\nPage 2 of 6\n\n================================================== pwd ==================================================\r\n/home/test\r\n================================================== id ==================================================\r\nuid=1000(test) gid=0(root) groups=0(root),27(sudo)\r\n================================================== uname -a ==================================================\r\nLinux rockikz 4.17.0-kali1-amd64 #1 SMP Debian 4.17.8-1kali1 (2018-07-24) x86_64 GNU/Linux\r\n================================================== df -h ==================================================\r\nFilesystem Size Used Avail Use% Mounted on\r\nudev 1.9G 0 1.9G 0% /dev\r\ntmpfs 392M 6.2M 386M 2% /run\r\n/dev/sda1 452G 410G 19G 96% /\r\ntmpfs 2.0G 0 2.0G 0% /dev/shm\r\ntmpfs 5.0M 0 5.0M 0% /run/lock\r\ntmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup\r\ntmpfs 392M 12K 392M 1% /run/user/131\r\ntmpfs 392M 0 392M 0% /run/user/1000\r\nAwesome, these commands were successfully executed on my Linux machine!\r\nRelated: Build 35+ Ethical Hacking Scripts \u0026 Tools with Python EBook\r\nExecuting Scripts\r\nNow that you know how to execute commands one by one, let's dive a little bit deeper and execute entire shell\r\n(.sh) scripts.\r\nConsider this script (named \"script.sh\"):\r\ncd Desktop\r\nmkdir test_folder\r\ncd test_folder\r\necho \"$PATH\" \u003e path.txt\r\nAfter the SSH connection, instead of iterating for commands, now we read the content of this script and execute it:\r\n# read the BASH script content from the file\r\nbash_script = open(\"script.sh\").read()\r\n# execute the BASH script\r\nstdin, stdout, stderr = client.exec_command(bash_script)\r\n# read the standard output and print it\r\nprint(stdout.read().decode())\r\n# print errors if there are any\r\nhttps://www.thepythoncode.com/article/executing-bash-commands-remotely-in-python\r\nPage 3 of 6\n\nerr = stderr.read().decode()\r\nif err:\r\n print(err)\r\n# close the connection\r\nclient.close()\r\nexec_command() method executes the script using the default shell (BASH, SH, or any other) and returns\r\nstandard input, standard output, and standard error, respectively. We will read from stdout and stderr if there are\r\nany, and then we will close the SSH connection.\r\nAfter the execution of the above code, a new file test_folder was created in Desktop and got a text file inside that\r\nwhich contained the global $PATH variable:\r\nGET -10% OFF: Build 35+ Ethical Hacking Scripts \u0026 Tools with Python EBook\r\nConclusion\r\nAs you can see, this is useful for many scenarios. For example, you may want to manage your servers only by\r\nexecuting Python scripts remotely; you can do anything you want!\r\nAnd by the way, If you want to run more complex jobs on a remote server, you might want to look into Ansible\r\ninstead.\r\nYou can also use Fabric library, as it is a high-level Python library designed just to execute shell commands\r\nremotely over SSH. It builds on top of Invoke and Paramiko.\r\nFeel free to edit the code as you wish; for example, you may want to parse command-line arguments with\r\nargparse.\r\nIf you're into cyber security, then I highly encourage you to take our Ethical Hacking with Python EBook, where\r\nwe build 35+ hacking tools and scripts from scratch using Python!\r\nREAD ALSO: How to Create a Reverse Shell in Python.\r\nHappy Coding ♥\r\nhttps://www.thepythoncode.com/article/executing-bash-commands-remotely-in-python\r\nPage 4 of 6\n\nFinished reading? Keep the learning going with our AI-powered Code Explainer. Try it now!\r\nView Full Code Analyze My Code\r\nSharing is caring!\r\nRead Also\r\nhttps://www.thepythoncode.com/article/executing-bash-commands-remotely-in-python\r\nPage 5 of 6\n\nComment panel\r\nSource: https://www.thepythoncode.com/article/executing-bash-commands-remotely-in-python\r\nhttps://www.thepythoncode.com/article/executing-bash-commands-remotely-in-python\r\nPage 6 of 6",
	"extraction_quality": 1,
	"language": "EN",
	"sources": [
		"MITRE"
	],
	"references": [
		"https://www.thepythoncode.com/article/executing-bash-commands-remotely-in-python"
	],
	"report_names": [
		"executing-bash-commands-remotely-in-python"
	],
	"threat_actors": [
		{
			"id": "aa73cd6a-868c-4ae4-a5b2-7cb2c5ad1e9d",
			"created_at": "2022-10-25T16:07:24.139848Z",
			"updated_at": "2026-04-10T02:00:04.878798Z",
			"deleted_at": null,
			"main_name": "Safe",
			"aliases": [],
			"source_name": "ETDA:Safe",
			"tools": [
				"DebugView",
				"LZ77",
				"OpenDoc",
				"SafeDisk",
				"TypeConfig",
				"UPXShell",
				"UsbDoc",
				"UsbExe"
			],
			"source_id": "ETDA",
			"reports": null
		}
	],
	"ts_created_at": 1775434158,
	"ts_updated_at": 1775791469,
	"ts_creation_date": 0,
	"ts_modification_date": 0,
	"files": {
		"pdf": "https://archive.orkl.eu/77492042e0e8133e8a4491ff55cf1816de09f283.pdf",
		"text": "https://archive.orkl.eu/77492042e0e8133e8a4491ff55cf1816de09f283.txt",
		"img": "https://archive.orkl.eu/77492042e0e8133e8a4491ff55cf1816de09f283.jpg"
	}
}