|
| 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() |
0 commit comments