Shashikant shah

Monday 12 October 2015

How to Encrypt Your Bash Shell Script on Linux


1. Download shc and install it

Download shc and install it as shown below.

# wget http://www.encryptsystem.com/download/sch/shc-3.8.7.tgz
# tar xvfz shc-3.8.7.tgz
# cd shc-3.8.7
# make

Verify that shc is installed properly.

$ ./shc -v
shc parse(-f): No source file specified

shc Usage: shc [-e date] [-m addr] [-i iopt] [-x cmnd] [-l lopt] [-rvDTCAh] -f script

2. Create a Sample Shell Script

$ vi script.sh
#!/bin/bash

echo “Hello friends my first encrypt bash script.”

$ ./script.sh
Hello friends my first encrypt bash script.

3. Encrypt the Shell Script Using shc

Encrypt the script.sh shell scripting using shc as shown below.

$ ./shc -f script.sh
This will create the following two files:

$ ls -l script.sh*
-rwxrw-r–. 1 shashi shashi 149 Mar 27 01:09 script.sh
-rwx-wx–x. 1 shashi shashi 11752 Mar 27 01:12 script.sh.x
-rw-rw-r–. 1 shashi shashi 10174 Mar 27 01:12 script.sh.x.c

4. Execute the Encrypted Shell Script

$ ./linux.sh.x
Hello friends my first encrypt bash script

5. Specifying Expiration Date for Your Shell Script

Using shc you can also specify an expiration date. i.e After this expiration date when somebody tries to execute the shell script, they’ll get an error message.

Let us say that you don’t want anybody to execute the script.sh.x after 31-Dec-2014 (I used last year date for testing purpose).

Create a new encrypted shell script using “shc -e” option to specify expiration date. The expiration date is specified in the dd/mm/yyyy format.

$ ./shc -e 31/12/2014 -f script.sh
In this example, if someone tries to execute the random.sh.x, after 31-Dec-2014, they’ll get a default expiration message as shown below.

$ ./script.sh.x
./script.sh.x: has expired!
Please contact your provider
If you like to specify your own custom expiration message, use -m option (along with -e option as shown below).

$ ./shc -e 31/12/2014 -m “Contact admin@encryptsystem.com for new version of this script” -f script.sh

$ ./script.sh.x
./script.sh.x: has expired!
Contact admin@encryptsystem.com for new version of this script

6. Create Redistributable Encrypted Shell Scripts

Apart from -e, and -m (for expiration), you can also use the following options:

-r will relax security to create a redistributable binary that executes on other systems that runs the same operating system as the one on which it was compiled.
-T will allow the created binary files to be traceable using programs like strace, ltrace, etc.
-v is for verbose
Typically you might want to use both -r and -T option to craete a redistributable and tracable shell encrypted shell script as shown below.

$ ./shc -v -r -T -f script.sh
shc shll=bash
shc [-i]=-c
shc [-x]=exec ‘%s’ “$@”
shc [-l]=
shc opts=
shc: cc script.sh.x.c -o script.sh.x
shc: strip script.sh.x
shc: chmod go-r script.x

$ ./script.sh.x
Hello friends my first encrypt bash script.

Finally, it is worth repeating again: You should not be encrypting your shell script in the first place. But, if you decided to encrypt your shell script using shc, please remember that a smart person can still generate the original shell script from the encrypted binary that was created by shc.






Friday 9 October 2015

script for insert variable to other tty

#!/bin/bash
COMMAND=$1
OBJ_SEQUENCE_NUM=$2
RUN_COMMAND=$(echo "$COMMAND" | tr '[a-z]' '[A-Z]')
file="/tmp/update_obj.txt"

if [[ ! -f $file ]];then
    echo "$file not found."
else
    remove=$(rm -rf /tmp/update_obj.txt)
fi
    case $RUN_COMMAND in
        "COMPLETE")
        echo "comlete comand exc $2."
        ;;
        "FAIL")
        echo "fail comand exc $2."
        ;;
        *)
        echo "invaild command."
        exit 0
        ;;
esac

arr=$(echo $OBJ_SEQUENCE_NUM | tr "," "\n")

for i in $arr
do
create_file=$(echo "$RUN_COMMAND $i" >> /tmp/update_obj.txt)
all_command=$(cat /tmp/update_obj.txt)
done

