Friday, September 24, 2010

How to physically locate a FreeBSD machine using its shell

If a machine is being used only through remote access, it is very likely that one will forget its physical location. And one won't care where it is located unless one day you plan to upgrade the hardware of the machine. This is when one will try to recall where the machine is placed. Most of the time, its not easy to recall where the machine is located.

Wouldn't it be great if it was possible for you to tell the machine to make some noise so that you can locate it? Actually, it is possible. How? very easy :

Load speaker device in kernel:
kldload speaker

Then you can use it this way
Code:
echo "BP" > /dev/speaker
echo "SO4L16G>L8C." > /dev/speaker

Tuesday, June 22, 2010

How to use regex in find command

Today I discovered how to use regex in unixs' find command. It has a very simple syntax as below:

Syntax: find \path\ -regex '\regex pattern\'
e.g. find ./ -regex 'file[0-9]+.txt'

In above example, we are looking for files with name like file1.txt or file23.txt or file567.txt. This is a match on the whole path, not a search. For example, to match a file named `./fubar3', you can use the regular expression `.*bar.' or `.*b.*3', but not `f.*r3'.

According to find man page, it is also possible to specify the type of regex to be used, like emacs regex, posix-awk, posix-basic, posix-egrep and posix-extended. Default is emacs regex.

Another option is -iregex. It is just like -regex, but the match is case insensitive.

Monday, June 21, 2010

How to specify destination for tar extraction

Specify a destination to extract a tar archive
It is very simple, say:
/root/file.tar.gz - gzipped tar archive file
/usr/local/tmp - destination location where above file needs to be extracted to.

command:
tar -xzf /root/file.tar.gz -C /usr/local/tmp

You might have already noticed '-C' option. This can be used to explicitly specify destination location if it is other than PWD.


Sunday, March 21, 2010

Selenium: Upload Files

Selenium despite of being a great tool can be very painful at times. It is when you are trying hard to automate some action and despite of putting all the steps correctly Selenium fails to do what you want. If the warrior within you do not want to give up easily, you will definitely end up draining hours of energy to figure out that one li`l bit. In the end you may triumph or forfeit. I have gone through such experiences a number of times and many a times all the hard work pays in the end. In the jubilation of figuring out the riddle, I almost always forget to document steps those would help me in case I stumble upon the same situation later in time. But this time I made sure that I document it and put it on the Internet so that I can also change to a giver from an all time taker from the Internet.

This time I was trying to automate a part of web page where the script was supposed to upload a file by clicking on a browse button, choose a local file and upload it. Very easy to do when done manually but not so easy through Selenium if you don't know what exactly needs to be done. First of all, clicking on the browse button and selecting a file using selenium doesn`t work. Instead Selenium RC has a function, type(), which is used to type text into text boxes or text fields. This function can be used to type in the full path of the file into the browse element. Example:

$sel->type($locator, $filename);

Here $locator can be the name or id/value pair or just id of the browse element. For more information on locators in Selenium please see this.

Once Selenium has picked up the mentioned file using type function, now its time click on upload button. If you are lucky you'll need not to do anything more and just use click function of Selenium and get going. But modifying browse button tends to misbehave. I noticed that once you have used type function to select a file, Selenium will loose focus and won't be able to execute click function on upload button successfully. The solution of this is to get focus of upload button using focus function from WWW::Selenium and then use click function. So the final sequence turns out to be as below:

$sel->type('browseid', '/home/dir/filename');
$sel->focus('uploadid');
$sel->click('click');

This worked for me very well, hope will be helpful to all of you :)

Tuesday, December 23, 2008

Sockets in Perl

A Simple Example:
$ perl test.pl
The server had a response line of: HTTP/1.1 200 OK

CODE:
#!/usr/bin/perl -w
use Socket;

#Initialize a socket
my $proto = getprotobyname('tcp');
socket(FH, PF_INET, SOCK_STREAM, $proto) || die $!;

#Establishing a network connection
my $sin = sockaddr_in(80, inet_aton('10.10.2.2'));
connect(FH, $sin) || die $!;

#send the GET method with / as a parameter
my $buffer="GET / HTTP/1.0\n\n";
syswrite(FH, $buffer, length($buffer));

#reading the response
sysread(FH, $return_line, 200);

# print out the response
print "The server had a response line of: $return_line";
close(FH);
1;


Functions Details:


CLIENT SOCKET CALLS
Initializing a Socket:

function name: socket()
parameters: SH - File handle to be associated with newly created socket.
PF_INET - Indicates IP(Internet Protocol) is to be used.
getprotobyname('tcp') - Indicates TCP is to be used on top of IP.
SOCK_STREAM - this indicates the socket is stream oriented as opposed to record oriented.

e.g.: socket(SH, PF_INET, SOCK_STREAM, getprotobyname('tcp')) || die $!;
if the socket call fails, the program should die using the error missage in
$!.

Establishing a Network Connection:

function name: connect()
parameters: Socket::sockaddr_in() - Accepts a port number as first parameter and a 32 bit IP address as the second parameter.
Socket::inet_aton() - translates a hostname string or a dotted decimal string into a 32 bit IP address.


e.g.: my $sin = sockaddr_in(80,inet_aton('www.google.com'));
connect(SH, $sin) || die $!;
sockaddr_in() returns a data structure that is then passed to connect(). From there connect() attempts to establish a network connection to the specified server and port. Upon successful connection it returns a true otherwise it returns a false.


Writing a Data to a Network Connection:

function name: syswrite()
parameters: FH - File handle to write the data to.
$buffer - Data to write is second parameter.
length($buffer) - Third parameter is length of the data to be written.
e.g.: $buffer = "Hello World";
syswrite(FH, $buffer, length($buffer));


function name: print()
parameters: FH - File handle to write the data to.
data - Data to be written.


e.g.: select(FH);
$|=1; # set $| to non-zero to make selection autoflushed
print FH "hello world!";

Reading Data from a Network Connection:
function name: sysread()
parameters: FH - first parameter is a file handle used to specify the connection to read from.
$buffer - Second parameter is a scalar variable store data into.
$num - Third parameter is max number of bytes to read from the connection.

e.g.: sysread(FH, $buffer, 200);

Closing the connection:
close(FH);


SERVER SOCKET CALLS

First create a socket:
my $proto = getprotobyname('tcp');
socket(F, PF_INET, SOCK_STREAM, $proto) || die $!;


Bind the socket with a port number on the machine:
function name: bind()
parameters: F - File handle.
sockaddr_in() - to identify the port for bind().

e.g. my $sin = sockaddr_in(80, INADDR_ANY);
bind(F, $sin) || die $!;

Waiting for a connection:

function name: listen();
parameters: F - File handle.
$length - queue lenght in case of multiple clients trying to connect.

e.g. listen(F, $length) || die $!;

Accepting a connection:
function name: accept();
b FH - File handle accept will associate with a specific network connection.
F - A generic file handle associated with a server socket.

e.g. accept(FH, F) || die $!;