Linux Blog

The Linux Blog Introduces a new Writer!

Filed under: The Linux Blog News — TheLinuxBlog.com at 5:21 pm on Wednesday, October 31, 2007

Halloween is a fun night, but did you know that 13 Years ago on this night Red Hat was released? Halloween now happens to have a special meaning for me.

We have a new writer for The Linux Blog and what this evening has to do with this is that it is our new writers favorite day of the year!

Aaron currently blogs about technology on http://www.systemlag.com. After reading one of his blog posts (below) I asked him if he would be interested in helping me out by writing for The Linux Blog. He was skeptical at first as he did not know what he would blog about. As an aspiring Linux User I thought it would be good if The Linux Blog has a newbie column and asked if he would be interested in writing for it. He agreed.

You should notice blog posts coming from Aaron within the next couple of days.

Here’s the blog post that caught my attention:
Ok guys, here’s the deal. I have this crazy urge to learn Linux. Yeah, yeah, I know some of you fan boys out there are going to go nuts with the
“Oh my GAWD, he doesn’t know what to do when it comes to LINUX!?”
The short answer to that would be, um, no, I really don’t. I’ve grown up windows, kiddies (unless you count our good pal Commodore) and never really felt the need to take a closer look at Linux before now. So here’s where YOU come in: Suggestions. Right now, I’m thinking of picking up a used box from my local thrift store and, after cleaning all the porn out of it (and I’m hoping that’s all I would find), installing Linux and just giving it a go. What sort of specs should I be looking for as far as hardware goes (I’m not guessing I’ll need much), and which Linux app would be best suited for someone like me who’s just dipping his toe into the shark infested water?

It’s hard to find good bloggers these days. If any one else is interested in writing for us (either as a guest, one time posts or a full time blogger), please shoot me an e-mail owen -at- thelinuxblog.com

Putting it all together

Filed under: Shell Script Sundays — TheLinuxBlog.com at 12:40 pm on Sunday, October 28, 2007

Since the start of this column I’ve shown many techniques that can be used in Shell Scripts. Today I will show an example of what can be done when it is all put together.

To start off, I recently made a photo tagging script for a friend of mine. The purpose of this script is to basically gather information about the pictures such as the month, year and who are in the photo. It also allows people to upload new pictures. The information is stored in a MySQL database and photos are kept in one folder as a unique hash of the file.

The script that I am going to show this week basically hashes the filenames and creates the SQL code that is needed to import the records into the database.

The full script source code can be found here.

The first thing the script does is use getopts to put the options into variables. For more information on how to do this can be found in my blog post on Creating Script Parameters With getopts.

### Get passed options.
while getopts ":s:d:f:" ARG; do
case "${ARG}" in
s) dirSource="${OPTARG}";;
d) dirName="${OPTARG}";;
f) sqlDump="${OPTARG}";;
esac
done

Once the options have been put into variables the following is ran:

The only thing that is needed for this script to run is a path. All that the above does is make sure that a path was specified. If not it calls the function usage and exits the script.
Here is the usage function:

usage () {
echo -e "\nUsage: $0 -s [ -d -f ]\n"
echo -e "\t-s: specifies the source directory. (required)"
echo -e "\t-d: specify the destination."
echo -e "\t-f: create an sql dump to import into MySQL.\n"
}

The main loop is very simular to my When Photoshop Fails article as it uses find. Except it finds all jpg files regardless of case. It does this by using [j,J][p,P][g,G] as the file mask.

find "${dirSource}" -name *.[j,J][p,P][g,G] | while read sourceFile; do

The following checks to make sure that dirName is not empty. The dirname command takes a file and will extract the directory name from it. So, if a directory to put the moved files in wasn’t given at run time, the directory that the new picture gets put in is the same as the old one.

### We don't want to overwrite this var by mistake.
if [ -z "${dirName}" ]; then
dirName=`dirname "${sourceFile}"`
fi

This is the last part of the loop. It uses md5sum to create a hash of the file is piped to awk which is used to print the actual hash. It then moves the file to the directory that was created from above. Create SQL and cleanVar are both functions that are called to do another task.

