Ping host measured in microseconds

[problem]

You want to ping a host on a given port and record time taken in microseconds.

The following solution uses PHP socket libraries to open a connection, to the given host and port – then just close it again. By obtaining the microseconds before and after, it is able to deduce time taken in microseconds.

Additionally I’ve added functionality to print the epoch, so the timestamp can be obtained.

[/problem]

[solution]

pingStat.php


<?php
list($usec, $sec)=explode(" ",microtime());
$thisDate=sprintf("%f", ($usec+$sec));
$host="$argv[1]";
$port="$argv[2]";

if(!$host) {
die("Usage: $0 host port [ dtg ]n");
}

$fp = fsockopen("$host", $port, $errno, $errstr, 30);
if (!$fp) {

print("ping failed to host: $host:$port -
$errstr ($errno)n");

} else {

list($usec, $sec)=explode(" ",microtime());
$nextDate=sprintf("%f", ($usec+$sec));

if($argv[3]) {
printf("%d:%0.4fn",time(),$nextDate-$thisDate);
} else {
printf("%0.4fn",$nextDate-$thisDate);
}

fclose($fp);

}

?>

[/solution]

[example]

First example pings a host on port 80 and prints the current epoch.


$ php -q ./pingStat.php apachehost 80 y
1183524570:0.0824

Second example pings it again on port 443 and this time does not print epoch.


$ php -q ./pingStat.php apachehost 443
0.0775

[/example]

[reference]

[tags]Ping host measured in microseconds, PHP Coding School[/tags]

[/reference]

AIX ODM removing device

[problem]

Someone removes a tty, printer, etc by doing rm /dev/lp3.

Then you cannot recreate the device, as system returns “already defined at that location”.

[/problem]

[solution]

You can try to recreate the device with mknod, but need the major/minor numbers.

Then do rmdev -l lp1 -d.

To get major, minor numbers you need to query the ODM.

[/solution]

[example]


odmget -q "value3=lp1" CuDvDr

Should return something like:

CuDvDr:

resoure=”devno”
value1=”15″
value2=”2″
value3=”lp1″

The major number is 15 and minor 2.

Therefore mknod /dev/lp1 c 15 2 creates the device again.

Then rmdev -l /dev/lp1 -d should remove it cleanly.

In extreme cases, where this still does not remove it, you may have to manipulate
the ODM directly. * Proceed with caution *

Backup

cd /etc/objrepos
cp -i CuDv CuDv.$(date +%j)
cp -i CuAt CuAt.$(date +%j)
cp -i CuDvDr CuDvDr.$(date +%j)

Remove ODM entries

odmdelete -q "name=lp1" CuAt
odmdelete -q "name=lp1" CuDv
odmdelete -q "value=lp1" CuDvDr
synclvodm rootvg # resync odm

Reboot if necessary.

[/example]

[reference]

[tags]AIX ODM, odmdelete, synclvodm, Unix Coding School[/tags]

[/reference]

who command 5 ways

[problem]

You want to know who is on the system. Or which userid you are using.
What runlevel you are at – or when the system was booted.

All doable with who.

[/problem]

[solution]

See the example and reference tabs, to see 5 ways to run who, showing different things about the system and users.

[/solution]

[example]

1. Basic


[marcus@bagend puterpet]$ who
marcus pts/1 Sep 19 16:07 (10.0.0.8)
marcus pts/3 Sep 22 17:46 (10.0.0.8)

2. With Heading and Idle time dot means currently active.


[marcus@bagend puterpet]$ who -Hu
NAME LINE TIME IDLE PID COMMENT
marcus pts/1 Sep 19 16:07 ? 29984 (10.0.0.8)
marcus pts/3 Sep 22 17:46 . 29566 (10.0.0.8)

3. Show who I am in the current shell and where I came from.


[marcus@bagend puterpet]$ who am i
marcus pts/3 Sep 22 17:46 (10.0.0.8)

4. Show the current run level Shows boot time and last runlevel too.


[marcus@bagend puterpet]$ who -r
run-level 5 Sep 9 10:47 last=S

5. Show last boot time


[marcus@bagend puterpet]$ who -b
system boot Sep 9 10:47

[/example]

[reference]

[tags]who command, Unix Coding School[/tags]

[/reference]

List files 12 ways

[problem]

You want to list files under UNIX! 🙂 Here I show 12 different ways to do it.

[/problem]

[solution]

See the example and reference tabs.

[/solution]

[example]

1. Basic


[marcus@bagend Perl_Bin]$ ls
d-dump-tst.pl demo-ldap-entry2.pl demo-ldap-entry5.pl ldap-to-sql.pm
logcam.pm showstat.pl
demo-ldap-entry1.pl demo-ldap-entry3.pl demo-ldap-entry6.pl listenport.pl perl_eval_pattern.pl showtime.pl
demo-ldap-entry1.pm demo-ldap-entry4.pl ldap-to-sql.pl logcam.pl
scanport.pl

2. Long


[marcus@bagend Perl_Bin]$ ls -l
total 68
-rwxr--r-- 1 marcus adm 335 Sep 12 2005 d-dump-tst.pl
...
-rwx------ 1 marcus adm 151 Feb 5 2006 showtime.pl

2. Hidden files


[marcus@bagend Perl_Bin]$ ls -la
total 76
drwxr-xr-x 2 marcus adm 4096 Aug 15 04:53 .
drwx------ 29 marcus adm 4096 Sep 23 11:47 ..
-rwxr--r-- 1 marcus adm 335 Sep 12 2005 d-dump-tst.pl
...
-rwx------ 1 marcus adm 151 Feb 5 2006 showtime.pl

3. Sort by last modified


[marcus@bagend Perl_Bin]$ ls -lt
total 76
-rwxr--r-- 1 marcus adm 286 Aug 15 04:53 perl_eval_pattern.pl
-rw-r--r-- 1 marcus adm 210 Apr 30 08:07 logcam.pm
-rwxr--r-- 1 marcus adm 3483 Feb 5 2006 logcam.pl
...
-rwxr--r-- 1 marcus adm 335 Sep 12 2005 d-dump-tst.pl

4. Sort by last modified – reverse


[marcus@bagend Perl_Bin]$ ls -ltr
total 76
-rwxr--r-- 1 marcus adm 335 Sep 12 2005 d-dump-tst.pl
...
-rwxr--r-- 1 marcus adm 3483 Feb 5 2006 logcam.pl
-rw-r--r-- 1 marcus adm 210 Apr 30 08:07 logcam.pm
-rwxr--r-- 1 marcus adm 286 Aug 15 04:53 perl_eval_pattern.pl

5. Show hidden characters in file name


[marcus@bagend Perl_Bin]$ ls -ld *test*
-rw-r--r-- 1 marcus adm 5 Sep 23 11:59 ???testfile1
[marcus@bagend Perl_Bin]$ ls -lb *test*
-rw-r--r-- 1 marcus adm 5 Sep 23 11:59 bbbtestfile1

6. Show a digest of directories With asterix for executable and slash for directory, etc.


[marcus@bagend Perl_Bin]$ ls -aF
./ demo-ldap-entry1.pl* demo-ldap-entry3.pl* demo-ldap-entry6.pl*
listenport.pl* perl_eval_pattern.pl* showtime.pl*
../ demo-ldap-entry1.pm demo-ldap-entry4.pl* ldap-to-sql.pl*
logcam.pl* scanport.pl*
d-dump-tst.pl* demo-ldap-entry2.pl* demo-ldap-entry5.pl* ldap-to-sql.pm
logcam.pm showstat.pl*

7. Do a recursive listing

[marcus@bagend Perl_Bin]$ ls -lR
.:
total 76
-rwxr--r-- 1 marcus adm 335 Sep 12 2005 d-dump-tst.pl
...
-rw-r--r-- 1 marcus adm 5 Sep 23 11:59 ???testfile1

./test1:
total 4
drwxr-xr-x 2 marcus adm 4096 Sep 23 12:01 test2

./test1/test2:
total 0
-rw-r--r-- 1 marcus adm 0 Sep 23 12:01 showit

8. List the directory not its contents.


[marcus@bagend marcus]$ ls -ld Perl_Bin
drwxr-xr-x 3 marcus adm 4096 Sep 23 12:01 Perl_Bin

9. Digest the listing


[marcus@bagend Perl_Bin]$ ls -m
d-dump-tst.pl, demo-ldap-entry1.pl, demo-ldap-entry1.pm, demo-ldap-entry2.pl, demo-ldap-entry3.pl, demo-ldap-entry4.pl, demo-ldap-entry5.pl,
demo-ldap-entry6.pl, ldap-to-sql.pl, ldap-to-sql.pm, listenport.pl, logcam.pl, logcam.pm, perl_eval_pattern.pl, scanport.pl, showstat.pl,
showtime.pl, test1, ???testfile1

10. Include the inode – useful for then doing find . -inum #### – to remove, etc


[marcus@bagend Perl_Bin]$ ls -li
total 76
647846 -rwxr--r-- 1 marcus adm 335 Sep 12 2005 d-dump-tst.pl
...
648063 -rw-r--r-- 1 marcus adm 5 Sep 23 11:59 ???testfile1

11. Force ls to produce one column


[marcus@bagend Perl_Bin]$ ls -1
d-dump-tst.pl
...

12. Show file modified time in seconds Okay I cheated here – it is Perl! 🙂 But hey it’s my blog!! 😉


[marcus@bagend Perl_Bin]$ perl -e
'print(localtime((stat($ARGV[0]))[9])."n");' mytestfile
Sat Sep 23 12:50:03 2006

[/example]

[reference]

[tags]Listing files, Unix Coding School[/tags]

[/reference]

Permissions extension setfacl

[problem]

You want to allow access to files or directories, without giving exclusive use of a given group, user, etc.

[/problem]

[solution]

Use setfacl. 🙂 See the example and reference tabs.

[/solution]

[example]


setfacl -m user:USERID:rw- filename

getfacl filename

Can also do this: getfacl filename1 | setfacl -f – filename

Performing an ls -ld filename, will show a plus in the permission column.

[/example]

[reference]

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

[/reference]

Linux dircmp – directory compare

[problem]

I wanted to run a dircmp with the -d option, to show all files that are different.

Plus display those differences.

Linux replied – command not found! 🙂

[/problem]

[solution]

Just diff the directories, with the -d flag. See the example and reference tabs for more info.

[/solution]

[example]


diff -d dir1 dir2

[/example]

[reference]

[tags]UNIX, Linux, Dircmp, Unix Coding School[/tags]

[/reference]

Redundant key strokes

[problem]

Carrying on with my lazy steps, there are bits of code used with UNIX commands, which are completely redundant.

[/problem]

[solution]

Save yourself some key strokes, remember these examples. Or come back to unix.coding-school.com and check them out again. 🙂

[/solution]

[example]


chmod a+r filename

The “a” is redundant – if you just do a “+r” – all is implied.


ls -l | awk ' $4 > 1000 { print $0 } '

In awk printing $0 is default action.


find . -type f -print

With find default action is to print.


echo a b c | tr '[abcdef]' '[ABCDEF]'

Use a range: echo a b c | tr ‘[a-z]’ ‘[A-Z]’

In vi use ZZ instead of :wq 🙂

Only 2 chars less (including the shift) – but also just your little finger, instead of a hand span. 🙂

[/example]

[reference]

[tags]Redundant key strokes, UNIX shortcuts, Unix Coding School[/tags]

[/reference]

Restarting Linux services boot time

[problem]

Every time you reboot your Linux Box – web server, samba server, etc is down.

[/problem]

[solution]

Use chkconfig. See the example tab and then reference tab for man page.

[/solution]

[example]

Show all services


chkconfig --list

List just httpd


chkconfig --list httpd

Change restart settings so web server starts at run level 5.
(do a runlevel to see normal level – most likely 5).


chkconfig --levels 5 httpd on

[/example]

[reference]

[tags]chkconfig, Linux reboot, Linux services, Linux restarts, Unix Coding School[/tags]

[/reference]

Lazy tip – wrong password

[problem]

You start typing your password in UNIX, then realize – hold on that ain’t right.

Sometimes your control c it – other times just hit enter and do it again.

There is a quicker way. 🙂

[/problem]

[solution]

When your sat at that password: prompt and lord only knows what you’ve typed. No problem – just do one control u and all is right with the world again. 🙂

I only know from the billion odd times I’ve done it too! 😉 That equates to many wasted hours, this lazy tip will save you – over time.

[/solution]

[example]

Nothing to see here, check out the solution tab.

[/example]

[reference]

[tags]UNIX Lazy tip, UNIX passwords, Unix Coding School[/tags]

[/reference]

YUM fails with 404

[problem]

If you (like me) was well keen, to get your shiny new fedora core 5 system up and running. Then you run yum and think, hang on that ain’t right – 404. Linux is better than this!! 🙁

[/problem]

[solution]

Don’t bother searching the news groups, for posts that go nowhere (like I did for close on 2 hours – that I’ll never get back!! 🙂 ), here is the answer –

www.fedorafaq.org Do a search for yum – follow the prompts and you’re in.

[/solution]

[example]

Nothing to see here, check out the solution tab.

[/example]

[reference]

[tags]Linux YUM, YUM fails, YUM problems, Unix Coding School[/tags]

[/reference]