r2555: Start of a rpcclient type program.
[Samba/gebeck_regimport.git] / source4 / scripting / swig / rpcclient
blob2a3d12cc8e942f533e2f7ba242b8e7bc36cf23b2
1 #!/usr/bin/python
3 import sys, os, string
4 from cmd import Cmd
5 from optparse import OptionParser
8 import dcerpc, samr
10 class rpcclient(Cmd):
12 prompt = 'rpcclient$ '
14 def __init__(self, binding, domain, username, password):
15 Cmd.__init__(self)
16 self.binding = binding
17 self.domain = domain
18 self.username = username
19 self.password = password
21 def emptyline(self):
23 # Default for empty line is to repeat last command - yuck
25 pass
27 def onecmd(self, line):
29 # Override the onecmd() method so we can trap error returns
31 try:
32 Cmd.onecmd(self, line)
33 except dcerpc.NTSTATUS, arg:
34 print 'The command returned an error: %s' % arg[1]
36 # Command handlers
38 def do_help(self, line):
39 """Displays on-line help for rpcclient commands."""
40 Cmd.do_help(self, line)
42 def do_shell(self, line):
44 status = os.system(line)
46 if os.WIFEXITED(status):
47 if os.WEXITSTATUS(status) != 0:
48 print 'Command exited with code %d' % os.WEXITSTATUS(status)
49 else:
50 print 'Command exited with signal %d' % os.WTERMSIG(status)
52 def do_EOF(self, line):
53 """Exits rpcclient."""
54 print
55 sys.exit(0)
57 # SAMR pipe commands
59 def do_SamrEnumDomains(self, line):
60 """Enumerate domain names."""
62 usage = 'usage: SamrEnumDomains'
64 if line != '':
65 print usage
66 return
68 pipe = dcerpc.pipe_connect(
69 self.binding,
70 dcerpc.DCERPC_SAMR_UUID, dcerpc.DCERPC_SAMR_VERSION,
71 self.domain, self.username, self.password)
73 connect_handle = samr.Connect(pipe)
75 for i in connect_handle.EnumDomains():
76 print i
78 def do_SamrLookupDomain(self, line):
79 """Return the SID for a domain."""
81 usage = 'SamrLookupDomain DOMAIN'
83 parser = OptionParser(usage)
84 options, args = parser.parse_args(string.split(line))
86 if len(args) != 1:
87 print 'usage:', usage
88 return
90 pipe = dcerpc.pipe_connect(
91 self.binding,
92 dcerpc.DCERPC_SAMR_UUID, dcerpc.DCERPC_SAMR_VERSION,
93 self.domain, self.username, self.password)
95 connect_handle = samr.Connect(pipe)
97 print connect_handle.LookupDomain(args[0])
99 if __name__ == '__main__':
101 # Parse command line
103 usage = 'rpcclient BINDING [options]'
105 if len(sys.argv) == 1:
106 print usage
107 sys.exit(1)
109 binding = sys.argv[1]
110 del(sys.argv[1])
112 if string.find(binding, ':') == -1:
113 binding = 'ncacn_np:' + binding
115 parser = OptionParser(usage)
117 parser.add_option('-U', '--username', action='store', type='string',
118 help='Use given credentials when connecting',
119 metavar='DOMAIN\\username%password',
120 dest='username')
122 parser.add_option('-c', '--command', action='store', type='string',
123 help='Execute COMMAND', dest='command')
125 options, args = parser.parse_args()
127 # Break --username up into domain, usernamd and password
129 if not options.username:
130 options.username = '%'
132 domain = ''
133 if string.find(options.username, '\\') != -1:
134 domain, options.username = string.split(options.username, '\\')
136 password = ''
137 if string.find(options.username, '%') != -1:
138 options.username, password = string.split(options.username, '%')
140 username = options.username
142 # Run command loop
144 c = rpcclient(binding, domain, username, password)
146 if options.command:
147 c.onecmd(options.command)
148 sys.exit(0)
150 while 1:
151 try:
152 c.cmdloop()
153 except KeyboardInterrupt:
154 print 'KeyboardInterrupt'