fileHash=`md5sum -b "${sourceFile}" | awk '{print $1}'`
mv "${sourceFile}" "${dirName}/${fileHash}.jpg"
createSQL; cleanVar

The createSQL function is used in this script to take the hash and insert it into the table that the PHP web app uses. At the moment the script just creates the SQL. The importing has to be done manually.

createSQL () {
if [ -n "${sqlDump}" ]; then
echo "INSERT INTO \`pictures\` (\`img_name\`) VALUES('${fileHash}');" >> "${sqlDump}"
fi
}

The cleaning function unsets fileHash which is the hash of the file that was just copied. It also does a check to see if the $dirName variable needs to be unset. It does this by checking to see if the dirName is the same as the directory name of the source file and unsets $dirName if it is.

### Clean up variables before next loop (precautionary).
cleanVar () {
unset fileHash
 
### We don't want to clean this var unless we have to.
if [ "${dirName}" == "$(dirname ${sourceFile})" ]; then
unset dirName
fi
}

Its a pretty simple script and uses a lot of what I’ve gone over in previous posts. Heres the script in action (the directory that the pictures are to be copied to must exist.)

owen@the-linux-blog:$ ls
JPG-hasmove.sh  NewPics  Pictures
owen@the-linux-blog:$ ls Pictures/
1.jpg  2.jpg  3.jpg  4.jpg  5.jpg
owen@the-linux-blog:$ ls NewPics/
owen@the-linux-blog:$ ./JPG-hasmove.sh -s Pictures/ -d NewPics -f SQLFile.sql
owen@the-linux-blog:$ ls
JPG-hasmove.sh  NewPics  Pictures  SQLFile.sql
owen@the-linux-blog:$ ls Pictures/
1.jpg  2.jpg  3.jpg  4.jpg  5.jpg
owen@the-linux-blog:$ ls NewPics/
294e8b2c5a9c4d61de67a166cd8e8e29.jpg  9b3270fc78fdf3920d0e44197da52944.jpg
6ef86ca6dcfb2467b46ed9d929fd4070.jpg  f353aa857522586d08ad7956090c8566.jpg
8aa35639bc0d3e7abc23c0b70acce08d.jpg
owen@the-linux-blog:$ cat SQLFile.sql
INSERT INTO `pictures` (`img_name`) VALUES('8aa35639bc0d3e7abc23c0b70acce08d');
INSERT INTO `pictures` (`img_name`) VALUES('f353aa857522586d08ad7956090c8566');
INSERT INTO `pictures` (`img_name`) VALUES('294e8b2c5a9c4d61de67a166cd8e8e29');
INSERT INTO `pictures` (`img_name`) VALUES('6ef86ca6dcfb2467b46ed9d929fd4070');
INSERT INTO `pictures` (`img_name`) VALUES('9b3270fc78fdf3920d0e44197da52944');

If you have a use for the script or this has helped you some please let me know, I always like to hear from people that find my posts useful. Until next time: Happy Shell Scripting!

Creating Dialogs with Dialog

Filed under: Shell Script Sundays — TheLinuxBlog.com at 5:48 pm on Sunday, October 21, 2007

Have you ever seen those pretty dialogs used in Shell Scripts such as the Slackware installation, the slackpkg program or even the NVIDIA driver installer? Well, my friends to display dialog boxes from shell scripts is very easy with… you guessed it – Dialog.

First of all, there are many different types of dialogs that you can create they are as follows: calendar, checklist, fselect, gauge, infobox, inputbox, menu, msgbox (message), password, radiolist, tailbox, tailboxbg, textbox, timebox, and yesno (yes/no).

This blog post is intended to be a primer on using dialog. More examples will be posted in future blog posts in the Shell Script Sunday’s column.

The simplest form of a dialog in a shell script is probably the msgbox. All this really does is displays text. To display text in a dialog you would do the following:

owen@the-linux-blog:$ dialog --msgbox "Hello from the Linux Blog!" 5 50

The numbers after the text in quotes are the widths and heights of the box. The minimum height that I like to use is 5. The width doesn’t really matter as long as it is big enough. It is good to keep the box sizes standard across a whole script because it gets annoying with constantly resizing boxes.
If the text in a message box is too long it will auto wrap around and give you a type of scroll bar. As follows:

