Linux Blog

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!

Writing Loops

Filed under: The Linux Blog News — TheLinuxBlog.com at 2:34 pm on Friday, March 7, 2008

The act of a loop is doing something over and over until a condition. Like in bash scripts I get stuck in loops while trying to write posts for The Linux Blog. The problem is that when I get stuck in a loop I can write a lot of posts about one topic (for example SSH.) But I don’t want to have all of my front page covered in posts about SSH. This is where my readers (You) come in.

I made a promise last month that I would not ask for any more writers. Since this is a new month I can start over again. Basically any one willing to write short articles or how to’s is welcome to submit an article to me. I will be making sure that the WordPress login script works over this weekend that way if you would like to help me out and gain some exposure you can do so very easily. By writing for me it will help out with the content of this site by not making it all similar topics. Other than that news I’m working on getting some new features for this site worked out. When I do it should be interesting for all of us.

Have a great Friday!

– Owen.

Snippet: Keeping SSH Running

Filed under: Quick Linux Tutorials,Shell Script Sundays — TheLinuxBlog.com at 12:53 am on Sunday, November 4, 2007

I wrote a post not so long back called Bringing The Internet Up After Failure that explained how I was restarting my network services after the internet went down.

Shortly after this I was remotely working when I thought it would be a good idea to restart my SSHD to enable X11 forwarding. After running the script that normally restarts the service I tried to reconnect. Unfortunately the service never restarted after being stopped. This is not a good situation for any one to be in so I added something like this to my cron along at the end of my network services script:

ps ax | grep \usr\/sbin\/sshd | grep Ss && echo "SSHD Running" || echo "Starting SSH"; sudo /etc/rc.d/rc.sshd start

The only difference between the version I am running and the version above is that I don’t echo anything out. All the command does is use the ps command and grep for usr/sbin/sshd then grep for the STAT field of sS. I do this because sshd shows up in the process list as shown below:

owen@the-linux-blog$ ps ax | grep sshd
3463 pts/0    R+     0:00 grep sshd

The rest is self explanitory, it echo’s “SSHD Running” or sudo starts the SSHD.

If SSHD ever terminates on me or those of us who run this snippet we will be able to rightfully regain access to our systems, Hurray!

« Previous Page