{"id":351,"date":"2022-07-25T00:00:00","date_gmt":"2022-07-25T00:00:00","guid":{"rendered":"https:\/\/tac.debuzzify.com\/?p=351"},"modified":"2023-06-27T01:18:41","modified_gmt":"2023-06-27T01:18:41","slug":"python-ssh-shell-commands","status":"publish","type":"post","link":"https:\/\/www.the-analytics.club\/python-ssh-shell-commands\/","title":{"rendered":"How To Execute Shell Commands Over SSH Using Python?"},"content":{"rendered":"\n\n\n

A prevalent task when automating the boring stuff in Python is to run shell commands. If you are working with servers or virtual machines, you’d also need to run commands on a remote computer.<\/p>\n\n\n\n

You can use the standard Python module and subprocesses to run shell scripts<\/a>. It’s an easy way to execute commands. But on remote computers, you may need other techniques.<\/p>\n\n\n\n

This post walks you through executing raw shell scripts using ssh. Then we’ll also discuss more Pythonic ways to run scripts<\/a> on remote computers.<\/p>\n\n\n\n

\n
\n
\n

Grab your aromatic coffee <\/a>(or tea<\/a>) and get ready…!<\/p>\n<\/div>\n<\/div>\n<\/div>\n\n\n\n

Running SSH commands using the subprocess module.<\/h2>\n\n\n\n

The subprocess module needs no further installation. It’s a standard Python library. Hence, you can use the ssh command line utility<\/a> inside your subprocess run method.<\/p>\n\n\n\n

The following command will run<\/a> the Linux shell and get the free memory information of a remote computer. When running this script, the system will ask you to type in your password.<\/p>\n\n\n\n

Note that the subprocess run command<\/a> takes a list of arguments, not a string. But you can also use iterable such as a tuple<\/a> or a set in place of a list.<\/p>\n\n\n\n