Saturday, March 16, 2013

Identify available printer names

Some machines have access to more than one printer. Unless the default printer is the one you want, you need to know the name of the printer to use in the printer-related command. For instance, if you want to know the status of a printer, you execute the lpq command:

$ lpq
ml1640 is ready
no entries

ml1640 is the default printer for the machine. If you want a printer other than the default, the lpq command requires that you specify the printer name. Other commands such as lpr, and lprm behave the same way.

How do you find out the printer name that the Linux printing system will recognize?

$ lpstat -a
clp325w accepting requests since Tue 12 Mar 2013 09:42:07 PM PDT
ml1640 accepting requests since Sun 10 Mar 2013 09:22:33 PM PDT
scx3405 accepting requests since Sun 10 Mar 2013 09:24:00 PM PDT

Armed with the printer name scx3405, you simply run lpq again like this:

$ lpq -P scx3405
scx3405 is ready
no entries

If you run lpstat with the -s parameter, it will give even more information in the form of a status summary (including identifying the default printer):

$ lpstat -s
system default destination: ml1640
device for clp325w: ipp://192.168.1.22
device for ml1640: mfp:/dev/mfp4
device for scx3405: ipp://192.168.1.11

You can tell then ml1640, the default printer, is a local printer, and the rest are remote.

With lpstat, you can list the printer names that the Linux printing system can recognize.

Friday, March 8, 2013

Deciphering mysql error codes

I was tasked to upgrade the mysql server running on the Centos server.

Of course, the first thing I should do was to backup my database. I run the mysqldump command below to write out the sql statements that will re-create and populate the database tables.

# mysqldump -uroot -p -l --opt --all-databases=true > /root/mysqlall.sql
mysqldump: Couldn't execute 'show fields from `asset`': Got error 28 from storage engine (1030)

Lo and behold, I got an "error 28". What is "error 28"? The error message has no useful details that could help me troubleshoot.

The perror command came in handy in that situation.

# perror 28
OS error code  28:  No space left on device

Now, a quick df command confirmed that the hard-drive was full.

perror explains system error codes generated by mysql and the base OS(Linux).

Saturday, March 2, 2013

Auto fill in ssh client parameters

I often ssh to different servers, both at work and at home. Often, the Linux account name is different according to which server I'm logging in. Also, some servers are set up to allow ssh login through a different port than the default port 22. For instance, to login to 123.123.123.123, I need to type all this in:

# ssh -p 2222 admin_2@123.123.123.123

All of this can become human unmanageable rather quickly.

Luckily, I can set up the ssh client such that it fills in ssh login parameters such as port number and user name.

I simply type ssh followed by the IP address or hostname of the computer I want to login. ssh fills in the right port # and user name according to the IP or hostname.

# ssh 123.123.123.123
admin_2@123.123.123.123's password: 

To set it up, insert the following lines into your personal ssh client configuration file, ~/.ssh/config.

Host 123.123.123.123
   User admin_2
   Port 2222

Note that you should edit the configuration file on the ssh client computer. In other words, the setup is on the source computer from which you initiate the ssh request, not the target server that you want to login to.

Alternatively, you can make the change for all users. Insert the same lines into the system-wide ssh client configuration file. For Debian-based distributions, the file is /etc/ssh/ssh_config. You will need root access to edit that file.

After you make the edits, any new ssh client login will have the port and user name filled in automatically.

In addition to ssh, programs such as scp and sftp will also benefit from these settings.

# scp afile.txt  123.123.123.123:
admin_2@123.123.123.123's password: 
afile.txt                                  100%  198     0.2KB/s   00:00    

If you like this article, you may find this article interesting:

How to disable ssh host key checking

Saturday, February 23, 2013

Splitting up is easy for a PDF file

Occasionally, I needed to extract some pages from a multi-page pdf document. Suppose you have a 6-page pdf document named myoldfile.pdf. You want to extract into a new pdf file mynewfile.pdf containing only pages 1 and 2, 4 and 5 from myoldfile.pdf.

I did exactly that using pdktk, a command-line tool.

If pdftk is not already installed, install it like this on a Debian or Ubuntu-based computer.

$ sudo apt-get update
$ sudo apt-get install pdftk

Then, to make a new pdf with just pages 1, 2, 4, and 5 from the old pdf, do this:

$ pdftk myoldfile.pdf cat 1 2 4 5 output mynewfile.pdf

Note that cat and output are special pdftk keywords. cat specifies the operation to perform on the input file. output signals that what follows is the name of the output pdf file.

You can specify page ranges like this:

$ pdftk myoldfile.pdf cat 1-2 4-5 output mynewfile.pdf

pdftk has a few more tricks in its back pocket. For example, you can specify a burst operation to split each page in the input file into a separate output file.

$ pdftk myoldfile.pdf burst 

By default, the output files are named pg_0001.pdf, pg_0002.pdf, etc.

pdftk is also capable of merging multiple pdf files into one pdf.

$ pdftk pg_0001.pdf pg_0002.pdf pg_0004.pdf pg_0005.pdf output mynewfile.pdf 

That would merge the files corresponding to the first, second, fourth and fifth pages into a single output pdf.

If you know of another easy way to split up pages from a pdf file, please tell us in a comment. Much appreciated.

Two updates (part 2, part 3) are available for this post.

Saturday, July 21, 2012

Convert units of measurement

How do you convert from 1 measurement unit to another unit? Say from kilometers to miles.

I can always look it up on that fridge magnet. This one local realtor likes to send me freebies like the fridge magnet with the common conversion factors.

If you don't have a handy fridge magnet, you can try the units command-line utility program.

To install it on a Debian system, enter:

# apt-get install units

# units 60km miles
 * 37.282272
 / 0.0268224

It tells you that 60 kilometers is equivalent to 37.282272 miles.

The second line (beginning with the '/') is somewhat confusing. It is saying, 60 km = 1 / 0.0268224 miles.

Running units in verbose mode makes it clearer.

# units -v 60km miles
 60km = 37.282272 miles
 60km = (1 / 0.0268224) miles

If you like to put spaces in your search parameters, remember to enclose it with quotes.

# units '60 km'  miles
 * 37.282272
 / 0.0268224

If you just want to know the conversion factors, do this:

# units km miles
 * 0.62137119
 / 1.609344

Very quickly, you know:
1 km = 0.62137119 mile, and
1 mile = 1.609344 km.

To many people, installing units and learning all that syntax may be a little too much work.

You can google the answer.

Simply bring up your web browser and go to the Google search page.

Enter this to search:
60 km in miles

The top item returned is the answer you are looking for.

Google Everything.