## Thursday, May 31, 2007

### Convert Decimal to Hexadecimal, Octal or Binary using typeset

Here is a quick and dirty way of converting a decimal number to hexadecimal, octal, or binary. Below is an inline script followed by a normal shell script file. There are some non-looping examples toward the end of the post.

# ksh
# for i in 100 102 103 104 105 110 1000
> do
>
> typeset -i16 hex
> hex=\$i
> print \$i equals \$hex in hexadecimal
>
> typeset -i8 oct
> oct=\$i
> print \$i equals \$oct in octal
>
> typeset -i2 bin
> bin=\$i
> print \$i equals \$bin in binary
>
>print
> done

100 equals 8#144 in octal
100 equals 2#1100100 in binary

102 equals 8#146 in octal
102 equals 2#1100110 in binary

103 equals 8#147 in octal
103 equals 2#1100111 in binary

104 equals 8#150 in octal
104 equals 2#1101000 in binary

105 equals 8#151 in octal
105 equals 2#1101001 in binary

110 equals 8#156 in octal
110 equals 2#1101110 in binary

1000 equals 8#1750 in octal
1000 equals 2#1111101000 in binary

Or put it in a script and specify an argument list via CLI

# vi my_converter.ksh

#!/bin/ksh

if [ \$# = 0 ]; then
exit
fi

for i in \$*
do

typeset -i16 hex
hex=\$i
print \$i equals \$hex in hexadecimal

typeset -i8 oct
oct=\$i
print \$i equals \$oct in octal

typeset -i2 bin
bin=\$i
print \$i equals \$bin in binary
print
done

:wq!

# chmod 755 my_converter.ksh

# my_converter.ksh 23 32 33 (Michael Jordan, Magic Johnson, Larry Bird)

No looping examples

# sh
# echo 'obase=16;100'| bc
64

Convert Decimal to Decimal
# echo 'obase=10;10'| bc
10

Convert Decimal to Octal
# echo 'obase=8;34'| bc
42

Convert Decimal to Binary
# echo 'obase=2;10'| bc
1010

# ksh
# echo \$((2#101010))
42
# echo \$((8#52))
42
# echo \$((16#2A))
42

## Wednesday, May 30, 2007

### Calculate with the Basic Calculator -- bc

You can do simple command line calculations with the basic calculator, bc, such as add, subtract, multiply, division, modulus, sqrt, raise a number to a power, logarithms, exponential, compute sines, cosines, loops, functions, and more. Here are some really basic examples -- scale was used to represent precision. I have used the bc command while in the single user mode.

#bc
12-12
0
12+12
24
12*12
144
12/12
1
scale=3
12/12
1.000
12%12
0
2^3
8
sqrt(12)
3
scale=3
sqrt(12)
3.464
22/7
3
scale=6
22/7
3.142857
scale=10
22/7
3.1428571428

## Sunday, May 27, 2007

### Exclude Files or Directories from an Archive using tar

Sometimes you might want to routinely backup certain files or directories that are on a mass storage device (RAID), but you don't want to backup the entire RAID. You can do this by using the tar command's exclude option. You probably want to exclude the exclude and tarball files. Here's an example of its use.

First I create the exclude file and give it an arbitrary name, filesDir_to_exclude

#cat > filesDir_to_exclude
filesDir_to_exclude
esofthub.tar
temp1
temp2
temp2
junk
large_directory
larger_directory
massive_archive_directory
...
Control d

#cd /raid
#tar cvfpX esofthub.tar filesDir_to_exclude *

### Encrypt and Decrypt the Contents of a File

Here's a simple way to encrypt the contents of a message using the crypt command. The security level is fairly weak but it's not meant to be PGP. Here an example to encrypt and decrypt the contents of a file.

To encrypt
Encrypt with mykey in the open
# crypt mykey<not_encrypted_file>encrypted_file

Prompted for mykey
# crypt<not_encrypted_file>encrypted_file
Enter key:mykey

To decrypt
Decrypt with mykey in the open
# crypt mykey<encrypted_file | more

Prompted for mykey
# crypt<encrypted_file | more
Enter key:mykey

Mail the encrypted file as an attachment
Use a mail client such as Netscape, FireFox, or dtmail because it's easier to move the binary file.
#dtmail

## Saturday, May 26, 2007

### Remove the UNIX root Password

From time to time, passwords are lost or forgotten - even root's password. One way of removing the root password is to boot to single user mode from an installation cdrom. Mount the root, /, partition and blank out the encrypted password field in the shadow file. Here's an illustration.

ok boot cdrom -s

# mount /dev/dsk/c0t0d0s0 /mnt

root:9Zpah2q.zd.JJu:13832::::::
daemon:NP:6445::::::
bin:NP:6445::::::
sys:NP:6445::::::
lp:NP:6445::::::
uucp:NP:6445::::::
...

root:BLANK THIS FIELD OUT:13832::::::
daemon:NP:6445::::::
bin:NP:6445::::::
sys:NP:6445::::::
lp:NP:6445::::::
uucp:NP:6445::::::
...
:wq!

# cd /
# umount /mnt
# reboot -- -s

Change the root password in single user mode (don't need to add "root" but I do it for clarification.
# passwd root
passwd (SYSTEM): passwd successfully changed for root

## Thursday, May 24, 2007

### Display the Address Space of a Process

The pmap command is used to display the address space of process ID. It shows the specific allocation of private and shared memory in kilobytes and the permissions for each mapped file. We've used it to troubleshoot database issues with memory. Here's an example.

pmap -x 2563
2563: in.telnetd
Address Kbytes Resident Shared Private Permissions Mapped File
...
...
-------- ------ ------ ------ ------
total Kb 1792 1528 1120 408

## Monday, May 21, 2007

### Sun Fire V1280 is Crashing

For this post, I'm deviating from my normal simple UNIX HOWTO's format.

A colleague of mine is having a problem with a Sun Fire v1280 server crashing. He tried Googling for the error message in red but hasn't found anything yet. Your insights would be greatly appreciated.

"cannot schedule clearing of error on page 0x00000000.f6eb4000; page not
in VM system type errors."

-------------

We *are* seeing memory errors too, but a note said this is a CPU problem.

We have some apparent bad memory and we have the replacements but the current memory errors are on a different J stick.
-------------

### Remove Zero Length and Core Files With Inline Script

Here's a C shell inline script to remove zero length and core files. This particular script removes these files from the esofthub directory. Here's an example.

#csh
# foreach zero_core(/home/esofthub/*)
? if (-z \$zero_core || `basename \$zero_core` == "core") then
? rm \$zero_core
? endif
? end

Just find zero length files (per request)

#csh
# foreach zero_core(/home/esofthub/*)
? if (-z \$zero_core) then
? rm \$zero_core
? endif
? end

## Sunday, May 20, 2007

### Drop a Sybase Database and its Device

Occasionally, you may need to drop a Sybase database and its device. For example, the database may need to be expanded but there is enough space on the device. But first, you will need to drop the database before dropping its device. Then confirm whether the database and the device were dropped or not. Here is an example.

1> use master
2> go
1> sp_helpdb
2> go
1> drop database MYDB
2> go
1> sp_dropdevice MYDB_device
2> go
1> sp_helpdb
2> go
1> sp_helpdevice
2> go

## Friday, May 18, 2007

### Extract the Contents of a Compressed Tar File

A colleague of mine asked about extracting the contents of a compressed tarball into a particular directory. I suggested downloading the tarball into the /tmp directory. It was a fairly large tarball consisting of several hundred megabytes. Here's an example of what was done.

#cd /tmp
#zcat compressed_tarball.Z | (cd /export/opt; tar xvfp -)

## Thursday, May 17, 2007

### Identify Process Hierarchy for an Application

Occasionally, you might want to print out the process hierarchy for an application. This can be done using the ptree command. This command can print out the parent and child processes. Here’s an example of its use using Netscape’s PID.

# ptree -a 532
1 /etc/init -
311 /bin/ksh /usr/dt/bin/Xsession
356 /usr/dt/bin/sdt_shell -c unset DT; DISPLAY=:0;
359 -sh -c unset DT; DISPLAY=:0; /usr/dt/bin/dtse
372 /usr/dt/bin/dtsession
379 dtwm
404 /usr/dt/bin/dtexec -open 0 -ttprocid 2.16H6ao 01 371 1289
405 /usr/dt/bin/dtterm
407 /sbin/sh
531 ./netscape
532 ./.netscape.bin -xrm *appDir: /usr/dt/appconfig/n
534 (dns helper)

## Tuesday, May 15, 2007

### Get Information on a Domain Name

I was curious as to how many yahoo.com variations were out there. I used the whois utility, which is an Internet user name directory service, to ascertain that information.

# whois yahoo.com

Whois Server Version 1.X

Domain names in the .com and .net domains can now be registered
with many different competing registrars. Go to http://www.internic.net
for detailed information.

YAHOO.COM.ZZZZZZ.MORE.INFO.AT.WWW.BEYONDWHOIS.COM
YAHOO.COM.ZZZZZ.GET.LAID.AT.WWW.SWINGINGCOMMUNITY.COM
YAHOO.COM.ZOMBIED.AND.HACKED.BY.WWW.WEB-HACK.COM
YAHOO.COM.VN
YAHOO.COM.VIRGINCHASSIS.COM
YAHOO.COM.UNIQUELYUJEWELS.COM
YAHOO.COM.TWIXTEARS.COM
YAHOO.COM.TW
YAHOO.COM.SG
YAHOO.COM.OPTIONSCORNER.COM
YAHOO.COM.MX
YAHOO.COM.MORE.INFO.AT.WWW.BEYONDWHOIS.COM
YAHOO.COM.JOSEJO.COM
YAHOO.COM.JENNINGSASSOCIATES.NET
YAHOO.COM.IS.N0T.AS.1337.AS.SEARCH.GULLI.COM
YAHOO.COM.HK
YAHOO.COM.ELPOV.COM
YAHOO.COM.DALLARIVA.COM
YAHOO.COM.BR
YAHOO.COM.BGPETERSON.COM
YAHOO.COM.AU
YAHOO.COM

The registry database contains only .COM, .NET, .EDU domains and
registrars.

## Thursday, May 10, 2007

### Nested Inline For Loop for Recursive Directory Creation

The inline script will create 5 recursive directories and recursively change the directory permission bits to read, write and execute for the owner and for all others read and execute.

Here's the syntax.

#sh
#cd to_creation_dir
# for i in dir_1 dir_2 dir_3 dir_4 dir_5
> do
> for j in 2005 2006 2007
> do
> for k in jan feb mar apr may jun jul aug sep oct nov dec
> do
> for l in mon tue wed thu fri sat sun
> do
> for m in log1 log2 log3 log4 log5
> do
> mkdir -p \$i/\$j/\$k/\$l/\$m; chmod -R 755 \$i/\$j/\$k/\$l/\$m
> done
> done
> done
> done
> done

### Add String to Filenames Using an Inline For Loop

A friend of mine had about 500 files in a directory. Based on specific extensions, he wanted roughly 300 of them to have a prepended string attached. An inline script was used to accomplish the task. Here is a simple example.

Here is the busy work part
# ls *.specific_extension_1 > mylist
# ls *.specific_extension_2 >> mylist
# ls *.specific_extension_3 >> mylist
or
# ls *.specific_extension_1 *.specific_extension_2 *.specific_extension_3 > mylist

Now for the inline for loop
# sh
# for filename in `cat mylist`
do
mv \$filename PREPEND_STRING\$filename
echo \$filename changed | tee -a /tmp/logfile
done

## Wednesday, May 09, 2007

### Dump Sybase Database to Tape

Obviously, it is always a good idea to backup your database to a spare system disk, RAID, or to another slice on the system disk. But I think it is equally important to backup the database to something offline, i.e. tape.

If the tape hasn't been initialized before, you will need to add the keyword init into your syntax. You will only have to do that once. Again, it's a fairly straightforward process. Here is an example.

1> use master
2> go
1> dump database MYDB to mytapealias with init
2> go

### Load Sybase Database From Tape

On occasion, you may have to restore a database from a tape. This is a fairly simple and straightforward task to perform. Here is the syntax to load a Sybase database from tape.

First, get a list of the database filename(s) on the tape

1> load database MYDB from mytapealias with listonly
2> go

Next, restore the database from tape and then put it online

1> use master
2> go
1> load database MYDB from mytapealias with file = 'MYDBXXXXXXX'
2> go
1> online database MYDB
2> go

## Tuesday, May 08, 2007

### List Logged In Users on System

Here are four handy commands to ascertain who is logged onto a local system. The first one is the who command, which provides a list of users. The second one is the who am i command. It shows the current user. The third one is the w command. It shows information about currently logged-in users. The last command is the finger command, and it displays user information.

The who command
#who

The who am i command
#who am i
esofthub

The w command
#w
From a commenter's post.
root@mars# w
1:44pm up 4:29, 1 user, load average: 0.05, 0.05, 0.05
User tty login@ idle JCPU PCPU what
root pts/1 9:24am 1:56 w

The finger command
#finger esofthub

Other notable commands to ascertain logged in users on the network (rwho and rusers can bog large networks down due to broadcasts)

The rwho command
#rwho

The rusers command
#rusers

## Sunday, May 06, 2007

### Lookup an Internet Protocol (IP) Address for a URL

Sometimes it might be necessary to lookup an IP address for a URL. The nslookup command can provide you that capability. Here's an example.

#nslookup some_urlname.com

Here's the output of nslookup
Server: xxx.xxx.net (DNS server)

Name: xxxx.com

Here are some more examples using nslookup
# nslookup

Default Server: XXX.XXXX.net

Nameservers

> set type=ns
> some_urlname.com
Server: XX.XXXX.net

some_urlname.com nameserver = ns5.some_urlname.com
some_urlname.com nameserver = ns8.some_urlname.com
some_urlname.com nameserver = ns9.some_urlname.com
some_urlname.com nameserver = ns2.some_urlname.com
some_urlname.com nameserver = ns1.some_urlname.com
some_urlname.com nameserver = ns3.some_urlname.com
some_urlname.com nameserver = ns4.some_urlname.com

Authoritative answers can be found from:

Mail servers

> set type=mx
> some_urlname.com
Server: XXX.XXXX.net

some_urlname.com preference = 1, mail exchanger = e.mx.mail.some_urlname.com
some_urlname.com preference = 1, mail exchanger = f.mx.mail.some_urlname.com
some_urlname.com preference = 1, mail exchanger = g.mx.mail.some_urlname.com
some_urlname.com preference = 1, mail exchanger = a.mx.mail.some_urlname.com
some_urlname.com preference = 1, mail exchanger = b.mx.mail.some_urlname.com
some_urlname.com preference = 1, mail exchanger = c.mx.mail.some_urlname.com
some_urlname.com preference = 1, mail exchanger = d.mx.mail.some_urlname.com
Authoritative answers can be found from:
some_urlname.com nameserver = ns2.some_urlname.com
some_urlname.com nameserver = ns1.some_urlname.com
some_urlname.com nameserver = ns3.some_urlname.com
some_urlname.com nameserver = ns4.some_urlname.com
some_urlname.com nameserver = ns5.some_urlname.com
some_urlname.com nameserver = ns8.some_urlname.com
some_urlname.com nameserver = ns9.some_urlname.com