February 2008
Monthly Archive
Fri 15 Feb 2008
I get several requests for directory data in CSV format. Until openldap has a csv output option, we will use SED to save the day!
ldapsearch -h localhost -b o=mydomain,c=us uid=sbak* mail telephonenumber
dn: GID=0000001,o=mydomain,c=us
telephoneNumber: 1-734-555-0001
mail: sbak@mydomain.com
dn: GID=0000002,o=mydomain,c=us
telephoneNumber: 1-734-555-0002
mail: sbaker@mydomain.com
If we redirected the above date to a file 11.txt we can than run this command to place a comma at the end of every line:
sed -n '1h;2,$H;${g;s/\n/,/g;p}' 11.txt > 22.txt
Next we will use a simple sed to replace the double comma with a newline:
sed 's/,,/\n/g' 22.txt > 33.csv
Lets look at 33.csv to see the finished product:
sbak@fedoraCore:~ $ cat 33.csv
dn: GID=0000001,o=mydomain,c=us,telephoneNumber: 1-734-555-0001,mail: sbak@mydomain.com
dn: GID=0000002,o=mydomain,c=us,telephoneNumber: 1-734-555-0002,mail: sbaker@mydomain.com
Now you can send your managers data that they can open with excel!
Mon 11 Feb 2008
Jay R. Wren has a great post on how to do this:
querying-active-directory-with-unix-ldap-tools
What Jay did not mention is how to search the Global Catalog (GC). The global catalog contains a partial replica of all objects in the forest. To find Domain Controllers that are serving as Global Catalogs you simply need to query DNS SRV Records. Global Catalog SRV Records take the following form: _ldap._tcp.gc._msdcs.MYINETDOMAIN.com
host -av _ldap._tcp.gc._msdcs.MYINETDOMAIN.com
The key to being able to query active directory from linux machines is DNS. The Host command can be used to look at DNS for AD SRV and A records. These records contain names and IPs of Active Directory Domain Controllers.
The only other information you need is the Global Catalog port: 3268
Putting this all together, you should have:
ldapsearch -xLLL -h IPofGC -p 3268 -b dc=MYINETDOMAIN,dc=com -D ME@MYINETDOMAIN.com -w passwd [filter] [attribute list]
Finally, I have created a shell script called GC.sh that behaves like “ldapsearch”
HOST=`host -av _ldap._tcp.gc._msdcs.MYINETDOMAIN |tail -3 |head -1 |awk '{print $5}'`
echo "using $HOST"
ldapsearch \-xLLL \-h $HOST \-p 3268 \-b dc\=MYINETDOMAIN,dc\=COM \-D "ME@MYINETDOMAIN.com" \-w xxxxxxxx $1 $2 $3 $4 $5 $6
$1 is the filter.
$2-6 are attributes that you wish returned
Leave $2-6 blank if you wish to return all attributes.
Ex: ./GC.sh cn=jrwren hascsharpskills hasmathskills
dn: cn=jrwren,ou=users,dc=myinetdomain,dc=com
hascsharpskills: 1
hasmathskills: 0
Sun 10 Feb 2008
We recently had a baby and take a LOT of pictures. The problem is, depending on which way the stars are aligned, my wife or I will:
- Download pictures with the USB cable provided to her laptop
- Place SD card into the printer of her computer and grab them that way
- Or I will place the SD card into the SD card reader on my PC and grab them
The problem with this is, the pictures are scattered about over 3 machines and I never know where they are. Its was getting increasingly annoying, so I decided to sync the “My Pictures” folder on each of our three windows machines with Rsync, mount, and cron on my Fedora machine. The main idea is to Pull all three “My Pictures” folders to the linux in one central folder. And then push that central folder back out to all three machines.
Step 1:
Mount your windows machine to /mnt/directory
mount -t cifs //192.168.1.2/C$ -o username=USERNAME,password=PASSWORD /mnt/Windows_C/.
Step 2:
use Rsync to grab directory listing and grab only updated or new items.
rsync -aruvz /mnt/Windows_C/Documents\ and\ Settings/steve/My\ Documents/My\ Pictures/Addison/* /home/sbak/AddisonSync/.
Step 3:
Repeat steps 1 and 2 for all machines that you wish to sync.
Step 4:
Push the sync’d folder on the linux machine to all machines that need to be synced. To do this, you simply reverse the source and destination fields of the rsync command in step 2.
rsync -aruvz /home/sbak/AddisonSync/* /mnt/Windows_C/Documents\ and\ Settings/steve/My\ Documents/My\ Pictures/Addison/.
Step 5:
Put the above steps into a shell script. One for pull and one for push for each machine.
PullMachineA.sh:
mount -t cifs //192.168.1.2/C$ -o username=USERNAME,password=PASSWORD /mnt/Windows_C/.
rsync -aruvz /mnt/Windows_C/Documents\ and\ Settings/steve/My\ Documents/My\ Pictures/Addison/* /home/sbak/AddisonSync/.
umount /mnt/Windows_C/
PushMachineB.sh
mount -t cifs //192.168.1.2/C$ -o username=USERNAME,password=PASSWORD /mnt/Windows_C/.
rsync -aruvz /home/sbak/AddisonSync/* /mnt/Windows_C/Documents\ and\ Settings/steve/My\ Documents/My\ Pictures/Addison/.
umount /mnt/Windows_C/
Step 6:
schedule shell scripts in crontab. I scheduled mine every 6 hours.
0 0,6,12,18 * * * (/home/sbak/Syncs/pullA.sh >> /home/sbak/Syncs/logs/AddisonSync.log)
10 0,6,12,18 * * * (/home/sbak/Syncs/pullB.sh >> /home/sbak/Syncs/logs/AddisonSync.log)
20 0,6,12,18 * * * (/home/sbak/Syncs/pullC.sh >> /home/sbak/Syncs/logs/AddisonSync.log)
30 0,6,12,18 * * * (/home/sbak/Syncs/pushA.sh >> /home/sbak/Syncs/logs/AddisonSync.log)
40 0,6,12,18 * * * (/home/sbak/Syncs/pushB.sh >> /home/sbak/Syncs/logs/AddisonSync.log)
50 0,6,12,18 * * * (/home/sbak/Syncs/pushC.sh >> /home/sbak/Syncs/logs/AddisonSync.log)