Skip to content

Commit 3ad3f8b

Browse files
committed
added executing bash commands remotely tutorial
1 parent 5a8ee08 commit 3ad3f8b

File tree

6 files changed

+85
-0
lines changed

6 files changed

+85
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
5353
- [How to Extract PDF Tables in Python](https://www.thepythoncode.com/article/extract-pdf-tables-in-python-camelot). ([code](general/pdf-table-extractor))
5454
- [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))
5555
- [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))
56+
- [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))
5657

5758
- ### [Web Scraping](https://www.thepythoncode.com/topic/web-scraping)
5859
- [How to Access Wikipedia in Python](https://www.thepythoncode.com/article/access-wikipedia-python). ([code](web-scraping/wikipedia-extractor))
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# [How to Execute BASH Commands in a Remote Machine in Python](https://www.thepythoncode.com/article/executing-bash-commands-remotely-in-python)
2+
To run this:
3+
- `pip3 install -r requirements.txt`
4+
- To execute certain commands, edit `execute_commands.py` on your needs and then execute.
5+
- 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:
6+
```
7+
python execute_bash.py 192.168.1.101 -u root -p inventedpassword123 -b script.sh
8+
```
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import paramiko
2+
import argparse
3+
4+
parser = argparse.ArgumentParser(description="Python script to execute BASH scripts on Linux boxes remotely.")
5+
parser.add_argument("host", help="IP or domain of SSH Server")
6+
parser.add_argument("-u", "--user", required=True, help="The username you want to access to.")
7+
parser.add_argument("-p", "--password", required=True, help="The password of that user")
8+
parser.add_argument("-b", "--bash", required=True, help="The BASH script you wanna execute")
9+
10+
args = parser.parse_args()
11+
hostname = args.host
12+
username = args.user
13+
password = args.password
14+
bash_script = args.bash
15+
16+
# initialize the SSH client
17+
client = paramiko.SSHClient()
18+
# add to known hosts
19+
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
20+
try:
21+
client.connect(hostname=hostname, username=username, password=password)
22+
except:
23+
print("[!] Cannot connect to the SSH Server")
24+
exit()
25+
26+
# read the BASH script content from the file
27+
bash_script = open(bash_script).read()
28+
# execute the BASH script
29+
stdin, stdout, stderr = client.exec_command(bash_script)
30+
# read the standard output and print it
31+
print(stdout.read().decode())
32+
# print errors if there are any
33+
err = stderr.read().decode()
34+
if err:
35+
print(err)
36+
# close the connection
37+
client.close()
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import paramiko
2+
3+
hostname = "192.168.1.101"
4+
username = "test"
5+
password = "abc123"
6+
7+
commands = [
8+
"pwd",
9+
"id",
10+
"uname -a",
11+
"df -h"
12+
]
13+
14+
# initialize the SSH client
15+
client = paramiko.SSHClient()
16+
# add to known hosts
17+
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
18+
try:
19+
client.connect(hostname=hostname, username=username, password=password)
20+
except:
21+
print("[!] Cannot connect to the SSH Server")
22+
exit()
23+
24+
# execute the commands
25+
for command in commands:
26+
print("="*50, command, "="*50)
27+
stdin, stdout, stderr = client.exec_command(command)
28+
print(stdout.read().decode())
29+
err = stderr.read().decode()
30+
if err:
31+
print(err)
32+
33+
34+
client.close()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
paramiko
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
cd Desktop
2+
mkdir test_folder
3+
cd test_folder
4+
echo "$PATH" > path.txt

0 commit comments

Comments
 (0)