Smallest AJAX example in the world?

[problem]

Learn and demonstrate some simple AJAX.

To produce a clear, clean example – as small as possible.

[/problem]

[solution]

Can a smaller working AJAX example be produced?

Always a believer in keeping it simple & find it easier to learn something complex, by making it as small as possible. See Example.

[/solution]

[example]

  1. Click ‘see demo’.
  2. Click image to see updated date.
  3. Click ‘view the code’.

Here is the code:

<?php

if(isset($_GET['subreq'])) {
echo date('l dS of F Y h:i:s A');
exit(0);
}
?>
<img src=/icons/a.png onClick="javascript:
var xmlhttp=false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
try {
xmlhttp = new ActiveXObject('Msxml2.XMLHTTP');
} catch (e) {
try { xmlhttp = new ActiveXObject('Microsoft.XMLHTTP'); } catch (E) { xmlhttp = false; }
}
@end @*/
if(!xmlhttp) { xmlhttp=new XMLHttpRequest(); }
xmlhttp.open('GET','index.php?subreq=y', true);
xmlhttp.onreadystatechange = function() {
if(xmlhttp.readyState==4) { document.getElementById('output_div').innerHTML = xmlhttp.responseText; }
}
xmlhttp.send(null);
">
<div id='output_div'>click image above to put date here</div>

Or Click this link:

[/example]

[reference]

[tags]AJAX tutorial, AJAX, AJAX demonstration, PHP, Learn AJAX[/tags]

For excellent doco (as always) check out Wiki.

[/reference]

Shell Tips and Tricks

[problem]

  1. You want to trace program execution on Solaris?
  2. You want to perform network tracing on Solaris?
  3. Perform a text dump of a binary file – or see ascii codes, etc?
  4. Generate some random data?
  5. List directory contents in 3 columns?
  6. Loop through a list of files and perform actions on them?
  7. Check for values in vars?
  8. Detach a process without it dying.
  9. Perform arithmetic in shell.

[/problem]

[solution]

Some of the most useful Shell Tricks I’ve learned – check them out at Example – enjoy! 🙂

[/solution]

[example]

  1. System process debug (solaris) all exec/reads

    truss -xall -vall -rall -t'read' -p PID

  2. Network trace connection from hostname (solaris)

    snoop -x0 src hostname

  3. Optical dump of a file, useful for seeing control chars, etc and lines with no newline.

    od -c filename

  4. Generate 512 bytes of random data, cat -ve escapes control chars.

    dd bs=1 count=512 if=/dev/urandom | cat -ve

  5. list directory contents, display in 3 columns.

    ls | paste - - -

  6. Loop through all files and perform an action on it.

    for na in * ; do echo $na ; done

  7. Check for values in vars.

    [[ $(echo $var | grep -ic "pattern") -eq 0 ]] &

    You could also go for “-eq 1” – does contain or -ne 0 for same, etc. Plus you could use || for “or” either in place of “&&” or after this block – same as if .. then “positive” else “negative” fi.

  8. Detach a process without it dying.

    disown [%1]

    nohup command > /tmp/logfile.log 2>&1&

    If you start a process with nohop (ignore the hup signal), it will not exist (usually) when your shell is exited. This can now be simulated on solaris, by backgrounding a process (with ctrl Z and bg) then typing disown. jobs command will show all backgrounded jobs. Ideally start commands like this, to avoid the shell exit problem:

  9. Perform arithmetic in shell.

    bc -l

    Binary Calculator in long format – for float, etc. Can also be automated like this, to convert between decimal and hex:

    i=0; while ((i<20)) ; do echo -n "$i: "; echo "base=10;obase=16;$i" | bc; ((i++)); done | paste - - - -
    0: 0 1: 1 2: 2 3: 3
    4: 4 5: 5 6: 6 7: 7
    8: 8 9: 9 10: A 11: B
    12: C 13: D 14: E 15: F
    16: 10 17: 11 18: 12 19: 13

[/example]

[reference]

[tags]Unix Shell Tips, Unix Shell Tricks, UNIX Arithmetic, Optical Dump, Unix Coding School[/tags]

[/reference]

undocument find secret

[problem]

