Wednesday, July 31, 2013

Using "wget" command for transferring files in Linux machines

Enable the http server in a Linux machine

Go to, vim /etc/httpd/conf/httpd.conf (In Redhat)

Look for "Listen X.X.X.X:80", remove the # mark before it and change it to "0.0.0.0:80"

Now restart the service using the command:

service httpd restart

Not go to some Client machine, which is directly reachable from the Server. And you can get files from  the server using the command:

If you want to get a file "sample.txt" from "/var/www/html" folder, use the following command:

wget ftp://USER:PASSWORD@192.168.10.11://file.txt

1) Provided the Server is reachable with IP 192.168.10.11 and user credentials as "USER", password as "PASSWORD".
2) "/var/www/html" is the default location where the files are stored.
To change this location, go to file "/etc/httpd/conf/httpd.conf" and look for "DocumentRoot" and change this "/var/www/html" to "/home/user/" and then restart the httpd service, now you can download files from "/home/user"
e.g. To download a file with name "sample.tgz", use

wget ftp://user:user123@192.168.10.11://home/user/sample.txt



How to Start ftp service in Linux

1) FTP ( File Transfer Protocol ) is the simplest and most secure way to exchange files over    internet/intranet.
2) Transferring files from a client computer to a server  is called " Uploading" and transferring from a
 server to a client is “Downloading”
3) Port :21 is used for Control connection and Port 20 is used for Data Connection

Setting up and "ftp" server

SERVER SIDE :
Required package: vsftpd
Use this command to install the package (in Ubuntu): sudo apt-get install vsftpd

Changing configuration file "vsftpd.conf"; for allowing other users to access/write data.
vim /etc/vsftpd.conf
local_enable=YES
write_enable=YES
#anon_upload_enable=YES
After finishing the required changes 'save' and 'exit' from the .conf file

Now restart the "ftp" service using the following command:
sudo /etc/init.d/vsftpd restart
OR
service vsftpd start
Some other self explanatory commands:
service vsftpd status
service vsftpd stop
service vsftpd restart

CLIENT SIDE :
To do ftp to X.X.X.X IP PC we use,
ftp X.X.X.X
ftp>
Use the following commands within the 'ftp' command prompt:
Basic get/put commands
get filename
put filename
put/get commands with confirmation
mget filename
mput filename

bye-to exit from the ftp command prompt.

Tuesday, July 23, 2013

Find and Replace in Shell/Bash Scripts

We can do this Find and Replace task in Scripts using vi/vim editors.

Open your Script with vi/vim editor, then
:%s/WordToReplace/ReplaceWith/g

Finding and Replacing a word with Confirmation
:%s/WordToReplace/ReplaceWith/gc

Case Insenitive Finding and Replacing
:%s/OLD/new/gi
This will find all the words "Old, OLd,olD..." and Replaces with word "new"

Case Insensitive Find and Replace
:%s/OLD/new/gI

Find and Replace between L1 and L2 lines
:L1,L2s/OLD/new/g

Finding and Replacing only specific word
:%s/\<OLD\>/new/gc








Commenting some lines of code in Shell/Bash Scripts

To comment a set of lines in a Shell script:
1) Using if false and fi

#!/bin/bash
a=2
if false
then 
b=a
echo a
printf "-------\n"
fi
printf "%d %d\n", a, b


2) Using Comments
#!/bin/bash
a=2
<<COMMENT
b=a
echo a
printf "-------\n"
COMMENT
printf "%d %d\n", a, b

3) Using Search and Replace option

To comment code between lines L1 and L2 we can use
:L1,L2s/^/#/

Wednesday, July 10, 2013

Static ARP entries in Linux

Sometimes ARP between machines may not resolve. In that case we may need to add Static Routes manually.
Adding a Static ARP in Linux Machine:

#arp -s <IPAddr> <MAC>

Deleting a Static ARP in Linux Machine: 

#arp -d <IPAddr>

IPAddr - IP Address of the Host (Other PC) 
MAC - Other PC MAC