Archive for July, 2008
How to use Secure Shell (SSH)
Jul 31st
Secure shell (SSH) is a replacement for telnet. telnet uses plain text protocol so a man in the middle can see what is being exchanged between client and server including sensitive information like passwords. A more sophisticated hacker can even hijack your connection.
SSH deals with these vulnerabilities of telnet service using SSL and uses data encryption. Let us see how SSH can be used to login to a remote system for shell access.
[liz@techpulp ~]# ssh liz@neo.techpulp.com The authenticity of host 'neo.techpulp.com (65.35.38.45)' can't be established. RSA key fingerprint is fa:e7:9f:10:e4:bb:de:2a:c9:aa:7f:92:be:4a:58:7c. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently More >
PHP Page Redirection
Jul 27th
This article explains how to redirect to another page using PHP. You can use header function in the beginning of PHP script to inform browser about page redirection. Basically this writes Hyper Text Transfer Protocol(HTTP) header with the field Location. You should not write any text before writing HTTP header. However you can have some text after writing header and it will appear in the browser while browser attempts to access the new page. Generally you won’t notice the message unless your internet connection is slow and browser is taking more time to retrieve new file.
<?php
header('Location: http://mysite.com/newpage.html');
?>
More >
How to resolve “Address already in use” error with bind system call
Jul 26th
To get rid of “Address already in use” error with bind system call, SO_REUSEADDR socket option should be set before invoking bind. This makes operating system to allow the socket to bind if there is no active socket bound to the requested address and port. Add the following code to the code snippet provided in “Find peer IP address and port number using getpeername” article.
/* create a socket */
sd = socket(AF_INET,SOCK_STREAM,0);
opt = 1;
if(setsockopt (sd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof (opt)) < 0) {
perror("setsockopt");
exit(1);
}
...
Find local IP address and local port number using getsockname
Jul 26th
The getsockname function can be used to find the local port used by a given socket. It is valid to use this function only if the socket is already using a local port number. i.e A server socket which invoked bind system call with zero as port number. A client socket which has established connection to a server. In either case operating system selects an unused port number automatically. Similarly this function can also be used on UDP sockets.
The following code snippet explains how to use getsockname to retrieve the local port number assigned by operating system.
#include <stdio.h> #include <stdlib.h> #include <string.h> #include More >
Find peer IP address and port number using getpeername
Jul 26th
The getpeername function can be used to retrieve the peer IP address and port number on a given socket. For this to work, the socket should have a valid TCP connection established. Typically this is used by server to find the IP address and port number of client.
In the following code snippet, a TCP server binds to port number 5555 and waits for clients. It retrieves client’s IP address and port number using getpeername if a client is connected.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/un.h>
int main()
{
struct sockaddr_in sin; More > How to extract files from RPM
Jul 26th
All the files present in a RPM file can be extracted using rpm2cpio and cpio utilities. The rpm2cpio command is used to convert RPM to cpio compatible archive and cpio command can be used to extract files from the archive.
The following example shows how extract files from ElectricFence RPM.
[liz@techpulp ~]# rpm2cpio ElectricFence-2.2.2-23.rpm | cpio -idv
Identify processes that have open sockets
Jul 26th
The lsof can be used to find the list of processes that have active sockets open.
Let us see the list of current TCP connections using netstat command.
[liz@techpulp ~]# netstat -nt Active Internet connections (w/o servers) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 192.168.20.24:40816 74.125.19.83:80 ESTABLISHED tcp 0 0 192.168.20.24:43059 74.125.19.83:80 ESTABLISHED [liz@techpulp ~]#
Now let us find which process has these TCP sockets open using lsof command.
[liz@techpulp ~]# /usr/sbin/lsof -i
COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
firefox-b 3288 liz 13u IPv4 157814 TCP
liz.techpulp.com:43058->cf-in-f83.google.com:http (ESTABLISHED)
firefox-b 3288 liz 40u IPv4 158205 TCP
liz.techpulp.com:43059->cf-in-f83.google.com:http (ESTABLISHED)
[liz@techpulp ~]#
So Firefox More >
Linux file compression tools/utilities
Jul 26th
Linux has various types of compression and decompression tools like tar, gzip, bzip2, zip and compress. This article describes practical usage of these utilities.
tar and untar
tar command operates on one or more files or directories and creates an archive. An archive created by tar command is called a tarball. Generally such archives will have .tar as file extension. tar is more of a archiving utility than a compression utility. Typically it is used along with file compression utilities like gzip and bzip2. See various ways of creating a tarball using -cvf option in the following examples.
[liz@techpulp ~]# tar -cvf mydir.tar mydir/ [liz@techpulp ~]# tar -cvf myfiles.tar file1.txt More >
Print first N characters of a string using printf
Jul 26th
#include <stdio.h>
int main(int argc, char *argv[])
{
char *p = "My Name is Neo";
printf("First 5 bytes of string is '%.5s'\n", p);
return 0;
}
Result
[neo@techpulp ex]# gcc example.c -o example [neo@techpulp ex]# ./example First 5 bytes of string is 'My Na' [neo@techpulp ex]#


Recent Comments