You want to find a pattern in a file and have the line displayed.

[/problem]

[solution]

I “discovered” this undocument (well in all the doco I’ve ever read), pretty much by accident.

Basically find produces a list of files (type f), in the current directory and supplies them individually to the grep command. Ordinarily if grep is supplied a single file, it just returns the pattern. You can suffix grep with -n to also return the line number and -l to return file names without the pattern.

But you cannot (AFAIK) force grep to return the filename and the pattern, from a list of files supplied by find. Of course you could use xargs, to produce a similar mechanism – but that is yet another process to fire up. 🙂

Also you could call a script, instead of grep – which uses something like sed to substitute the beginning of the line with filename – sed “s/^/$1: /” for example.

See the example.

[/solution]

[example]

Anyway, I’m quite happy just using this little secret. 🙂 Having used it many times:

find . -type f -exec grep -i "pattern" {} /dev/null ;

[/example]

[reference]

[tags]find undocumented secret, Unix Coding School[/tags]

[/reference]

Optical Dump

[problem]

Ever need to look at a file, but have not been able to – due to hidden control characters, etc.

[/problem]

[solution]

Or just needed to see spaces and end of line characters, comparing files, etc. Optical dump or more specifically the od command, can be extremely useful for display ascii characters. See the example.

[/solution]

[example]

The basic syntax looks like this:

od -c filename
Heres a demo:
echo "hi
> world
> " | od -c
0000000 h i n w o r l d n n
0000012

Also useful if wanted to look at a binary file, without breaking your terminal. 🙂 Or looking through encryption.

Also an easy converting of ascii to hex:
dd if=/dev/urandom count=1 bs=128 | od -x
1+0 records in
1+0 records out
0000000 5715 4ac5 e457 9c7b 013b 94f8 cdb7 36b7
0000020 1ae0 a650 e236 c4e3 589f 4af2 dddd f67c
0000040 f722 58b3 7e4e 1de4 6de4 36fb 2c8a 6864
0000060 c682 f951 f966 108f b9dd eaca aefd 8a8a
0000100 da45 7e0f a3ae 350f b0b8 a1f8 1dbe aa4c
0000120 aad8 7002 f0ec 2a94 076a 70f9 0d6f cf44
0000140 1cb2 88b6 9db2 258c 6b4e 08f7 bc41 e6e2
0000160 5ec1 101a f849 b50c 322a 43cf 1520 7dba
0000200

[/example]

[reference]

[tags]optical dump, binary format, Unix Coding School[/tags]

[/reference]

Shell Utilities

[problem]

  1. You want to compare 2 streams, with byte offset and line numbers?
  2. Pull off content with a byte offset?
  3. Display lines before or after a pattern?
  4. Send an email – upon job failure?

[/problem]

[solution]

The shell is a powerful beast! Anything is possible … with a little know-how. 🙂 See the examples.

[/solution]

[example]

  1. md5sum TARBALL | awk '{print $1;}' | cmp - TARBALL.md5
    # cmp good for comparing output with file contents, or diffing files. Outputs byte offset and line number. md5sum creates a hash (checksum) of file.
  2. dd bs=1 skip=106 if=infile
    # disk to disk copy, example outputs infile with offset of 106 bytes (block size of 1 byte, skip 106 bytes).
  3. Ever needed to display a line before or after a pattern. Yep sure you can use Perl, but this is a bit simplier to work out.

    See the code here

    See a run through screen shot here

    Usage – ppgrep #1 #2 pattern file – where #1 is the number of lines prior to the pattern (could be 0) and #2 is number of lines post pattern.

  4. Sending email upon UNIX cron failure
  5. [/example]

    [reference]

    [tags]dd, cmp, md5sum, grep, sed, Unix Coding School[/tags]

    [/reference]

openssl many uses

[problem]

  1. You want to generate a self-signed certificate?
  2. Encrypt a message with triple des
  3. View base 64 encoded data
  4. Generate and use random data
  5. Test ciphers
  6. Monitor certificate expiry dates

[/problem]

[solution]

openssl to the rescue! 🙂 See examples.

[/solution]

