-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrunscript_sm.py
More file actions
executable file
·68 lines (53 loc) · 2.68 KB
/
runscript_sm.py
File metadata and controls
executable file
·68 lines (53 loc) · 2.68 KB
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
#!/usr/bin/env python3
"""
SUSE Manager / Uyuni API - Run Script (Bulk Mode)
Reads a list of hosts, executes script, writes Job IDs to CSV.
"""
import argparse, xmlrpc.client, ssl, getpass, sys, datetime, os, csv
def main():
p = argparse.ArgumentParser()
p.add_argument('-s', '--server'); p.add_argument('--url'); p.add_argument('-u', '--user', required=True)
p.add_argument('-p', '--password')
p.add_argument('--hosts', required=True, help="File containing hostnames (one per line)")
p.add_argument('--script', required=True, help="File containing the script to execute")
p.add_argument('--verify', action='store_true')
args = p.parse_args()
if args.url: api_url = args.url
elif args.server: api_url = f"https://{args.server}/rpc/api"
else: print("[!] Error: Provide -s/--server or --url"); sys.exit(1)
# Read Script
if not os.path.exists(args.script): print(f"[!] Script file {args.script} not found."); sys.exit(1)
with open(args.script, 'r') as f: script_content = f.read()
# Read Hosts
if not os.path.exists(args.hosts): print(f"[!] Hosts file {args.hosts} not found."); sys.exit(1)
with open(args.hosts, 'r') as f: target_hosts = [line.strip() for line in f if line.strip()]
pwd = args.password or getpass.getpass()
ctx = ssl.create_default_context()
if not args.verify: ctx.check_hostname=False; ctx.verify_mode=ssl.CERT_NONE
try:
c = xmlrpc.client.ServerProxy(api_url, context=ctx); k = c.auth.login(args.user, pwd)
print(f"[*] Authenticated. Processing {len(target_hosts)} hosts...")
jobs = []
for host in target_hosts:
print(f"[*] resolving {host}...")
# Resolve ID
try:
systems = c.system.getId(k, host)
if not systems:
print(f"[-] Host {host} not found.")
continue
sid = systems[0].get('id')
print(f" -> ID: {sid}. Scheduling script...")
aid = c.system.scheduleScriptRun(k, sid, "root", "root", 600, script_content, datetime.datetime.now())
print(f" -> Job ID: {aid}")
jobs.append([host, aid])
except Exception as e:
print(f"[!] Error processing {host}: {e}")
# Write Output
with open('jobs.csv', 'w', newline='') as f:
writer = csv.writer(f)
writer.writerows(jobs)
print(f"\n[+] Done. {len(jobs)} jobs scheduled. Saved to 'jobs.csv'.")
c.auth.logout(k)
except Exception as e: print(f"Global Error: {e}")
if __name__ == "__main__": main()