Showing posts with label Shell Script. Show all posts
Showing posts with label Shell Script. Show all posts

Sunday, March 12, 2017

Shell script to fetch valid and unexpired domain from URL list



Lets support we have URLs in the file as below:
[root@core tmp]# cat > url_lists.txt
https://adhitprofits.com/?ref=just4us
http://ladys.ro/PGEUhnzx
http://8322699.fgxpress.com/
http://www.hourlychicpay.com/?ref=Galalmog
http://www.uprivatebanking.com/massimo8
http://promatrixplus.com/?ref=2909
http://www.trideci.com/index.php?rid=galalmog123
http://turbopayplan.com/?ref=wealthnmore
http://galalmog.stiforptour.com/


We need to list all the domain that are valid and not expired with sorted as below:
adhitprofits.com
gladys.ro
fgxpress.com
hourlychicpay.com
uprivatebanking.com
gpromatrixplus.com
trideci.com
gturbopayplan.com
stiforptour.com

Use Following shell command to filter out the domains.
cat all_url.txt | grep ".com" | grep -v [0-9] | grep -v "-" | grep -v "@" | tr '[:upper:]' '[:lower:]' | \
sed -e 's/http[s]*:\/\///g' | awk -F "/" '{print $1}' | awk -F "." '{print $(NF-1)"."$NF}' | sort | uniq > all_url_filtered_v1.txt

Descriptions:
Command Description
grep ".com":Search for .com only
grep -v [0-9]:Removes url with numbers
grep -v "-":Removes url with hyphen
grep -v "@":Removes url with @
tr '[:upper:]' '[:lower:]':Converts upper case into lower case
\:Line break for the command that make our command more readable
sed -e 's/http[s]*:\/\///g':Find and replace http:// and https:// with nothing
awk -F "/" '{print $1}':Split the URL on basic of / and print the first part only
awk -F "." '{print $(NF-1)"."$NF}':Split the URL on basic of . and fetch second last, last and add "." in between
sort:Sort the output
uniq:list all the unique output only
> all_url_filtered_v1.txt:Redirect all output to all_url_filtered_v1.txt



Install jwhois package that will fetch the Expiration Date of the domain.
yum install jwhois-4.0-19.el6.x86_64
whois oraclecloudadmin.com
whois oraclecloudadmin.com | grep "Expiration Date" | awk '{print $NF}' | awk -F "T" '{print $1}'





Create a script file that read every line from the output file and use dig command to find the valid domains on basic of ANSWER SECTION and use whois command to find the Expiration Date and compare with current date of the system.

cat test.sh  
#!/bin/bash

# loop throught the end of file
while read line
do
        # dig to find out the valid command
        nsoutput=$(dig ${line} | grep -A1 "ANSWER SECTION" | grep -v "ANSWER SECTION")
        # If we have answer section with IP address then the domain is considered as VALID
        if [ "${nsoutput}" != "" ]; then
                # Fetch the expiriry date of the domain and compare with the date.
                ED=$(whois ${line} | grep "Expiration Date" | awk '{print $NF}' | awk -F "T" '{print $1}')
                if [[ "${ED}" > "$(date +%Y-%m-%d)" ]]
                then
                        echo ${line} >> all_url_filtered_final.txt
                fi
        fi
 done < all_url_filtered_v1.txt



Saturday, January 28, 2017

Startup script for VMWARE ESXi 6

Login to the VM host machine via ssh.












List all the vms with the command below
vim-cmd vmsvc/getallvms








Get current status of the VM
vim-cmd vmsvc/power.getstate 10

Start VM with command line
vim-cmd vmsvc/power.on 10
vim-cmd vmsvc/power.on 6
vim-cmd vmsvc/power.on 9
vim-cmd vmsvc/power.on 8












Make startup script so that VM will start automatically on the computer boot.
Go to directory /etc/rc.local.d/
Create a file with name vm_startup.sh and following contents
/bin/vim-cmd vmsvc/power.on 10
/bin/vim-cmd vmsvc/power.on 6
/bin/vim-cmd vmsvc/power.on 9
/bin/vim-cmd vmsvc/power.on 8












Changing hostname of VMWARE ESXi

Get the current hostname and fqdn
esxcli system hostname get







Set the hostname and fqdn
esxcli system hostname set --fqdn=vm.ramro.com.np




Sunday, September 27, 2015

Encrypt bash script on Linux using shc

Download and install shc source.

wget http://www.datsi.fi.upm.es/~frosal/sources/shc-3.8.7.tgz
tar xzvf shc-3.8.7.tgz
cd shc-3.8.7
make
make install

























Create simple script file with following codes

#!/bin/bash

echo -e -n "Enter the first value \t:"
read a
echo -e -n "Enter the second value \t:"
read b
sum=$(expr $a + $b)
echo "Result is $sum"












Encrypt the test.sh script file with shc as below.


$ ./shc -f test.sh

List the files with ls command Now we have 2 more files with .sh.x and .sh.x.c extensions


[root@core scripts]# ls
test.sh  test.sh.x  test.sh.x.c









Execute the Encrypted script file with following command

 ./test.sh.x








Additional parameter that can be used with shc are below





 Setting expiration date on the script file with the message.

 shc -e '28/09/2015' -m "This script file is expired" -f test.sh






 

Execute the script before expiration date.

 ./test.sh.x 








 Change date and time with following command

  sudo date +%Y%m%d -s '20150928'







  


Execute the script after expiration date.  

  [dilli@core scripts]$ ./test.sh.x 
./test.sh.x: has expired!
This script file is expired









ulimit -c

If output is zero means that core file is not created.

Now we set core file size limit to 70000 byte

ulimit -c 70000

Now we start binary & segfault it right away.I used IP-Digger binary to get plain text from it.

./IP-Digger4.sh.x&  ( sleep 0.02 && kill -SIGSEGV $! )

 sleep 0.02 will give the binary enough time to start up and decrypt the original script. The variable $! contains the pid of the last background process started, so we can easily kill it with the segmentation fault signal SIGSEGV (same as kill -11 $!). 

+ segmentation fault (core dumped)  ./IP-Digger4.sh.x

cat core | strings >plain_text

Wednesday, June 3, 2015

bash Dynamic array variable name

#!/bin/bash

# Declaring variables
CNT=1

# Declaring dynamic array with space
eval "arr_row${CNT}=( \"rajiv maharjan\" \"shova maharjan\" )"
let CNT=CNT+1
eval "arr_row${CNT}=( \"dilli maharjan\" \"sapana maharjan\" \"samjhana maharjan\" \"bipana maharjan\" \"tirtha maharjan\" )"

# Manually displaying dynamic array variable
echo "Displaying first row"
CNT1=1
VAR="\${arr_row${CNT1}[0]}"
echo ${VAR}
echo $(eval "echo ${VAR}")

CNT1=2
VAR="\${arr_row${CNT1}[0]}"
echo ${VAR}
echo $(eval "echo ${VAR}")
VAR="\${#arr_row${CNT1}[@]}"
#echo ${VAR}
#echo ${#arr_row2[@]}

# Using loop to iterate all the array elements
for i in $(seq 0 $(expr $(eval "echo $VAR") - 1))
do
        VAR1="\${arr_row${CNT1}[${i}]}"
        eval "echo ${VAR1}"
done