Modify LDAP record entry

Problem

You want to modify or change a record in LDAP.

Supplanting one value with another.



Solution

Use ldapmodify from the command line.

Again take a backup with -L – just to be sure. ๐Ÿ™‚



Example


Here is an example of modifying a record in LDAP, when you need to modify an entry to an existing record:


ldapmodify -x -v-Dโ€cn=Manager,dc=demo,dc=netโ€-w secret<<EOT
dn: cn=jdoe,dc=demo,dc=net
changetype: modify
replace: mail
mail: [email protected]
EOT



Reference

[tags]ldapmodify, modify ldap record, LDAP Training School[/tags]



Beginning ldap – modify a record

Problem

You want to modify an LDAP record.

For example change telephone number, address, etc.



Solution

Use ldapmodify from command line. I might seem daunting to start with, but it is the best way.

Plus you should perform a search with -L option, to take a backup to file.



Example


Here is an example of modifying a record in LDAP, when you need to add an entry to an existing record:


ldapmodify -x -v-Dโ€cn=Manager,dc=demo,dc=netโ€-w secret<<EOT
dn: cn=jdoe,dc=demo,dc=net
changetype: modify
add: mail
mail: mail: [email protected]
EOT

Here we are adding the mail field and value.



Reference

[tags]ldapmodify, adding field to ldap record, LDAP Training School[/tags]



Adding LDAP record

Problem

You want to add an LDAP record. Effectively create a record.



Solution

To add a record to LDAP, you simply run an ldapmodify with the -a flag.



Example



ldapmodify -x -a -v-Dโ€cn=Manager,dc=demo,dc=netโ€-w secret < ldifFile

Where ldifFile is a file either hand crafted or generated with ldapsearch -L.



Reference

[tags]ldapadd, ldapmodify, LDAP Training School[/tags]



Compare command output with file

Problem

You want to run a command, save the output to a file – then compare the output at a later date with the file.

For example – you want to update your crontab: save a backup crontab -l > cronfile.$(date +%j), make your updates, then compare it with the backup file.



Solution

Use diff with the hypen to represent read from stdin.

This saves keeping multiple copies.



Example



crontab -l > cronfile.$(date +%j)
crontab -e # make your diffs
crontab -l | diff - cronfile.$(date +%j)

Also you can use cmp, which just reports the line number, etc


crontab -l > cronfile.$(date +%j)
crontab -e # make your diffs
crontab -l | cmp - cronfile.$(date +%j)



Reference

[tags]crontab, diff, cmp, comparing command output with file, UNIX Coding School[/tags]



Simple Basic UNIX Permission Denied

Problem

You are quite happily editing away, then you come to save … oh dear, permission denied.

Either the file is set to readable, or it is owned by another user.

But you have write permission to the directory, either by user or group!



Solution

If it is just a simple case of the file being unwritable and you own it, if in vi following the first example.

If you are not the owner, write the file to /tmp. Then follow the second example.



Example


Allow write permission to a file you are currently vi editing – but the file is not writable.

:!chmod u+w %

If you are not the owner, no need to worry, even without root we can change the owner if you have write access to the directory! ๐Ÿ™‚


:!zsh
mv -i your_filename /tmp/your_filename.mv
cp -i /tmp/your_filename.mv your_filename

Obviously if not in vi, you'll need to break out to a shell using the mechanism in your editor. Also you may not have zsh, so ksh – bash, etc will do.

I tend to do all this on the command line, with a semi-colon between the mv and cp – so I can use command line expansion (the move has not happened yet!).

mv -i thefile[tab] /tmp/thefileXXXX;cp -i /tmp/thefileXXXX . [return]





Network sniffing on Linux

Problem

Just what the hell is your apache web server doing!! ๐Ÿ™‚

Seeing how clients are connecting to servers and what data is passing between, can prove invaluable in finding the solutions to issues.



Solution

Network trace it – showing the headers and content being sent and received.



Example



tcpdump -vvv -A -XX host 10.0.0.8 and port 80

Just replace 10.0.0.8 with your client ip address.

tcpdump -c 50 port 80
tcpdump: listening on eth0
00:43:09.262942 10.0.0.8.1288 > bree.http: S 2445348839:2445348839(0) win 16384
00:43:09.263069 bree.http > 10.0.0.8.1288: S 3557400364:3557400364(0) ack 2445348840 win 5840 (DF)
00:43:09.265882 10.0.0.8.1288 > bree.http: . ack 1 win 17520
00:43:09.268591 10.0.0.8.1288 > bree.http: P 1:493(492) ack 1 win 17520

On AIX you can ordinarly use iptrace or tcpdump.

On Solaris, we simply use snoop.
snoop -x0 src hostname

Where source is the client and hostname is the server. Other options are port portnum, etc. snoop –help or man snoop for more options.



Reference

[tags]Network, Tracing, tcpdump, Linux[/tags]



Removing links to missing files

Problem

You have many symbolic links that point to missing files.

I guess it is all just about good housekeeping! ๐Ÿ™‚ Well and the fact your application might break, depending on something being there, that is not.



Solution

This bit of shell under example tab, deals with them swiftly and sharply. ๐Ÿ™‚

Effectively it just does a find from the current directory, then loops around looking for missing files.



Example



for na in `find . -type l`
do
ls -lLd $na 2>&1 | grep "No such file" |
while read junk delme junk
do
rm ${delme%:}
done
done



Reference

[tags]symbolic links, find, rm, while, UNIX Coding School[/tags]



compressed using zip gzip bzip2

Problem

You have been sent (or want to download) a zip file. But also want to verify the contents, before decompressing.



Solution

Under UNIX there are a number of ways to compress files, or deal with those that have already been compressed.

See the examples tab.



Example


  • Firstly dealing with files ending with .gz.

    To view the contents of a gzipped file, just use gzip -l filename.gz

    To unzip the file, use gzip -d filename.gz or gunzip filename.gz

    To view the contents of the gzipped file, without decompressing it: gzip -c -d filename.gz.

    To create a gzip – gzip –best filename – or gzip reads from stdin, in the best of UNIX traditions. ๐Ÿ™‚ You can also pipe gzip output or redirect it.

  • Next unzip.

    To view the contents of a zipped file, just use zipinfo filename.zip

    To unzip the file, use unzip filename.zip.

    To view the contents of the gzipped file, without decompressing it: unzip -c filename.gz.

    To create a zip – zip filename – or zip reads from stdin.

  • Lastly bzip.

    To decompress the file, use bunzip2 filename.bz2.

    To view the contents of the bzipped file, without decompressing it: bzcat filename.bz2.

    To create a zip – bzip2 filename – or zip reads from stdin.



Reference

[tags]gzip, zip, bzip2, UNIX Coding School[/tags]



Strip XML comments

Problem

You want to strip XML comments out of your XML file.

This should also work with HTML comments.

Very handy for spotting errors, especially in configuration files! ๐Ÿ™‚



Solution

In this example, I use pure shell commands to identify and remove comments.

Obviously it assumes comments are not embedded, within valid XML, but on their own line.

I’ve done some brief testing, appears to work fine so far.

Have n’t tried nested comments either. ๐Ÿ™‚



Example



#!/bin/bash

[ $# -ne 1 ] && { echo "Usage: $0 file.xml"; exit 1; }

file=$1

[ ! -f $file ] && {
echo "$0: $file is not a file, exiting ..."; exit 1
}

flag=0

cat $file | while read line
do
[[ $(echo "$line" | grep -c -- '<!--') -eq 1 ]]
&& { flag=1 }

[[ $(echo "$line" | grep -c -- '-->') -eq 1 ]]
&& { flag=2 }

[ $flag -eq 0 ] && { echo "$line" } ||
[ $flag -eq 2 ] && { flag=0 }

done

exit 0

Drop me a comment, with your experiences of it.



Reference

[tags]Strip XML Comments, UNIX Coding School[/tags]



catch unix process – ps

Problem

You want to catch a process, but they just shoot by too quickly.

Every time you run a ps, you cannot spot that specific command.

You need a capture script.



Solution

Run the code at the example and it will catch changing and new processes. To be more aggressive, just change “sleep 5” to “sleep 1”.



Example


user=โ€marcusโ€

ps โ€“fu $user > catchdiff

while :

do

ps -fu $user | diff - catchdiff | egrep -v 'catchdiff|-bash$|^---|^[0-9]'

sleep 5

done



Reference

[tags]ps, diff, egrep, while, process[/tags]