|
|
Note: The archives category content is an automatically generated focus channel and does not neccessarily reflect the opinions of this blog. No responsibility is taken for the external links presented here, follow at your own discretion. The archives content is never scraped from sites - but an abstract obtained from search engines.
Category: 'UNIX Coding Specialist'
Tuesday, January 1st, 2008
| Problem |
Solution |
Example |
Reference |
Recommended |
truss -xall -vall -rall -t'read' -p PID # To run a command and have it produce debug info, run it prefix with the truss syntax: truss -xall -vall -rall -t'read' your command # Another good option when debugging, is to run truss and look at the opens. Or more specifically the failures - as this often highlights missing libraries or permissions issues. truss -t'open' -p PID # - On Linux use strace, which takes basically the same options. You just use -e, like this:
strace -e'open' ls open("/etc/ld.so.preload", O_RDONLY) = -1 ENOENT (No such file or directory) open("/etc/ld.so.cache", O_RDONLY) = 3 open("/lib/tls/librt.so.1", O_RDONLY) = 3 open("/lib/libtermcap.so.2", O_RDONLY) = 3 open("/lib/libacl.so.1", O_RDONLY) = 3 open("/lib/tls/libc.so.6", O_RDONLY) = 3 open("/lib/tls/libpthread.so.0", O_RDONLY) = 3 open("/lib/libattr.so.1", O_RDONLY) = 3 open("/usr/lib/locale/locale-archive", O_RDONLY|O_LARGEFILE) = 3 open(".", O_RDONLY|O_NONBLOCK|O_LARGEFILE|O_DIRECTORY) = 3 open("/etc/mtab", O_RDONLY) = 3 open("/proc/meminfo", O_RDONLY) = 3
|
No Comments »
Tuesday, January 1st, 2008
| Problem |
Solution |
Example |
Reference |
Recommended |
- You want to trace program execution on Solaris?
- You want to perform network tracing on Solaris?
- Perform a text dump of a binary file - or see ascii codes, etc?
- Generate some random data?
- List directory contents in 3 columns?
- Loop through a list of files and perform actions on them?
- Check for values in vars?
- Detach a process without it dying.
- Perform arithmetic in shell.
|
|
| Problem |
Solution |
Example |
Reference |
Recommended |
- System process debug (solaris) all exec/reads
truss -xall -vall -rall -t'read' -p PID - Network trace connection from hostname (solaris)
snoop -x0 src hostname - Optical dump of a file, useful for seeing control chars, etc and lines with no newline.
od -c filename - Generate 512 bytes of random data, cat -ve escapes control chars.
dd bs=1 count=512 if=/dev/urandom | cat -ve - list directory contents, display in 3 columns.
ls | paste - - - - Loop through all files and perform an action on it.
for na in * ; do echo $na ; done - 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. - 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: - 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
|
No Comments »
Monday, December 31st, 2007
| Problem |
Solution |
Example |
Reference |
Recommended |
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.
|
|
| Problem |
Solution |
Example |
Reference |
Recommended |
Anyway, I'm quite happy just using this little secret. Having used it many times: find . -type f -exec grep -i "pattern" {} /dev/null \;
|
No Comments »
Saturday, December 29th, 2007
| Problem |
Solution |
Example |
Reference |
Recommended |
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.
|
|
| Problem |
Solution |
Example |
Reference |
Recommended |
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
|
No Comments »
Saturday, December 29th, 2007
| Problem |
Solution |
Example |
Reference |
Recommended |
- You want to generate a self-signed certificate?
- Encrypt a message with triple des
- View base 64 encoded data
- Generate and use random data
- Test ciphers
- Monitor certificate expiry dates
|
|
No Comments »
Saturday, December 29th, 2007
| Problem |
Solution |
Example |
Reference |
Recommended |
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" junk@techie-blogs.com 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
|
No Comments »
Sunday, December 16th, 2007
| Problem |
Solution |
Example |
Reference |
Recommended |
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
|
No Comments »
Sunday, December 16th, 2007
| Problem |
Solution |
Example |
Reference |
Recommended |
Always take a backup first, then vi /etc/sysconfig/iptables. In the example I am allowing access to all ports between 600 and 699 - for udp and tcp traffic.
|
|
| Problem |
Solution |
Example |
Reference |
Recommended |
# grep 600 /etc/sysconfig/iptables -A RH-Firewall-1-INPUT -p tcp -m state –state NEW -m tcp –dport 600:699 -j ACCEPT -A RH-Firewall-1-INPUT -p udp -m state –state NEW -m udp –dport 600:699 -j ACCEPT Then run /etc/init.d/iptables restart You can test if this connection is now viable, by using my Perl port testing code under Perl Coding School Other gotchas with samba and nfs, was fiddling with selinux and ensuring portmap was running, etc. Happy to explain further, feel free to post a comment with your questions.
|
No Comments »
Sunday, December 16th, 2007
| Problem |
Solution |
Example |
Reference |
Recommended |
Openssl tripleDES encrypt command: openssl des3 -salt -in file_to_encrypt -pass pass:_your_password_ 
Place contents to encrypt in the file reference by file_to_encrypt Replace your_password with your secret Openssl will output to stdout - so best to capture like this: myvar=$(the openssl command) Openssl tripleDES decrypt command: openssl des3 -d -salt -in file_to_encrypt -pass pass:_your_password_ Put cipher text to decrypt in filename supplied to -in Replace _your_password_ with the secret Outputs to stdout See the full demo attached here for a blow by blow: [ How to encrypt with openssl and tripleDES ]
|
No Comments »
Sunday, December 16th, 2007
| Problem |
Solution |
Example |
Reference |
Recommended |
You want to generate a self-signed certificate, for use with a web server. This will allow you to communicate with your web server over HTTPS, effectively encrypting your traffic.
|
|
| Problem |
Solution |
Example |
Reference |
Recommended |
To create a certificate request: openssl req -new -days 730 -keyout keyna.pem -out keyna.pem [ See a run through screen shot here ] Self-sign the request - only do this if you are not sending off to a CA such as Verisign. openssl ca -policy policy_anything -days 730 -out certna.pem -infiles keyna.pem [ See a run through screen shot here ] If you get 'unable to write random state', try this: look for command ssh-rand-helper usually under ssh install directories (or if ~/.ssh/.prng_seed exists - you can just link to it or copy it to $HOME). ssh-rand-helper -b 1024 > $HOME/.prng_seed To strip the password out, for restarts: openssl rsa -in keyna.key -out keyna.key.unsecure
|
1 Comment »
|