-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsshauto.py
69 lines (50 loc) · 1.91 KB
/
sshauto.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import subprocess
import json
import argparse
from getpass import getpass as gp
import logging.handlers
def load_data(file="demo.json"):
data_file = file
with open(data_file) as config_file:
data = json.loads(config_file.read())
return data
if __name__ == "__main__":
password = None
parser = argparse.ArgumentParser()
parser.add_argument("--conf", help="Server configuration file")
args = parser.parse_args()
logFormatter = logging.Formatter("%(asctime)s [%(threadName)-12.12s] [%(levelname)-5.5s] %(message)s")
rootLogger = logging.getLogger()
rootLogger.setLevel(logging.INFO)
fileHandler = logging.FileHandler(f"log.log")
fileHandler.setFormatter(logFormatter)
rootLogger.addHandler(fileHandler)
consoleHandler = logging.StreamHandler()
consoleHandler.setFormatter(logFormatter)
rootLogger.addHandler(consoleHandler)
d = load_data(args.conf)
askonce_entered = False
for entry in d:
user = entry["username"]
if not askonce_entered:
password = entry["password"]
if password == "askonce":
password = gp("Enter your password: ")
askonce_entered = True
elif password == "ask":
password = gp("Enter your password: ")
ip = entry["ip"]
commands = entry["scripts"]
for command in commands:
try:
rootLogger.info(command)
rootLogger.info(f"Working on {ip}, {command}")
command_line = f"plink {ip} -l {user} -pw {password} -m {command} -batch"
pipe = subprocess.Popen(
command_line, shell=True, stdout=subprocess.PIPE
).stdout
output = pipe.read().decode()
pipe.close()
rootLogger.info(output)
except Exception as e:
rootLogger.info(f"Error: {e}")