[example]

  1. openssl code to generate self-signed cert
  2. encrypt with tripledes
  3. openssl base64 [ -d ]
    # encrypted with base64.
  4. Useful fortransfering control/binary content – like this:

    dd bs=1 count=512 if=/dev/urandom | openssl base64

    Also useful for reading mail

    openssl base64 -d /var/spool/mqueue/qfk3GAlBMn018552

  5. Using openssl s_client to test ciphers supported, with automated script – then loop sites and email.
  6. Using openssl s_client to extract expiry dates, with automated script – then loop sites and email.
  7. [/example]

    [reference]

    [tags]openSSL, dd, UNIX, SSL Certificates, PKI, Encryption, Generate Random Data, Unix Coding School[/tags]

    [/reference]

uuencode send attachments

[problem]

You want to send an attachment, from UNIX command line or from within a shell script

[/problem]

[solution]

uuencode works a treat with Solaris. Not sure on RH Linux, etc.

[/solution]

[example]


cat filename | uuencode wat_u_want_attatch_2b_called.ext | mail -s "this is the subject and here is wat u want attach 2b called" email_addr

Simple as that, it will send the filename as an attachment to a blank email, with your subject.

Here is a demo:

cat index.php | uuencode index.php | mail -s "test uuencode" [email protected]

To do multiple attachments, simply cat files – then uuencode them into a temp file. Like this:


cat file1.doc | uuencode attach1.doc > /tmp/send1.doc

Repeat this for say file2 into attach2 and send2. Then to send them all do this:

cat /tmp/send[0-9].doc | mail -s "your subj" youraddr

[/example]

[reference]

[tags]uuencode, Unix Coding School[/tags]

[/reference]

tar many uses

[problem]

You want to make some backups with tar.

[/problem]

[solution]

tar is very functional and has many uses.

[/solution]

[example]

If you are lucky enough to have GNU tar (tar –version will return GNU if so), then you can compress and backup at the same time. 🙂

tar zcvf /tmp/filename.tgz .

This backups and zips the contents of the current directory, to filename.tgz in tmp directory.

If not – no drama. This will backup and pipe through content to gzip, which then zips it.

tar cvf - . | gzip --best > /tmp/filename.tgz

Recreate a directory:

mkdir new

tar -C old -cf - . | tar -C new -xvf

Restore over the network:

ssh remote_hostname "(tar -cf - remote_directory)" | tar -xvf -

For example:

ssh bree "(tar -cf - Perl_Bin)" | tar -xvf -

[/example]

[reference]

[tags]tar, backups, restores, Unix Coding School[/tags]

[/reference]

Port forwarding with SSH

[problem]

You want to forward all traffic from a port on your box, to another host/port combination.

[/problem]

[solution]

The command in the example will port forward, from the local box – any traffic sent to port 8081, will be sent to port 8080 on www.example.com:

[/solution]

[example]

ssh -x -g -L 8081:www.example.com:8080 localhost

Don’t forget, if you need to open LINUX (FC4) firewall, do this (as root):

vi /etc/sysconfig/iptables # add following line then save, exit
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 8081 -j ACCEPT
/etc/init.d/iptables restart

[/example]

[reference]

[tags]Port forwarding, SSH, Unix Coding School[/tags]

[/reference]

Email upon cron failure

[problem]

You want to receive an email upon cron job failure.

[/problem]

[solution]

The example code can be wrapped around a normal cronjob, to send an email if the cron fails.

[/solution]

[example]

Add to beginning:

bash -c '( msg=`

Add to the end:

2>&1 > /tmp/logfail.log`; if [ ! -z "$msg" ] ; then echo "$msg" |

/bin/mail -s "`/bin/uname -n`:$LOGNAME:`/bin/date`: YOUR MESSAGE" YOUR_MAIL_LIST; fi )'

Demo

# # # # # bash -c '( msg=`YOUR COMMAND 2>&1 > /dev/null`; if [ ! -z "$msg" ] ; then echo "$msg" |/bin/mail -s "`/bin/uname -n`:$LOGNAME:`/bin/date`: YOUR MESSAGE" YOUR_MAIL_LIST; fi )'

Example of generating an email upon cron failure

Another example

[/example]

[reference]

[tags]bash, cron email, Unix Coding School[/tags]

[/reference]