summaryrefslogtreecommitdiffstats
path: root/source4/scripting/bin/machineaccountpw
blob: eab773ec7c08b175c6acc2853ce44f44f9dcc29c (plain)
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
#!/usr/bin/env python3
import optparse
import sys

# Find right directory when running from source tree
sys.path.insert(0, "bin/python")


import samba
from samba import getopt as options
from samba import NTSTATUSError
from samba.credentials import Credentials
parser = optparse.OptionParser("machineaccountpw")
sambaopts = options.SambaOptions(parser)
parser.add_option_group(sambaopts)
parser.add_option_group(options.VersionOptions(parser))
opts, args = parser.parse_args()

if len(args) != 0:
    parser.print_usage()
    sys.exit(1)

try:
    lp_ctx = sambaopts.get_loadparm()
except RuntimeError as error:
    print("Unable to load smb.conf %s: %s" % (sambaopts.get_loadparm_path(),
                                              error),
          file=sys.stderr)
    sys.exit(1)

creds = Credentials()

creds.guess(lp_ctx)
try:
    creds.set_machine_account(lp_ctx)
except NTSTATUSError as error:
    print("Failed to find a stored machine account credential on this system: %s" \
          % error.args[1],
          file=sys.stderr)
    sys.exit(1)

print(creds.get_password())