From 3ad3f8b66cc223560bf21c752282357357254ae8 Mon Sep 17 00:00:00 2001 From: x4nth055 Date: Mon, 11 Nov 2019 14:22:09 +0100 Subject: [PATCH] added executing bash commands remotely tutorial --- README.md | 1 + general/execute-ssh-commands/README.md | 8 ++++ general/execute-ssh-commands/execute_bash.py | 37 +++++++++++++++++++ .../execute-ssh-commands/execute_commands.py | 34 +++++++++++++++++ general/execute-ssh-commands/requirements.txt | 1 + general/execute-ssh-commands/script.sh | 4 ++ 6 files changed, 85 insertions(+) create mode 100644 general/execute-ssh-commands/README.md create mode 100644 general/execute-ssh-commands/execute_bash.py create mode 100644 general/execute-ssh-commands/execute_commands.py create mode 100644 general/execute-ssh-commands/requirements.txt create mode 100644 general/execute-ssh-commands/script.sh diff --git a/README.md b/README.md index dfd35b42..9e8ac121 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy - [How to Extract PDF Tables in Python](https://www.thepythoncode.com/article/extract-pdf-tables-in-python-camelot). ([code](general/pdf-table-extractor)) - [How to Use Pickle for Object Serialization in Python](https://www.thepythoncode.com/article/object-serialization-saving-and-loading-objects-using-pickle-python). ([code](general/object-serialization)) - [How to Automate your VPS or Dedicated Server Management in Python](https://www.thepythoncode.com/article/automate-veesp-server-management-in-python). ([code](general/automating-server-management)) + - [How to Execute BASH Commands in a Remote Machine in Python](https://www.thepythoncode.com/article/executing-bash-commands-remotely-in-python). ([code](general/execute-ssh-commands)) - ### [Web Scraping](https://www.thepythoncode.com/topic/web-scraping) - [How to Access Wikipedia in Python](https://www.thepythoncode.com/article/access-wikipedia-python). ([code](web-scraping/wikipedia-extractor)) diff --git a/general/execute-ssh-commands/README.md b/general/execute-ssh-commands/README.md new file mode 100644 index 00000000..5a5dd06b --- /dev/null +++ b/general/execute-ssh-commands/README.md @@ -0,0 +1,8 @@ +# [How to Execute BASH Commands in a Remote Machine in Python](https://www.thepythoncode.com/article/executing-bash-commands-remotely-in-python) +To run this: +- `pip3 install -r requirements.txt` +- To execute certain commands, edit `execute_commands.py` on your needs and then execute. +- To execute an entire BASH script (.sh) named `script.sh` for instance on `192.168.1.101` with `test` as username and `abc123` as password: + ``` + python execute_bash.py 192.168.1.101 -u root -p inventedpassword123 -b script.sh + ``` \ No newline at end of file diff --git a/general/execute-ssh-commands/execute_bash.py b/general/execute-ssh-commands/execute_bash.py new file mode 100644 index 00000000..135cef57 --- /dev/null +++ b/general/execute-ssh-commands/execute_bash.py @@ -0,0 +1,37 @@ +import paramiko +import argparse + +parser = argparse.ArgumentParser(description="Python script to execute BASH scripts on Linux boxes remotely.") +parser.add_argument("host", help="IP or domain of SSH Server") +parser.add_argument("-u", "--user", required=True, help="The username you want to access to.") +parser.add_argument("-p", "--password", required=True, help="The password of that user") +parser.add_argument("-b", "--bash", required=True, help="The BASH script you wanna execute") + +args = parser.parse_args() +hostname = args.host +username = args.user +password = args.password +bash_script = args.bash + +# initialize the SSH client +client = paramiko.SSHClient() +# add to known hosts +client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) +try: + client.connect(hostname=hostname, username=username, password=password) +except: + print("[!] Cannot connect to the SSH Server") + exit() + +# read the BASH script content from the file +bash_script = open(bash_script).read() +# execute the BASH script +stdin, stdout, stderr = client.exec_command(bash_script) +# read the standard output and print it +print(stdout.read().decode()) +# print errors if there are any +err = stderr.read().decode() +if err: + print(err) +# close the connection +client.close() diff --git a/general/execute-ssh-commands/execute_commands.py b/general/execute-ssh-commands/execute_commands.py new file mode 100644 index 00000000..4e83bb70 --- /dev/null +++ b/general/execute-ssh-commands/execute_commands.py @@ -0,0 +1,34 @@ +import paramiko + +hostname = "192.168.1.101" +username = "test" +password = "abc123" + +commands = [ + "pwd", + "id", + "uname -a", + "df -h" +] + +# initialize the SSH client +client = paramiko.SSHClient() +# add to known hosts +client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) +try: + client.connect(hostname=hostname, username=username, password=password) +except: + print("[!] Cannot connect to the SSH Server") + exit() + +# execute the commands +for command in commands: + print("="*50, command, "="*50) + stdin, stdout, stderr = client.exec_command(command) + print(stdout.read().decode()) + err = stderr.read().decode() + if err: + print(err) + + +client.close() diff --git a/general/execute-ssh-commands/requirements.txt b/general/execute-ssh-commands/requirements.txt new file mode 100644 index 00000000..aa35c711 --- /dev/null +++ b/general/execute-ssh-commands/requirements.txt @@ -0,0 +1 @@ +paramiko \ No newline at end of file diff --git a/general/execute-ssh-commands/script.sh b/general/execute-ssh-commands/script.sh new file mode 100644 index 00000000..ba4dffe2 --- /dev/null +++ b/general/execute-ssh-commands/script.sh @@ -0,0 +1,4 @@ +cd Desktop +mkdir test_folder +cd test_folder +echo "$PATH" > path.txt