sleep 1

. ./OBJCmdLineadmin.ksh << EOF > update.log
    $all_command
    exit
EOF
=========================================

script.sh complete 7673254,87328

Tuesday 6 October 2015

Script for copy data

#!/bin/bash

Souce_path=$1
Dest_path=$2

if [[ "$Souce_path" == '' ]] || [[ "$Dest_path" == '' ]];then
    echo "Source or Destination path can not be empty.";
    exit 0
fi

run=$(cp -rf $Source_path $Dest_path 2> /tmp/err)
input=$?
   
    if [ "$input" = = 0 ];then
    echo "Successfuly copied Directory and File."
else
    echo "Do not copy Directory and File."
exit 2
fi
=====================
# ./script  /shashi/test.txt   /test/

Monday 13 July 2015

ARRAY Details

$ cat arraymanip.sh
#! /bin/bash
Unix[0]='Debian'
Unix[1]='Red hat'

echo ${Unix[1]}

$./arraymanip.sh
Red hat
====================
declare -a Unix=('Debian' 'Red hat')
echo {unix[@]}
#./t.sh
Debian Red hat
==================

declare -a shashi=('jk'' 'gol 'hello');

echo ${shashi[0]}

#./script.sh

jk
====================
Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');

echo ${#Unix[@]} #Number of elements in the array
echo ${#Unix}  #Number of characters in the first element of the array.i.e Debian
echo ${#unix [1]} # length of the element located at index.
echo ${unix[@]:1:3} # remove the sentense in one line.e.g:- remove sentense 1 & 3
echo ${unix[2]:1:2} # remove the word in sentance.
echo ${unix[@]/ubuntu/sco unix} # ubuntu ko replace karega.
echo ${#unix[*]} # last file name.

unset unix[1] #1 is not showing.
unset Unix    # unix array not showing
================
$cat arraymanip.sh
Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');
Unix=("${Unix[@]}" "AIX" "HP-UX")
echo ${Unix[7]}

$./arraymanip.sh
AIX

========================
diff_file
file1
file2
file3

#!/bin/bash
file=`cat "diff_file"`
for t in ${file[@]}
    do
echo $t
    done
echo "read file content!"

#./script.sh
file1
file2
file3

=======================

#!/bin/bash
# define file array
files=(/etc/*)

# find total number of files in an array
echo "Total files in array : ${#files[*]}"
total=${#files[*]}

# Print 1st file name
echo "First filename: ${files[0]}"
echo "Second filename: ${files[1]}"
echo "Third filename: ${files[1]}"

# total - 1 = last item (subscript) in an array
echo "Last filename: ${files[$(( $total-1 ))]}"

echo
echo "****************"
echo "*** For Loop ***"
echo "****************"

# Use for loop iterate through an array

# $f stores current value

for f in "${files[@]}"
do
    echo -n "$f "
done

echo
echo
echo "**************************"
echo "*** C Style For Loop ****"
echo "**************************"
echo
# Use c style for loop
# get total subscripts in an array
total=${#files[*]}
#
for (( i=0; i<=$(( $total -1 )); i++ ))
do
    echo -n "${files[$i]} "
done

echo

=======================close======================================
cat file
2012/05/21
2012/05/22

vim test.sh

OLDIFS=$IFS
IFS="/"
while read f1 f2 f3
do
    echo "Year is : $f1"
    echo "Month is : $f2"
    echo "Date is : $f3"
done < file
IFS=$OLDIFS

./test.sh
Year is : 2012
Month is : 05
Date is : 21

Year is : 2012
Month is : 05
Date is : 22

====================================
# bash -c 'set w x y z; IFS=":-;"; echo "$*"' 
 
# w:x:y:z

Sunday 12 July 2015

script for data copy to remote server and date file matching script


1.Put to remote host password :-
# echo "echo redhat" > /root/shashi/passwd

 2.Create a data copy script :-
#!/bin/bash

export SSH_ASKPASS="/root/shashi/passwd"

setsid ssh -o StrictHostKeyChecking=no "root"@"192.168.10"

exit 1

OR

setsid scp -r "/root/shashi/*" "root"@"192.168.0.10:/root/shashi/data"
==========================
# chmod +x passwd
#chmod +x script.sh
#./script.sh
==========================================================
===========================================================

#!/bin/bash

Path=/tmp

d=$(date +%d --date="1 days ago")

file=$(ls -lh $Path | awk '{print $6 " " $7 " " $9}' | sed -n /$d/p)

count=`echo -n "$file" | wc -l`

    if [ 0 == "$count" ];then

echo "date is not Match with file"

else

echo "date is Match with file"

fi
-=========================================================
 How do I backup /var/www/html using rsync?

Run rsync over SSH using password authentication, passing the password on the command line:
$ rsync --rsh="sshpass -p myPassword ssh -l username" server.example.com:/var/www/html/ /backup/

======================================================================
export SSHPASS=password
sshpass -e ssh user@remotehost

Saturday 11 July 2015

diff script in linux

#!/bin/bash
batch=/home/user/test/test1
prod=/home/user/test/test2

echo -n "Enter DC FTP-1 Prod path: "
    read ftpbatch

echo -n "Enter DC FTP-2 Prod  path: "
    read ftpprod

echo -n "Enter File name to campare : "
    read file

export A=$ftpbatch
export B=$ftpprod
export C=$file

E=$(rm -rf $batch/$C* | rm -rf $batch/*)
F=$(rm -rf $prod/$C* | rm -rf $prod/*)

D=`wget -P $batch ftp://$server1$A/$C 2>&1 /tmp/ftp_wget_logs`
G=$(echo $?)
if [ 0 == "$G"]; then
    echo "file has been download $batch/$C"
else
    echo "file is not found ftp://$server1$A/$C"
exit 1

T=`wget -P $batch ftp://$server1$B/$C 2>&1 /tmp/ftp_wget_dest_logs`
H=$(echo $?)
if [ 0 == "$H"]; then
    echo "file has been download $prod/$C"
else
    echo "file is not found ftp://$server1$B/$C"
exit 1

I=$(cd $batch && wc -l $C)
J=$(cd $prod && wc -l $C)
# DD=$(sed "s/ /\\ /g" /tmp/ab) world me space hai to.
#$(cd $batch && wc -l "$DD")

H=$(cd $batch && du -sh $C | awk '{print $1}')
H=$(cd $prod && du -sh $C | awk '{print $1}')
cmd1=$(diff -u --ignore-all-space $batch/"$C" $prod/$"$C") sare space remove
cmd=$(diff --changed-group-format='%>%<' --unchanged-group-format='' $batch/$C $prod/$C)
Z=$(diff --changed-group-format='%>%<' --unchanged-group-format='' $batch/$C $prod/$C | wc -l)

if [ 0 == "$Z" ];then
cat << EOF > /home/user/test/diff_logs
===================================
"File-Name: $C ,File-Size : $H, TL: $I"
"File-Name: $C ,File-Size : $G, TL: $J"
"DIff is not found in both files."
==================================
EOF
else
cat << EOF > /home/user/test/diff_logs
"File-Name: $C ,File-Size : $H, TL: $I"
"File-Name: $C ,File-Size : $G, TL: $J"
$cmd
=====================================
EOF
fi

=============== close=============================================
diff contune 
 ===============================================================
#!/bin/bash
batch=/home/user/test/test1
prod=/home/user/test/test2

A=$(ls -l $batch | awk '{print $9}' | sed 1d > /tmp/prod_ftp1)
B=$(ls -l $prod | awk '{print $9}' | sed 1d > /tmp/prod_ftp2)
C=`diff --changed-group-format='' --unchanged-group-format='%>' /tmp/prod_ftp1 /tmp/prod_ftp2`
D=`echo -n "$C" > /tmp/prod_ftp3`

total=${#files[*]}

for (( i=0; i<=$(( $total -1 )); i++ ))
do
    echo -n "${files[$i]"
done

Rename File in linux

We are find the file name shashi_kant and Rename to shashi-kant.

#!/bin/bash
FILE1=shashi_kant
PATH1=/home/shashi/R0_data

if [ 0 == 0 ]; then

A=$(ls -l $PATH1 | grep -i $FILE1 | awk {'print $9'} > /tmp/R0_shashi)
for file in `cat /tmp/R0_shashi`
do
B=$(echo $file | tr '[:lower:]' '[:upper:]')
C=$(mv   $PATH1/$file  $PATH1/$B)  ### CAPITAL
done
fi

if [ 0 == 0 ]; then

B=$(ls -l $PATH1 | grep -i $FILE1 | awk {'print $9'} > /tmp/R0_shashi1)
for file1 in `cat /tmp/R0_shashi1`
do
D=`mv   $PATH1/$file1  $PATH1/${file1/${file1:6:1}/-}`; ## rename file
done
fi

Thursday 26 February 2015

Installing and Configuring RSH on Linux

rsh  is used to execute commands on a remote machine.
The rsh command  executes the   command or a program in another host from current working  machine without having to login into that remote machine  by entering a password as in ssh. You can run any unix command, or a shell script of a remote host.

Requirement :-
Remote Machine access without password.

Server :- 192.168.0.10 Server.example.com

Client:-192.168.0.11 client.example.com

192.168.0.10 server.example.com
User :- test

192.168.0.11 client.example.com
User :- test1

1.Both Machine in  /etc/hosts file :-

192.168.0.10  server.example.com server
192.168.0.11 client.example.com client


2.Install some package at Server.example.com :-

# yum install xinetd
# yum install rsh-server rsh*
# rpm -qa|grep -i ^rsh-server

Server side :-

3.Now you need to Edit /root/.rhosts to add lists of hosts that can access without password. If you want to allow access to everyone without password, you need to put '+' in the file. A '+' indicates allowing everyone.

# vi /root/.rhosts
client.example.com root
cilent.example.com test1
# chmod 600 /root/.rhosts

4.test1 user access all data from server machine as a user test.:-
#vim /home/test/.rhosts
client.example.com test1

Note :-Test1 user access remote machine “server.example.com” and login as a test user.  

5.Add a full permission to use the command over the network. Plus sign will give a full permission.
vi /etc/hosts.equiv + +

6.Now you need to allow RSH , RLogin and Rexec to be used.  Open these files one by one and then set the "disable=no"

/etc/xinetd.d/rsh ,
/etc/xinetd.d/rlogin 
 /etc/xinetd.d/rexec

7. Now you need to modify the file  to allow password less  to the machine.

a)/etc/pam.d/rlogin
b)/etc/pam.d/rsh
c)/etc/pam.d/rexec

BEFORE: auth    required        pam_rhosts_auth.so

AFTER : auth    required        pam_rhosts_auth.so promiscuous

8.Enable to services :-

# chkconfig rsh on
# chkconfig rexec on
# chkconfig rlogin on
# chkconfig xinetd on

9.Restart xinetd service:-

# service xinetd restart

10.Add line in /etc/securetty :-
rsh,
rlogin
rexec

Client side :-

11.Testing  on client.example.com, rlogin and rsh to nodes without passwords:-

# yum install rsh

As a login “root” user and  run below commands :-

# rlogin 192.168.0.10
# rsh 192.168.0.10
# rsh 192.168.0.10 -l root ls -al /tmp

As a login “test1” user and run below command :-
# rsh 192.168.0.10 -l test

# rsh 192.168.0.10 -l test ls -al /tmp

# rsh 192.168.0.10 -l root

12.Copy data command  :-

# rcp –r  data test@server.example.com:
# rcp –r  file test@server.example.com:data

13.check port for rsh :-

# netstat -n --inet


Tuesday 24 February 2015

check ssh connection to remote host

#!/bin/bash

count=$(netstat -ntpa | grep ssh | grep -i EST* | wc -l)

if [ "$count" -gt 0 ]; then
comm=$(netstat -ntpa | grep ssh | grep -i EST* | awk '{print $7}' |
cut -d "/" -f1 > /tmp/ssh_1)

        for ID in `cat /tmp/ssh_1`
do
        user=`who -a | grep -ih "\W*\w*$ID\w*\W*" | grep -w  $ID | awk
'{print $1}'`
        IP=`who -a | grep -i $ID | awk '{print $8}' | cut -d "(" -f2 |
cut -d ")" -f1`
        Date=`who -a | grep -i $ID | awk '{print $4,$6}'`

cat << EOF
Date-Time :- $Date
username :- $user
IPAdd:- $IP
====================
EOF
done
else

echo "No ssh connection in Host"
fi

Wednesday 18 February 2015

Check latest line add in file.

#!/bin/bash

Latest_dir=/home/shashi/latest

diff=$(diff ${Latest_dir}/test* | sed 1d | sed 's/<//g' | sed '/^$/d' | sed 's/^ //g' | wc -l)

new=$(diff ${Latest_dir}/test* | sed 1d | sed 's/<//g' | sed '/^$/d' | sed 's/^ //g' | awk '{print FNR ")" $0}')

if [ 0 = "$diff" ]; then

    echo "not a add latest line in file"
else
    cat << EOF
Latest line is :-
${new}
EOF

cop=$(cp -rf ${Latest_dir}/test ${Latest_dir}/test1)

fi

Monday 16 February 2015

Script for Continue Copy Latest File.

#!/bin/bash
SOURCE=/home/shashi/data
R=/home/shashi/latest/first_log
D=/home/shashi/latest/second_log
DIST=/home/shashi/backup_latest
file=$(touch ${D})

while [ 0 = = 0 ]
do

$(find ${SOURCE} -mmin -1 | sed 1d > ${R})

DIF=$(diff ${R} ${D} | awk '{print $2}' | sed '/^$/d')

I=$(echo "${DIF}" > ${D})

        for copy in `cat $D`
        do
        cp -f $copy $DIST

        done
done

Thursday 15 January 2015

Delete files based on date :-

#!/bin/bash

P=$1
d=$(date +%d --date="2 days ago")

file=$(ls -lh $P | awk '{print $6 " " $7 " " $9}' | sed -n /$d/p | awk '{print $3}')

echo  "$file" > /tmp/word

for w in `cat /tmp/word`
        do
        find $P -name $w >> /tmp/path
done

e=`cp -rf /tmp/path /tmp/path.bkp`

c=`echo -n "$file" | wc -l`
 echo "count $c "
if [ 0 == "$c" ];then

        echo "not deleted."
exit 2
else
        for x in `cat /tmp/path`
        do
        rm -rf $x
done
fi
rm -rf /tmp/path
====================================================
./script.sh  /dir_name

Thursday 8 January 2015

Curl Command Used :-



Curl Command :-
Transferring data from one place to another is one of the main task done using computers connected to a network. There are so many GUI tools out there to send and receive data, but when you are working on a console, only equipped with command line functionality, using curl is inevitable. A less known fact is that curl can work with a wide range of protocols and can solve most of your scripting tasks with ease. Before getting into details of curl use cases and examples, let's see who is behind its development.

What is curl and what makes it a superb command line tool?
CURL is simply awesome because of the following reasons...
1.     CURL is an easy to use command line tool to send and receive files, and it supports almost all major protocols(DICT, FILE, FTP, FTPS, GOPHER, HTTP, HTTPS,  IMAP, IMAPS,  LDAP,  LDAPS,  POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMTP, SMTPS, TELNET and TFTP) in use.
2.     Can be used inside your shell scripts with ease
3.     Supports features like pause and resume of downloads
4.     It has around 120 command line options for various tasks
5.     It runs on all major operating systems(More than 40+ Operating systems)
6.     Supports cookies, forms and SSL
7.     Both curl command line tool and libcurl library are open source, so they can be used in any of your programs
8.     It supports configuration files
9.     Multiple upload with a single command
10. Progress bar, rate limiting, and download time details
11.  ipv6 support.

1. Download a Single File

The following command will get the content of the URL and display it in the STDOUT (i.e on your terminal).

2.Fetch Multiple Files at a time

We can download multiple files in a single shot by specifying the URLs on the command line.
Syntax:
$ curl -O URL1 -O URL2

3.Follow HTTP Location Headers with -L option

By default CURL doesn’t follow the HTTP Location headers. It is also termed as Redirects. When a requested web page is moved to another place, then an HTTP Location header will be sent as a Response and it will have where the actual web page is located.
For example, when someone types google.com in the browser from India, it will be automatically redirected to ‘google.co.in’. This is done based on the HTTP Location header as shown below.
$ curl http://www.google.com
 
<TITLE>302 Moved</TITLE>
<H1>302 Moved</H1>
The document has moved
<A HREF="http://www.google.co.in/">here</A>
The above output says that the requested document is moved to ‘http://www.google.co.in/’.
We can insists curl to follow the redirection using -L option, as shown below. Now it will download the google.co.in’s html source code.
$ curl -L http://www.google.com
 

4. Continue/Resume a Previous Download

Using curl -C option, you can continue a download which was stopped already for some reason. This will be helpful when you download large files, and the download got interrupted.
If we say ‘-C -’, then curl will find from where to start resuming the download. We can also give an offset ‘-C <offset>’. The given offset bytes will be skipped from the beginning for the source file.
Start a big download using curl, and press Ctrl-C to stop it in between the download.
$ curl -O http://www.gnu.org/software/gettext/manual/gettext.html
##############             20.1%
Note: -# is used to display a progress bar instead of a progress meter.
Now the above download was stopped at 20.1%. Using “curl -C -”, we can continue the download from where it left off earlier. Now the download continues from 20.1%.
curl -C - -O http://www.gnu.org/software/gettext/manual/gettext.html
###############            21.1%

5. Limit the Rate of Data Transfer

You can ask curl to limit download speed to your desired value. This is very helpful, when you do not want your curl command to consume much of the available bandwidth. This can be done using the below method.
$ curl --limit-rate 100k -O http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.10.tar.gz

6. Download a file only if it is modified before/after the given time

We can get the files that are modified after a particular time using -z option in curl. This will work for both FTP & HTTP.
$ curl -z 21-Dec-11 http://www.example.com/yy.html
The above command will download the yy.html only if it is modified later than the given date and time
$ curl -z -21-Dec-11 http://www.example.com/yy.html
The above command will download the yy.html, if it is modified before than the given date and time.
Please refer ‘man curl_getdate’ for the various syntax supported for the date expression

7. Pass HTTP Authentication in cURL

Sometime, websites will require a username and password to view the content ( can be done with .htaccess file ). With the help of -u option, we can pass those credentials from cURL to the web server as shown below.
$ curl -u username:password URL
Note: By default curl uses Basic HTTP Authentication. We can specify other authentication method using –ntlm | –digest.

8. Download Files from FTP server

cURL can also be used to download files from FTP servers. If the given FTP path is a directory, by default it will list the files under the specific directory.
$ curl -u ftpuser:ftppass -O ftp://ftp_server/public_html/xss.php
The above command will download the xss.php file from the ftp server and save it in the local directory.
$ curl -u ftpuser:ftppass -O ftp://ftp_server/public_html/
Here, the given URL refers to a directory. So cURL will list all the files and directories under the given URL
If you are new to FTP/sFTP, refer ftp sftp tutorial for beginners.

9. List/Download using Ranges

cURL supports ranges to be given in the URL. When a range is given, files matching within the range will be downloaded. It will be helpful to download packages from the FTP mirror sites.
$ curl   ftp://ftp.uk.debian.org/debian/pool/main/[a-z]/
The above command will list out all the packages from a-z ranges in the terminal.

10. Upload Files to FTP Server

Curl can also be used to upload files to the FTP server with -T option.
$ curl -u ftpuser:ftppass -T myfile.txt ftp://ftp.testserver.com
The above command will upload the file named myfile.txt to the FTP server. You can also upload multiple files at a same time using the range operations.
$ curl -u ftpuser:ftppass -T "{file1,file2}" ftp://ftp.testserver.com
Optionally we can use “.” to get the input from STDIN and transfer to the remote.
$ curl -u ftpuser:ftppass -T - ftp://ftp.testserver.com/myfile_1.txt
The above command will get the input from the user from Standard Input and save the contents in the ftp server under the name ‘myfile_1.txt’.
You can provide one ‘-T’ for each URL and the pair specifies what to upload where.

11.Deleting files on FTP server

You can also delete files on your FTP server using curl command, as shown below.
$ curl ftp://example.com/ -X 'DELE myfile.zip' --user username:password

11. More Information using Verbose and Trace Option

You can get to know what is happening using the -v option. -v option enable the verbose mode and it will print the details
curl -v http://google.co.in
The about command will output the following
* About to connect() to www.google.co.in port 80 (#0)
*   Trying 74.125.236.56... connected
* Connected to www.google.co.in (74.125.236.56) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.21.0 (i486-pc-linux-gnu) libcurl/7.21.0 OpenSSL/0.9.8o zlib/1.2.3.4 libidn/1.15 libssh2/1.2.6
> Host: www.google.co.in
> Accept: */*
* HTTP 1.0, assume close after body
< HTTP/1.0 200 OK
< Date: Tue, 10 Apr 2012 11:18:39 GMT
< Expires: -1
< Cache-Control: private, max-age=0
< Content-Type: text/html; charset=ISO-8859-1
< Set-Cookie: PREF=ID=7c497a6b15cc092d:FF=0:TM=1334056719:LM=1334056719:S=UORpBwxFmTRkbXLj; expires=Thu, 10-Apr-2014 11:18:39 GMT; path=/; domain=.google.co.in
.
.
If you need more detailed information then you can use the –trace option. The trace option will enable a full trace dump of all incoming/outgoing data to the given file

12. Use Proxy to Download a File

We can specify cURL to use proxy to do the specific operation using -x option. We need to specify the host and port of the proxy.
$ curl -x proxysever.test.com:3128 http://google.co.in

13. Send Mail using SMTP Protocol

cURL can also be used to send mail using the SMTP protocol. You should specify the from-address, to-address, and the mailserver ip-address as shown below.
$ curl --mail-from blah@test.com --mail-rcpt foo@test.com smtp://mailserver.com
Once the above command is entered, it will wait for the user to provide the data to mail. Once you’ve composed your message, type . (period) as the last line, which will send the email immediately.
Subject: Testing
This is a test mail
 
               OR
$curl --url "smtps://smtp.example.com:465" --ssl-reqd   --mail-from "user@example.com" --mail-rcpt "friend@example.com"   --upload-file mailcontent.txt --user "user@example.com:password" –insecure
 
In the above command, replace smtps://smpt.example.com:465 with your SMTP server and port.
--mail-from: This field contains the from address that the receiver should see.
--mail-rcpt: This field contains TO address
--upload-file: The file provided here should contain your message as a content
--user: SMTP user@domain:password
--insecure option used is exactly same as using -k option we saw earlier, to ignore unknown SSL certificates.

14.Using CURL to send HTTP POST, PUT, DELETE requests

Apart from the above seen examples, curl can be used to send different types of HTTP request methods to your server. This becomes very much handy if you want to configure an application using REST API. I will show you some example's of sending REST API requests using CURL.

Sending POST request using CURL

If you use CURL to send request to a server without any request method option in the argument, it will by default use HTTP GET request. Which is a nice behavior. But using -X option in curl, you can send your required HTTP method to the server. Let's see POST request.

$ curl -X POST -u  admin:admin http://example.com/myconfigs/status -Hcontent-type:application/xml -d @/home/user/file.xml


In the above example, PUT is the request method, -u is used to mention credentials to access that specific resource on the server, -H content-type is the type of content format that we will be sending to the server (can be xml, normal text etc). -d with @/home/user/file.xml indicates to send the content of file.xml to the server. This file will contain the configuration options with correct syntax that the URL http://example.com/myconfigs/status will accept.
Sending PUT request is exactly same as the above shown example of POST. Simply replace POST with PUT in the above example. If your web server does not accept these methods, you will get a 405 error as reply. HTTP 405 means that the server does not accept the HTTP method on that specific URL.  
You can also get HTTP unsupported Media Type error as reply, if the server does not accept application/xml format. The HTTP status code for unsupported media type is 415.
 

15.Ignore SSL Certificate Error with CURL



16.Modify User Agent In HTTP request

When a web browser sends an HTTP request, it includes its own details in the request. This detail allows the server to identify the browser details. The HTTP request header that includes this information is called as User-Agent. Basically server's can be configured to respond with different page layout for different user agent's. The user agent information send by the client will be visible in server logs.
A normal HTTP request send by a Firefox web browser will have a user agent that looks something like "Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.2.3)"
Using curl you can modify user agent value to your required string, as shown below.

$ curl -A "YOUR USER AGENT STRING GOES HEERE" http://example.com


17.Sending Cookies to Server using CURL

CURL can be used to send requests to the server using a previously got cookie. You can either use VALUE=DATA format or give a file name as parameter, so that curl can use cookie data from the file as input. You can also save the cookies you get from the server to a file using -c option as shown below.

$ curl -b mycookies.txt http://example.com