1
#!/usr/bin/python
import os

readLine = open('desktops.txt','r')

for line in readLine:
        machineName = line
        query = os.system('wmic -U corp.fakedomain.com/domainusername%password //192.168.1.100 "Select * from Win32_UserAccount Where LocalAccount = True"|grep "500|"|cut -d "\\" -f 2|cut -d "|" -f1')

I am receiving the following error...

myhostname:~# ./getLocalAdminNames.py sh: Syntax error: Unterminated quoted string sh: Syntax error: Unterminated quoted string sh: Syntax error: Unterminated quoted string sh: Syntax error: Unterminated quoted string sh: Syntax error: Unterminated quoted string

Then after I resolve the error I would like to substitute the IP with machineName variable.

Any help would be greatly appreciated.

C Dubya
  • 135
  • 1
  • 3
  • 10
  • Try printing the string you send to `os.system` to find out if you got the quoting right. Or better, just run `wmic` and do the rest (`grep` and `cut`) in Python, it'll make your life a lot easier. – Fred Foo Aug 23 '13 at 19:19

1 Answers1

0

You should escape your backslashes. Replace \\ with \\\\:

os.system('wmic -U corp.fakedomain.com/domainusername%password //192.168.1.100 "Select * from Win32_UserAccount Where LocalAccount = True"|grep "500|"|cut -d "\\\\" -f 2|cut -d "|" -f1')

or, make your string raw by adding r prefix:

os.system(r'wmic -U corp.fakedomain.com/domainusername%password //192.168.1.100 "Select * from Win32_UserAccount Where LocalAccount = True"|grep "500|"|cut -d "\\" -f 2|cut -d "|" -f1')

Also see:

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • Thank you! That worked but now each machine in that variable will be a hostname. I seem to be having a problem using the following... os.system(r'wmic -U corp.fakedomain.com/domainuser%domainpass //'+machineName+' "Select * from Win32_UserAccount Where LocalAccount = True"|grep "500|"|cut -d "\\" -f 2|cut -d "|" -f1') – C Dubya Aug 23 '13 at 19:57