Linux Blog

Forwarding Ports over an active SSH connection

Filed under: General Linux,Linux Software,Quick Linux Tutorials — TheLinuxBlog.com at 12:01 am on Tuesday, October 7, 2008

There is no doubt about it that SSH is a handy administration and remote access tool. Have you ever wanted to add port forwarding or cancel port forwarding from within an active SSH connection? Well, a feature that many do not know about is the ssh escape feature. With this pseudo terminal you can do some nifty things, such as forward ports over an active SSH connection.

To do this, follow these instructions:
(Read on …)

Using Subversion with SSH & Custom Ports

Filed under: Linux Software,Quick Linux Tutorials — TheLinuxBlog.com at 9:09 am on Monday, September 15, 2008

Lets say you use subversion on your home PC to keep track of projects and you want to checkout or export your project from a remote location. Here’s the catch, sshd is running on a custom port or forwarded from another. For some reason the command line SVN client does not support a port parameter when using the defacto svn+ssh://

svn co svn+ssh://thelinuxblog.com/owen/svn/project1/trunk project1
ssh: connect to host thelinuxblog.com port 22: Connection refused

Well we know why the error above happens its because I happen to run SSH on port 1337. The following work around requires root privileges, and may mess with your system a bit but if you really need to check something out, then it will work.

As root, login and stop SSH if you run it. With SSH Forward port 22 with a local SSH forwarding connection to the remote host.

[owen@thelinuxblog.com]$ sudo su -
[root@thelinuxblog.com]$ /sbin/service sshd stop
[root@thelinuxblog.com]$ ssh -p 1337 owen@thelinuxblog.com-L 22:<internal ip>:1337

Once this is done, your localhost:22 now forwards to your remote host. With another session (on your local machine) you can verify the connection by using ssh localhost. You will probably get warnings about the hosts identity being changed, or not verifying it, but you can ignore then. Once you’ve tested it, just use SVN as normal. When finished, remember to logout of the SSH session, and start SSH back up again if you run it.

Dealing with the HTML file input limitation of uploading multiple files

Filed under: General Linux — TheLinuxBlog.com at 8:59 am on Thursday, August 28, 2008

Everybody knows how annoying the <input type=”file”> HTML tag is right? Does it make you mad when you have to browse and upload each file individually? Sure you can use JavaScript to add / remove the input boxes, but you still need to browse for each file individually, which if you’re uploading lots of files doesn’t make sense.

Would you like a multiple file uploader like Facebook has? Perhaps more of a simple explorer like interface that will allow you to select multiple files? Possibly previewing them, and perhaps processing them on the client side?

Well, I wouldn’t say it was the easiest thing in the world to implement but there is an open source multiple file uploader that might suit your needs. Since its written in Java, its highly expandable (if you know how or pay a development company or freelancer) and can also be partially configured with JavaScript.

What is this fantastic sounding multiple file uploader you speak of?

Its called jupload and can be downloaded from jupload.sourceforge.net. Don’t let the website fool you because this tool is actually pretty neat.

If any one would like examples on how to use it, just write a blog post linking to me saying how cool it is and how much you need it, offer me cash, comment or participate in this blog, offer me goods / services, give me links from your website or just e-mail me politely asking for help and I’ll see what I can do.

If you don’t like it: start reading the documentation like I did, seriously its not that hard.

Why you should sign up for my RSS Feed!

Filed under: The Linux Blog News — TheLinuxBlog.com at 5:46 am on Friday, August 22, 2008

Why should you subscribe to my RSS feed?

Thats a good question and there are a couple of reasons I can think of off the top of my head.

For the longest time there was an application that I couldn’t remember the name of but thought the concept was as cool as the other side of the pillow. I remembered the name once an update to that application came into my subscribed feeds. I’m not saying I’ll keep you posted on every application or update that comes out, but I may jog your memory, spark some interest in something, help you figure something out or perhaps give you some ideas of your own.

You’ll be better able to participate, tell me how good/crappy my posts are and tell me I’m wrong or stupid. Either way, participation is good!

By subscribing to my RSS Feed I can tell how many actual readers I have. This is somewhat a measure of achievement or an ego booster for me to know that I have more than one reader.

Quite frankly I think results will vary from person to person and these outcomes are all just speculations but if you feel like making me happy whilst getting some useful information go ahead and click on the subscribe button. While your at it, if you have a Twitter go ahead and follow me on that too.

Just remember to keep your RSS reader open and if you choose not to subscribe to the RSS feed or following me then consider bookmarking thelinuxblog.com, or making it part of your daily routine. If you have any questions or complaints you can always e-mail me at: owen @<thelinuxblog.com> or drop me a comment. Thanks Guys!

Automatically reconnecting to a host

Filed under: Shell Script Sundays — TheLinuxBlog.com at 9:15 pm on Sunday, August 17, 2008

If you follow me on Twitter: http://www.twitter.com/LinuxBlog then you may know that I regularly update a bunch of Linux PC’s and servers. Now, since I’m sort of lazy and don’t like manually doing anything I don’t have to I thought I’d post the one liner I use to automatically reconnect to a host.

while ! ping -W 1 -c 1 <hostname or IP> 2>&1 >/dev/null; do true; done && sleep 15; ssh <user>@<hostname or IP>

This script uses the ping command to ping the server once (-c 1) with the timeout of 1 second (-W 1) ping a host or IP with a timeout of one second. Once the ping loop is broken (ping returns true) I let it sleep for 15 seconds to enable SSH to come up. Then the inevitable happens. I use SSH to reconnect to the host.

There you have it, a quick way to reconnect to a host without typing the command or pressing the up arrow every time. Enjoy!

Adding a service in Fedora

Filed under: Shell Script Sundays — TheLinuxBlog.com at 2:08 pm on Sunday, July 6, 2008

This week on Shell Script Sundays I’ll show you how to add a service to Fedora. This is very useful if you don’t happen to use yum for every service you want to run, and xinetd doesn’t really work for you.

Firstly there are three main parts to a Fedora service script. Start, Stop and Restart. They are pretty much self explanatory, but you don’t have to worry about the restart action since all it does is stop’s and then starts the service.

Without further ado here is the script:

#!/bin/bash
#
# Fedora-Service Update notification daemon
#
# Author:       TheLinuxBlog.com
#
# chkconfig:    1000 50 50
#
# description:  This is a test Fedora Service \
#               Second line of the fedora service template.
# processname:  FedoraTemplate
#
RETVAL=0;

start() {
echo “Starting Fedora-Service”
}

stop() {
echo “Stopping Fedora-Service”
}

restart() {
stop
start
}

case “$1″ in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
*)
echo $”Usage: $0 {start|stop|restart}”
exit 1
esac

exit $RETVAL

Now that you have a template for the script, you will want to modify it for your service. You need to keep the header at the top. This is how the Fedora Knows about your service. The three numbers indicate what order the scripts should start up and shut down in. The first seems to be a identification number and the other two are the startup and shutdown order. These can be adjusted depending on when you want the service to start up.Once you are done modifying the script put the script in /etc/init.d/

To make sure it works you can call it with service using the following actions:

service start
service stop
service restart

If all of the actions work, you are ready to add the service to the system. If you use the setup command as root it seems to do this step for you, but if you just want to add the service quickly without bothering to scramble through configuration menu’s you can do the following:

chkconfig –add <script name>

If you want the service to start automatically at boot up you can use ntsysv. For more information read my post on Managing Services on Fedora

Managing Services on Fedora

Filed under: Linux Software — TheLinuxBlog.com at 12:01 am on Wednesday, July 2, 2008

Fedora is a great desktop system, it has a pretty good services manager called service. It can be used to turn on, off and restart most services that run on the system. Should you want to manage running services all you have to do is type:

service <service name> <stop | start | restart>

If you don’t know the service name it can normally be found by issuing the –status-all switch. Doing so will output a long list of services so you may want to grep it or use a pager such as less:

[root@linux-blog ~]# service –status-all | less

[root@linux-blog ~]# service –status-all | grep snmp
capi not installed – No such file or directory (2)
JAVA_EXECUTABLE or HSQLDB_JAR_PATH in ‘/etc/sysconfig/hsqldb’ is set to a non-file.
snmpd is stopped
snmptrapd is stopped

While service is a great utility to manage services it does not stop them from running at startup. To manage services that start up when you boot you can use a handy little dialog script called “ntsysv”:

[root@linux-blog ~]# ntsysv

This will give you a nice dialog which is intuitive and similar to those of text based installers. Select the services you want to start up and then tab over to OK. I turn a lot of services off that I don’t use, if you’re unsure of what you need, try stopping it with the services command first and see if anything breaks. If after test it works well just turn it off.

Since I don’t use SELinux on my development machine, I always turn off setroubleshootd. I used ntsysv to stop it from starting at bootup and if I need it I can use the service command to start it.

Here is a screenshot of ntsysv in action:

managing services with ntsysv on Fedora

Sorting lists with Sort

Filed under: Quick Linux Tutorials — TheLinuxBlog.com at 11:11 pm on Wednesday, June 4, 2008

There comes a time when it is useful to know how to sort lists. Thankfully using open source software sorting text files can be very easy.

This example shows how to sort a file alphabetically using the sort command. I assume that you have a text file in mind so I will skip the creation of that. There are two ways to sort the file with the sort command. The first being the preferred method but not always suitable.

sort <textfile>

cat <textfile> | sort

Thats it! The second method can be useful if you would like to do any thing special, for example searching with grep, using cut or sed for data extraction or just out of convenience. Check out the man page for sort for more information on all of the options!

Using Bash Scripts in Web Applications

Filed under: Shell Script Sundays — TheLinuxBlog.com at 2:22 pm on Sunday, May 25, 2008

Using bash scripts for web applications is not exactly rocket science, nor is it necessarily the best idea in the world but it can be handy to do if you already have a bash script and want to use its functionality on the web. There are a couple of ways to use bash scripts on the web.

The first that I know of is as a CGI. All that you have to do for this one is create a cgi-bin or allow files with the extension .cgi to be executed this is done with apache in your httpd.conf file.

The Second is to use another scripting language to call the script. The easiest way for me is to use PHP. A system call to the script file can my made using the exec() function. Just make sure that the file has execute rights for the user that your web server runs as. Here is an example of using the exec() function in PHP:

$output = exec(‘/usr/local/bin/yourscript.sh’);

The Third method is to use Server Side Includes to include the script. I personally am not familiar with setting up SSI’s but this is how you execute a command from within a SSI:

<!–#exec cmd=”/usr/bin/date” –>

Which ever method you choose precautions have to be taken. Make sure that all inputs are sanitized so that a user cannot escape the command, pipe output to another file or manipulate the system in another way. In PHP it is easy to do this, but I can not speak for CGI’s or SSI’s. I hope this shows some insights as to how you can run bash scripts in your web application. If you have any other methods such as using mod_python or maybe tcl, please post them as a comment!

Bash Scripting Techniques

Filed under: Shell Script Sundays — TheLinuxBlog.com at 10:20 pm on Sunday, May 18, 2008

Here are some techniques that you can use in your bash scripts for finding and searching through files. Combined with other shell scripting techniques these can be very powerful.

Find all files in the current directory and print them:

find . -iname “.jpg”

Find all files that you have access to read with matching patern:

find / -iname “pattern”

Normally with grep text is matched and is case sensitive. Heres how to do a case insensitive search with grep:

cat <filename> | grep -i <match>

Finding and replacing text is easily done in bash with sed. This find and replace puts the contents into a new file:

 cat <filename> | sed ‘s/FIND/REPLACE/’ > <new filename>

Finding the line number that a particular line of text is on is sometimes useful. Here is how to do it:

 cat <filename> | grep -n <match>

Looping over a file in bash and echoing the output is sometimes useful for the processing of text files. Heres how to do it:

cat <filename> | while read i; do echo $i; done

Thats about all the bash scripting techniques that I can currently think of for finding in files. I know there are a ton more that I use but its hard to write them all down at once. As I come up with them or solve a problem I’ll add them here. If you have any of your own, please leave them in the comments.

RSS Feeds

Filed under: Shell Script Sundays — Kaleb at 11:43 am on Sunday, April 20, 2008

The other day I was playing around with AwesomeWM and I wanted to have the newest article from digg.com/linux_unix to be displayed in the statusbar. I thought to myself:

“I roughly know how RSS works, so I should be able to do this.”

It turns out it was extremely easy to do.

First how does RSS work. It’s easy just an xml file that gets downloaded with a list of the articles on the site. Well that’s pretty simple so I wrote a little script that will do all the things I need.

First I needed to download the list

wget -c http://digg.com/rss/indexlinux_unix.xml

done with that. Now for what I wanted and to make it a little cleaner i moved this file:

mv indexlinux_unix.xml ~/.news

this way it was in a file that i can easily access.

After that it was just some simple editing of the file using sed. If you don’t know much about sed I suggest you read up on it. It is an extremely powerful tool for quick editing and scripting. For the editing of
the file it was actually quite simple:

cat ~/.news | grep “<title>” | sed -e ‘s/<[/]title>//’ | sed -e ‘s/<title>//’ | sed -e ’2,2 !d’

now no worries I will explain this its actually quite simple.

I will assume you know what cat ~/.news does but if you don’t, it outputs the contents of the file until the end of the file.

| grep “<title>” is a very important part of the command. As I looked at the xml file i realized that i would get a simple list of all the articles if I greped the title. However thats not all.

It was a very messy output with <title> at the beginning and </title> at the end. Nobody wants to look at that, what I wanted was the text in between. | sed -e ‘s/<[/]title>//’ will get rid of the </title> in the line. I am almost certain that | sed -e ‘s/<\/title>//’ would have done that same thing but you can test that if you want. It needs to be done like this because “/” is a special character so it needs to be escaped.

The next part | sed -e ‘s/<title>//’ should be self explanatory. Basically it just gets rid of the <title> in the line. So now using the first 3 pipes you will get a nice pretty list of all the articles.

This is not what we wanted though. We wanted the newest article. so that’s why we use | sed -e ’2,2 !d’. This command will cut out everything except the second line in the list. “Hmm but why the second line Kaleb?”
well because while creating this script I found that the first <title> line was the line that told me where I was getting this information from. So it was http://digg.com/linux_unix now I don’t want that. so I went with the second line for the first article. Easy right.

Now as I mentioned at the begining of this article, I wanted to make this give me a clickable link for the awesome statusbar. I will go over awesome piping later this week but basically the only information you will need. Is to go threw your xml file for your RSS feed and find out between what tags the link for your article is and use the above command to show you that link instead of the title then have Firefox open that
link (or whatever browser you use). It was a very simple thing to do.

Kaleb Porter

porterboy55@yahoo.com

http://kpstuff.servebeer.com (website currently down)

« Previous PageNext Page »