Listen on port – client server demo

Problem

You want to listen on a port, maybe to test firewalls! 🙂

Excellent introduction to client server software.



Solution

Another use bit of socket programming, courtesy of Perl! 🙂

Extremely useful bit of Perl, which can be used with the other post on this site, to confirm network connectivity (port scanner).

This code (see example tab) will listen on a port (although be careful it is over 1024, unless you are running as root/admin).



Example



perl -MIO::Socket -e '$srv=IO::Socket::INET->
new(LocalPort=>$ARGV[0],Type=>SOCK_STREAM,Reuse=>1,Listen=>5)

or die "Failed trying to listen on $ARGV[0]n";while($cl=$srv->accept()) { while() { print }

} close($socket);' port

Can also use it as a cheap chat service! 🙂 Everything gets echo'd through – so just telnet host port and type away! 🙂

Also see my port scan code at the reference tab.



Reference

[tags]Perl Socket Programming, Network Programming, Perl Port Listen, Perl Client Server demo, Perl Coding School[/tags]



LDAP LDIF Perl search script

Problem

You want to search an LDIF file for a given dn, or pattern.



Solution

Multi-line pattern search and output – useful for LDIFs! 😉

Written in Perl – see example tab.



Example


Replace pattern to a given name, etc and filename to LDIF output file.

perl -ane '$/="dn" ;

print,"\n\n" if($_ =~/pattern/);' filename

For example:


$ cat user.ldif
dn: cn=user0,dc=subdiv,dc=demo,dc=net
objectClass: person
sn: User
cn: user0
userPassword: today321

dn: cn=user1,dc=subdiv,dc=demo,dc=net
objectClass: person
sn: User
cn: user1
userPassword: today321

$ perl -ane '$/="dn" ;
print,"nn" if($_ =~/user1/);' user.ldif

: cn=user1,dc=subdiv,dc=demo,dc=net
objectClass: person
sn: User
cn: user1
userPassword: today321



Reference

[tags]LDAP LDIF Search, LDAP LDIF extraction, LDAP Training School[/tags]



Modify LDAP records with JNDI

Problem

Need to modify an LDAP record with JNDI, the Java Naming Directory Interface.

Following on from using java to perform LDAP searches, here is a quick demo on modifying records.



Solution

Here I’m using java to modify John Doe’s record, changing the givenname entry to John A.

As you’ll notice all values are hard coded (such as hostname, port, password, etc). These could be read in from a config file, or passed in through command line arguments.



Example



$ cat chLdapDetails.java
import java.util.*;
import java.io.*;
import javax.naming.*;
import javax.naming.directory.*;
import javax.naming.ldap.*;

public class chLdapDetails {

public static void main(String argv[]) {

String url="ldap://127.0.0.1:389";
Hashtable env=new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL,url);
env.put(Context.SECURITY_AUTHENTICATION,"simple");
env.put(Context.SECURITY_PRINCIPAL,
"cn=Manager,dc=demo,dc=net");
env.put(Context.SECURITY_CREDENTIALS,"secret");

try {

DirContext ctx=new InitialDirContext(env);
ModificationItem[] mods = new ModificationItem[1];
mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
new BasicAttribute("givenname","John A"));
ctx.modifyAttributes("cn=jdoe,dc=demo,dc=net", mods);
ctx.close();

} catch(NamingException ne) { System.err.println(ne.toString()); }

}
}

Heres a run through:


$ java getLdapDetails Doe
John, Doe - john.doe@demo.net

$ java chLdapDetails
$ java getLdapDetails Doe
John A, Doe - john.doe@demo.net



Reference

[tags]LDAP updates JNDI, JNDI LDAP, JNDI, LDAP Training School[/tags]



scan network port with perl

Problem

You want to test a network port, on a remote system – over TCP/IP.

Maybe this is a new setup, or you want confirmation it is working.

Perhaps the firewall rules have just been changed! 🙂



Solution

A nice small bit of Perl code that I’ve used thousands of times!

In fact I’m running it in most of my production environments,
as a check that the a process is not only running – but also responding.

Yep, you could just use telnet – but some systems have that taken off for security.

Additionally it is n’t as easy to program telnet – I know, I know – you can with expect. 🙂



Example


Here is just a one liner, but you can easily incorporate this into a script.

perl -MIO::Socket -e '$socket=IO::Socket::INET->
new(Proto=>tcp,PeerAddr=>$ARGV[0],PeerPort=>$ARGV[1]);
if($@) { print "Failed: $@n" } else { print "Succeedn"; }' host port



Reference

[tags]Perl, Network, TCPIP, Port Scanner, Perl Coding School[/tags]



Obtain epoch time and calculate date yesterday

Problem

