forked from x4nth055/pythoncode-tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added executing bash commands remotely tutorial
- Loading branch information
Showing
6 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
paramiko |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |