Linux Blog

Finding a PC on your network

Filed under: General Linux, Linux Software — TheLinuxBlog.com at 4:31 am on Tuesday, September 4, 2007

When I’m at a remote location I sometimes need to gain access to a computer that is not accessible from the internet but is on the same network as another machine that is. I have remote SSH access into the box that is on the same network but I often don’t know the IP address for the computer that I am trying to gain access to since they are assigned via DHCP.
I have a simple solution that will locate the computer I wish to use once logged into the gateway in no time. The program needed is from our friends at insecure.org and is called nmap. It is a pretty standard tool so it should be included with your distribution. However if you do not own the Linux machine then it may not be installed or you may not have the ability to install it or have sufficient privileges to run it.
Basically what nmap does is scan the network.
The command I use to scan a whole subnet for my host is:

nmap -sP 192.168.x.x/24

The type of scan I use is a ping scan, I only determine if the host is online. If I know the DHCP pool starts from 1.1 and ends at 1.100 then I would use:

nmap -sP 192.168.1.1-100

This will yield faster scanning results since it only has to ping 100 hosts not the whole subnet.
Sometimes if the environment is a busy one (one with lots of hosts) a lot of online IP’s will be returned and its hard to identify which one your trying to connect to. I remedy this by just scanning the host range I need that only have port 22 open.

nmap 192.168.3.1-100 -sT -p22

The -sT option doesn’t require the user to be root but if you have root the -sS option is better as it gives detailed information such as the mac address which can come in handy if you happen to know what brand of network card is in the computer you are trying to log into.

This will work to find a computer on a network with Linux but it still requires you to know a little information about the PC your trying to find. If you need a better way of finding your PC’s I would recommend using static IP’s and DNS. Give it a shot if your on location somewhere and need access to your computer.

Linux on Household Appliances

Filed under: General Linux — TheLinuxBlog.com at 9:52 am on Monday, September 3, 2007

Linux is a versatile enough operating system that it could virtually run on anything. Me and some friends have joked about installing Gentoo on toasters but in reality how far away are we from this? For example lets take the Smart Fridge. Its basically a fridge that keeps track of what you have in it. Its packed with lots of features my favorite is its ability to tell you what recipes you can currently make with the ingredients that are in it. The smart fridge could easily be developed with Linux as its plat from.

Next lets look at other house hold items that we take for granted such as washers and dryers. Almost every house hold that owns a computer will have a washer and dryer. I would like to be able to have my washer and dryer notify me via e-mail, on screen display or other means when the cycle is finished. This would be easy to implement with an embedded OS such as Linux. There would be no extra electronics on the appliance because all of the required electronics are already in place. A simple serial or parallel interface would have enough bandwidth to give a detailed information on the current cycle or status.

What else is there? Ovens, Microwaves, Dishwashers or any other house hold appliance. Even simple tasks such as turning them on and off remotely would make sense. For the hardware hacker these features are easy to implement.

I don’t think that we are far from having these features. Take the VCR for example, once the record feature was mainstream some one came up with the idea to schedule recording on the units. A while later we got the Tivo DVR which has even better options for scheduling.

Is Linux going to be running on mainstream appliances? I believe it could be in time. Its already running on so many networking applications. Linksys run a modified version of it on their routers and on network attached storage devices. So I think it will only be a matter of time before it works its way into other mainstream devices. All that is needed is for a company to implement these features and a good marketing campaign. I know I would buy an intelligent household appliance if it would make my daily tasks easier.

Mass Modifying HTML Templates with Bash

Filed under: Shell Script Sundays — TheLinuxBlog.com at 8:48 am on Sunday, September 2, 2007

As a web applications developer I’m often doing web development work with static HTML pages. Shell scripts can be very powerful when it comes to web development I have some techniques that I use that save me time on tasks.

I recently used a series of shell scripts for web development to finish off a site that was split into two sections a content section and a template. The template was designed and finished but the content pages was not wrapped within the HTML pages. I did not want to use a dynamic scripting language like PHP to generate the pages so I came up with a few scripts to make my life a little easier and save me from copying and pasting in all of the content into the correct pages.

The first thing was that the person who designed the site interchanged between .html and .htm within the pages so what I did to fix that and make future work less complicated I renamed all of the files with a small shell script:

for x in $(ls -1|grep html);
do
RENAME=$(echo $x | cut -d . -f -1)
mv $x $RENAME.htm
done

That may not be the most elegant way to do it, but it took care of the file renaming for me. All I had to do now was make sure all of the pages did not link to the old HTML pages. To do this I used sed and a loop to find and replace as followed

for x in $(ls -1|grep htm);
do
cat $x | sed ’s/html/htm/g’ > $x
done

Since I was just adding content to the main section of the template I divided the HTML into two sections, the HTML that would go above the content and the HTML that would go below. I split the HTML into two separate files a file named “top.txt” and a file named “bottom.txt”. Since the content was designed in a uniform matter I simply appended the “bottom.txt” file by redirecting output:

cat bottom.txt >> contentfile1.htm

Now that the files had the bottom section of the template the top.txt had to be prefixed. To achieve this I used a file reversing method. Basically I overwrote the top file with the reversed version by using tac

tac top.txt > new_top; mv new_top top.txt

This is one way to reverse and overwrite a file (the other is in the next step).
Once I reversed the file, I performed the following:

cat contentfile1.htm | tac > contentfile1.htm; cat top.txt >> contentfile1.htm; cat contentfile1.htm | tac > contentfile1.htm

This line is quite long but what its doing is simple. Firstly it reverses the file, then it appends the already reversed top.txt. The last step is to reverse the whole thing again.
Sometimes depending on how many files I have to process I will put this in a simple loop but if its a reasonable amount of files then I’ll do it by hand.

One thing to always remember when using shell scripts on web development work is to create backups. There is nothing worse than saving time by using a script to do the work but having to write more scripts to undo things that went wrong. In this case I a lot of time but not as much as I could have if I had kept backups for each step of the way.

« Previous Page