You want to capture the current epoch. Maybe to use in a log file, or as a filename.

Or maybe you want to calculate the date yesterday.



Solution

This piece of code is very useful for performing date calculations. You can obtain the current epoch (time in seconds since Jan 1 1970), then add 3600 for 1 hour – or 86400 for 24 hours hence.



Example


So use in a UNIX variable like this:

epoch=perl -M'English' -e 'print $BASETIME."n";'

To work out 24 hours ago, just subtract 86400.

perl -M'English' -e 'print(($BASETIME-86400)."n");'

Then to see the date yesterday:


$ perl -M'English' -e 'print(($BASETIME-86400)."n");'
1180746252
$ perl -M'English' -e 'print(localtime(1180746252)."n");'
Sat Jun 2 09:04:12 2007



Reference

[tags]Perl, epoch, date manipulation, Perl Coding School[/tags]



Calculate largest field big data file

Problem

I wanted to upload a delimited field to mysql db, but hit the problem that the file contained nearly 3000 rows and no schema on the required size of each field.

Therefore I needed to traverse the file and calculate the length of each field. Then at the end, print the largest field found for each column.



Solution

Perl to the rescue! Pretty easy in Perl, after scratching my head attempting with awk. 🙂

Anyway the code is under the example tab. I’m using the pipe symbol as a delimiter “|” – so just substitute this with your delimiter, cat your file and pipe it through this script.

In the example I show how you can manipulate the file, to produce pipe delimited fields too.



Example


This is how to run the script. Basically you just need to pipe your output through find largest.

cat yourfile | ./find_largest.pl

Here is the code.

#!/usr/bin/perl

@highest=();

while( < STDIN > ) {

@thisline=split(/|/);

for($i=0;$i<=$#thisline;$i++) {

$thislength=length($thisline[$i]);

if($thislength > $highest[$i]) { $highest[$i]=$thislength; }

}

}

print(join("|",@highest)."n");

exit(0);

__END__

So for example, if I want to find the largest fields in one of my web logs - for crunching into a db:

  • First off I only want lines starting with a space, then a number.
  • Next I need to replace all multiple spaces between fields, with 1 space.
  • Then I replace the spaces between each field, with a pipe.
  • Lastly I pump it through find_largest.pl - which gives me the largest sized field


[marcus@bree]/var/log/httpd% grep "^ [0-9]" access_log.tools
| sed -e 's/ / /g' -e 's/ /|/g' |
~/Perl_Bin/find_largest.pl
|14|1|1|21|6|5|103|9|3|5



Reference

[tags]Perl, Data Analysis, Perl Coding School[/tags]



ldapsearch logical NOT

Problem

You want to perform an LDAP search, matching entries which do not match certain criteria.



Solution

To perform a logical NOT we just use the exclamation mark ! – see example.



Example


This is how to perform a logical OR LDAP search.


ldapsearch -x -v-D"cn=Manager,dc=demo,dc=net"-w secret
-b"dc=demo,dc=net" "(!(sn=Doe))"



Reference

[tags]ldapsearch syntax, openldap ldapsearch, LDAP Training School[/tags]



ldapsearch logical AND

Problem

You want to match more than one field, in your LDAP search.



Solution

To match more than one field we use the ampersand – “&” with ldapsearch.



Example


This is how to perform a logical AND LDAP search.


ldapsearch -x -v-D"cn=Manager,dc=demo,dc=net"-w secret
-b"dc=demo,dc=net" "(&(givenname=John)(sn=Smith))"



Reference

[tags]ldapsearch syntax, openldap ldapsearch, LDAP Training School[/tags]



ldapsearch with logical OR

Problem

You want to match more one or another pattern, in your LDAP search.



Solution

To match more one pattern or another we use the pipe symbol “|” .



Example


This is how to perform a logical OR LDAP search.


ldapsearch -x -v-D"cn=Manager,dc=demo,dc=net"-w secret
-b"dc=demo,dc=net" "(|(sn=Doe)(sn=Smith))"



Reference

[tags]ldapsearch syntax, openldap ldapsearch, LDAP Training School[/tags]



Deleting LDAP Record

Problem

You want to delete a LDAP entry.



Solution

In this example, we just use ldapdelete from the command line.

Remember to take a backup. ldapsearch with -L



Example


Here is an example of deleting a record in LDAP:


ldapdelete -v -D'cn=Manager..' -w ${passwd}
-h ${host} -p ${port}<<EOT
cn=….
EOT

Effectively – you just need to supply the full DN. Also be aware you need to delete the lowest branch first, for example:

uid=….
sales=…,uid=….

You need to delete sales first, then uid.



Reference

[tags]ldapdelete, LDAP Training School[/tags]