Friday, June 14, 2013

Check if a DN on a M$ Active Directory existed

I will use the ssh_client module I wrote (http://iambusychangingtheworld.blogspot.com/2013/05/manipulate-windows-machines-from-linux.html) to execute M$ Windows Active Directory command line remotely from my Ubuntu.

#! /usr/bin/python
import ssh_client

def dn_existed(dn, type_of_dn, ssh_client):

# type_of_dn is group, ou or user
command = 'dsget ' + type_of_dn + ' "' + dn + '"'
ssh_client.execute_command(command)

if 'succeeded' in ssh_client.return_str:
return True

return False

if __name__ == "__main__":
       client = ssh_client.MySSHClient()
       client.do_connect()

       dn = 'OU=MYOU,DC=MYGENIUS,DC=COM'
       if dn_existed(dn, 'ou', client):
              print "Existed"
       else:
              print "Not existed"