Skip to content

Commit

Permalink
added executing bash commands remotely tutorial
Browse files Browse the repository at this point in the history
  • Loading branch information
x4nth055 committed Nov 11, 2019
1 parent 5a8ee08 commit 3ad3f8b
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
8 changes: 8 additions & 0 deletions general/execute-ssh-commands/README.md
Original file line number Diff line number Diff line change
@@ -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
```
37 changes: 37 additions & 0 deletions general/execute-ssh-commands/execute_bash.py
Original file line number Diff line number Diff line change
@@ -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()
34 changes: 34 additions & 0 deletions general/execute-ssh-commands/execute_commands.py
Original file line number Diff line number Diff line change
@@ -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()
1 change: 1 addition & 0 deletions general/execute-ssh-commands/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
paramiko
4 changes: 4 additions & 0 deletions general/execute-ssh-commands/script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
cd Desktop
mkdir test_folder
cd test_folder
echo "$PATH" > path.txt

0 comments on commit 3ad3f8b

Please sign in to comment.