owen@the-linux-blog:$ dialog --msgbox "Hello from The Linux Blog. This text is so long it wraps it to a New Line" 5 50

Dialogs can be canceled. Clicking Ok or pressing enter/return returns “true” and pressing escape or Ctrl+C returns a false.
The simple shell scripting syntax shown in Shell Scripting 101

is used for this:

owen@the-linux-blog:$ dialog --msgbox "Dialog Exit Example" 5 50 && echo "ok" || echo "false"

Another simple dialog example is the Yes/No box. The syntax for this is exactly the same as the msgbox example except instead of using –msgbox, –yesno is used. The difference between a msgbox and a yesno box is that there a two buttons. It is pretty obvious as to what they are labeled, but for those in the back, I’ve included an example and some screen shots anyway.

owen@the-linux-blog:$ dialog --yesno "Are you learning anything from this blog" 5 50 && echo "Yes, thanks Owen." || echo "No, Write some better Linux Related Posts"

The Linux Blog - Dialog Example - Yes / No

Thats about all I have time for this week. Check back next week!

Creating Script Parameters With getopts

Filed under: Shell Script Sundays — TheLinuxBlog.com at 9:06 am on Sunday, October 14, 2007

Many programs for Linux have parameters that can be given at run time. These are also known as switches, arguments or options. These parameters make it easy to tell a program or script what to do and what options to use. In this Shell Script Sundays Blog post I will show you how to implement these in a script by using getopts.

For this example to work the following must be placed in a script:

while getopts ":e:" ARG;
do case "${ARG}" in
e) echo "${OPTARG}";;
esac;
done

This code basically gets loops around the arguments. All this script does is take the value after the -e and echo’s it out. In the example below I named the script getopts.

owen@the-linux-blog:$ ./getopts -e "Hi There Linux Blog Viewers"
Hi There Linux Blog Viewers

For each extra parameter that is needed a new constant to the getopts and do loop need to be added. For example if the option -e and -q need to be in the script then the following getopts loop would be created:

while getopts ":e:q" ARG;
do case "${ARG}" in
e) echo "${OPTARG}";;
q) echo "${OPTARG}";;
esac;
done

Of course the above script for -q also only echo’s out the value for -q as seen below:

owen@the-linux-blog:$ ./getopts -e "Hi There Linux Blog Viewers" -q "Another Option"
Hi There Linux Blog Viewers
Another Option

This is all very well, but documentation is always nice, even if you think you know how to use your script you may forget in the future. Its also nice if you have a script that other people can easily use. That being said its good to have a way to show users how to run the script.

usage () {
echo -e "Usage: $0 -e \"What To Echo\" [ -q \"Output\" ]"
echo -e "\t-e: specifies what to echo (required)"
echo -e "\t-q: Where to write output to. If not specified the output is written to the console"
}
 
while getopts ":e:q:" ARG;
do case "${ARG}" in
e) ECHO="${OPTARG}";;
q) OUTPUT="${OPTARG}";;
esac;
done
 
[ -z "${ECHO}" ] && { usage && exit 1; }
 
[ "${OUTPUT}" ] && { echo $ECHO > $OUTPUT; } || { echo $ECHO; }

The code above takes the options and assigns a variable to each of the options $ECHO is what to echo and $OUTPUT is where to write the output to. The script calls the usage() function and exits whenever the required option ($ECHO) is not set. If $ECHO is set it checks to see if $OUTPUT is set, if so it echo’s the contents of $ECHO to the $OUTPUT variable (file or device). If $OUTPUT is not set then it just echo’s the $ECHO variable normally. This is the script running with its various different actions:

owen@the-linux-blog:$ ./getopts
Usage: ./getopts -e "What To Echo" [ -q "Output" ]
-e: specifies what to echo (required)
-q: Where to write output to. If not specified the output is written to the console
 
owen@the-linux-blog:$ ./getopts -e "The Linux Blog getopts Example"
The Linux Blog getopts Example
 
