0

I'm writing a quick python script wrapper to query our crashplan server so I can gather data from multiple sites then convert that to json for a migration and I've got most of it done. It's probably a bit ugly, but I'm one step away from getting the data I need to pass on to the json module so I can format the data I need for reports.

The script should query ldap, get a list of names from a list of sites, then create a command (which works).

But when printing the list in a for loop it prints out each character, instead of each name. If I just print the list it prints out each name on a single line. This obviously munges up the REST call as the username isn't right.

'''
Crashplan query script

Queries the crashplan server using subprocess calls and formats the output

'''

import subprocess
import json

password = raw_input("What password do you want to use: ")

sitelist = ['US - DC - Washington', 'US - FL - Miami', 'US - GA - Atlanta', 'CA - Toronto']
cmdsites = ""


for each in sitelist:
    cmdsites = cmdsites + '(OfficeLocation={})'.format(each)

ldap_cmd = "ldapsearch -xLLL -S OfficeLocation -h ldap.local.x.com -b cn=users,dc=x,dc=com '(&(!(gidNumber=1088))(|%s))' | grep -w 'uid:' | awk {'print $2'}" % cmdsites

users = subprocess.check_output([ldap_cmd], shell=True)

##### EVERYTHING WORKS UP TO THIS POINT #####

for each in users:
    #    subprocess.call(['curl -X GET -k -u "admin:'+password+'" "https://crashplan.x.com:4285/api/User?username='+each+'@x.com&incBackupUsage=true&strKey=lastBackup"'], shell=True)   ### THIS COMMAND WORKS IT JUST GETS PASSED THE WRONG USERNAME
    print each           #### THIS PRINTS OUT ONE LETTER PER LINE ####

print type(users)   #### THIS PRINTS OUT ONE NAME PER LINE ####
Chris Martin
  • 30,334
  • 10
  • 78
  • 137
Chris Gleason
  • 149
  • 2
  • 9
  • See, `users` is a string, so if you loop on it, you're bound to get one letter every time. Rather `split` the string and then run the loop – ishaan Jan 23 '16 at 08:38
  • Oops. Just FYI that last line was a test of mine. It was supposed to be print (users) . I was trying to see what type was being passed. – Chris Gleason Jan 23 '16 at 08:45
  • I'm not talking about the line, but the loop. The `each` variable will only receive a letter and some whitespaces because you're looping on a string. Try the answer provided by @alecxe – ishaan Jan 23 '16 at 08:47

1 Answers1

4

You get an output as a string which, when iterated, produce one character per iteration.

You should split it by line breaks:

for each in users.splitlines():  
    print each
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • That was it. Thanks! Now I've just got to figure out the json printing! Cheers! I'll makes your's the answer when I can (it's making me wait 8 minutes to do it) – Chris Gleason Jan 23 '16 at 08:43