owen@the-linux-blog:$ ./getopts -e "The Linux Blog getopts Example. Output To Null" -q /dev/null
 
owen@the-linux-blog:$ ./getopts -e "The Linux Blog getopts Example" -q Write_To_This_File
owen@the-linux-blog:$ ls
Write_To_This_File  getopts
owen@the-linux-blog:$ cat Write_To_This_File
The Linux Blog getopts Example

As there are many different variations and each implementation would be different I can not cover each individual getopts scenario but by assigning variables your option arguments you should be able to get switches working in your own shell scripts.

Secure VNC By Tunneling with SSH.

Filed under: General Linux,Quick Linux Tutorials — TheLinuxBlog.com at 10:09 pm on Saturday, October 13, 2007

Introduction
Remote administration is either a luxury of necessity when it comes to computing. I don’t know how many times I’ve heard people say they would like to be able to remotely access their files or us their computers. Luckily for Linux users this is an easy task.
VNC (Short for Virtual Network Computing) is used to open up a window of a remote desktop. It was created by Olivetti & Oracle Research Lab and acquired by AT&T, now there are many different flavors of VNC servers and clients available. I recommend the use of TightVNC and x11vnc.

Security
VNC is not a secure protocol by default, passwords and data are transmitted in clear text and can be sniffed by any malicious user. To resolve this problem I introduce SSH Tunneling. As we all know SSH is a secure remote shell and with this we can tunnel ports to create a secure connection.

Server Setup
The First step is to have a VNC server running on the box. I like to use x11vnc as it uses the current X session if its available. After x11vnc is installed make sure that X and x11vnc are running.

Client Setup
On the client a VNC Client is needed any client will do but I recommend using TightVNC or RealVNC.

Commence the tunneling
Once the server and client are set up a SSH connection will have to be set up with local to remote port forwarding. The syntax for this command is:

ssh [-R [bind_address:]port:host:hostport] [user@]hostname

To setup a connection from my laptop to The Linux Blog:

owen@linux-blog-lappy:~$ ssh -R 5999:192.168.1.x:5900 thelinuxblog.com

The “-R port:host:hostport” Portion of the command is the part that sets up the port forwarding. In my above example its telling the SSH client to forward localport 5999 to port 5900 on 192.168.1.x (my laptops IP). Once your SSH connection has been made the VNC Client on your local Linux box can be started.
Each client is different but with TightVNC from your run prompt or terminal you can just type:

vncviewer localhost::5999

This will open up the VNC client on your local machine, connect to your local port that you set up in the SSH connection and tunnel all information through the secure connection.

Conclusion
Voilla! There you have it, you can now use SSH and VNC in unity to achieve secure VNC communications. Neat!

Giving Telnet Access To Root

Filed under: General Linux — TheLinuxBlog.com at 1:06 am on Thursday, October 11, 2007

After reading Gary Conn’s blog post The Top Ten Google Keyword Blunders. I naturally started searching thru The Linux Blog’s logs in attempt to find something interesting and irrelevant to my blogs main topic: Linux. Unfortunately I did not have that many terms that were funny like Gary’s but I do have one that could be funny to your average Linux user.

Behold the question:

“how to give root to access telnet in linux”

Something tells me that whoever made this search was probably your average Joe Windows Administrator, So in response I am writing a letter to whom ever made that search.

Dear Searcher,
 
While it is possible to give root telnet access under Linux I would strongly advise against it. To give root access you may want to consider using SSH. SSH is an encrypted session where as Telnet is transmitted in clear text; this is not acceptable and should be avoided. Even tho I recommend using SSH I would not recommend that you allow root to log in via SSH for security purposes. To aqquire root priviledges simply log in as a regular user and switch user to root as follows:
 
<a href="mailto:owen@the-linux-blog:~$">owen@the-linux-blog:~$</a> su
- or -
<a href="mailto:owen@the-linux-blog:~$">owen@the-linux-blog:~$</a> sudo su
 
The same commands would also work with telnet but still your root password would be transmitted in plain text. If you need any help setting this up, or further explanation please don’t hesitate to contact me.
Sincerely,
 
Owen.

Now, I understand that sometimes there are legitimate reasons to use telnet (such as for legacy purposes) but we all know that using telnet for remote administration is a BAD idea. If they or anyone else really need to know how to use telnet for a legitimate reason I will post how to do it, but until then it’s best kept a secret.

Some Linux Blog News

Filed under: The Linux Blog News — TheLinuxBlog.com at 5:25 pm on Wednesday, October 10, 2007

Firstly I would like to welcome a new writer to The Linux Blog his name is Andrew. Those of you who paid attention will have noticed that he wrote last Sundays shell scripting article. This was a huge favor for me since I was at the beach and did not have any internet access. His biography is in the works but in the mean time I would like everyone to give Andrew a nice warm Linux Blog welcome.

And now for another apology.

I’m sorry that I haven’t posted much here recently, I have been very busy. I do however have lots of half written articles and many more articles to write. If there is a particular topic that any one would like me to blog about, send me an email to owen -at- thelinuxblog.com.

– Owen

Perl Regular Expression Cheat Sheet

Filed under: Shell Script Sundays — Owen at 10:15 pm on Saturday, October 6, 2007

Regular Expressions can be tricky, that’s why it is a good idea to keep a quick “cheat sheet” handy when working with them, here’s a concise cheat sheet to get you started:

.  	Match any character
\a  	Match alarm
\d 	Match digit character
\D  	Match non-digit character
\e  	Match escape
\f  	Match form-feed
\n  	Match newline
\r  	Match return
\s  	Match whitespace character
\S  	Match non-whitespace character
\t  	Match tab
\w 	Match "word" character (alphanumeric and "_")
\W  	Match non-word character
\022 Match octal char (i.e. 22 octal)
\xff  	Match hex char (i.e. ff in hex)
 
*	Match 0 or more times
+      	Match 1 or more times
?      	Match 1 or 0 times
 
{n}    	Match exactly n times
{n,}   	Match at least n times
{n,m}	  Match at least n but not more than m times
 
^	Match if at beginning
$ 	Match if at end
 
Examples:
\d{2}-\d{2}-\d{2}		# match date in dd-mm-yy format
^[ \t]+ 			#match leading whitespace
[ \t]+$			#match trailing whitespace
^[ \t]+|[ \t]+$ 		#match leading or trailing whitespace
 
$string =~ m/text/;		#returns true if $string contains text, case sensitive
$string =~ m/text$/i;		#returns true if $string contains text
$string =~ s/text1/text/;	#replace text1 with text2 in $string
$string !~ m/text/;		#returns false if $string contains text, case sensitive
$string !~ m/text/i;		#returns false if $string contains text

I find it useful to print it out and have it handy whenever I wade into the murky waters of regular expressions.

Linux+ Certification

Filed under: General Linux,Shell Script Sundays,The Linux Blog News — TheLinuxBlog.com at 4:35 pm on Monday, October 1, 2007

My Shell Script Sundays article was not posted yesterday as I was reviewing for the Linux+ Certification by CompTIA. I have been meaning to take the exam for a while not but just never got around to doing it.

I took the exam today and passed. It was comparable to the other CompTIA exams I have taken except the topic was mostly on Linux instead of networking or hardware. The questions were very reasonable some were a little tricky and had me confused about the correct way to complete a task.

I only had the LPIC certification book to review from but had read the XK0-001 book about 8 months ago. The exam description explains that it was designed to measure the competencies of the Linux Professional with six to twelve months experience with Linux. Some of the other CompTIA exams say that you should have X years of experience in a certain field but is not needed, I believe that the experience for this exam is needed, maybe not half a years worth but at least a month of using nothing but Linux full time.

I would recommend to anyone interested in collecting certifications to take this exam. Even if Linux isn’t your main concentration it will still be a valuable asset in your certification portfolio. Now I’ve passed I have to find another certification to study for and beat around the bush to take the exam. I’m wondering if I should take another Linux certification (such as Redhat, LPIC or Novell’s) or go another route like Server+ or Security+. Either way it gives me something to do and keeps my brain active.

My apologies for not getting the Shell Script Sundays article out but I have a good one lined up for next week so check back!