Mount Google Drive on Linux (Debian) Server

For mounting Google Drive on your server. you will require two things.
  1. A project created and configured with OAuth client id and scecret on Google
  2. google-drive-ocamlfuse installed and configured
Create Project

1. go  to  https://console.developers.google.com/apis/dashboard

and create new project



Click on create credentials



Select OAuth Client ID



Click on configure Consent Screen



Provide email address and product name



To Create Client ID select others and provide Name and click on create



It will give you OAuth Client ID and Secret. Please note it down and keep safe.



Installation of google-drive-ocamlfuse On debian 9
1. First install the required packages
apt-get install libcurl4-gnutls-dev libfuse-dev libgmp-dev libsqlite3-dev camlp4-extra debianutils libcurl4-gnutls-dev perl  m4 pkg-config zlib1g-dev

2. adduser <user> fuse ( adding user to group fuse usrmod also works)

3. Set the Permissions

#sudo chown root.fuse /dev/fuse
#sudo chmod 660 /dev/fuse

4. Install Google Drive Ocamlfuse

# su <user>
# opam init
# opam update
# opam install depext
# eval `opam config env`
# opam depext google-drive-ocamlfuse
# opam install google-drive-ocamlfuse
# . /home/<user>/.opam/opam-init/init.sh > /dev/null 2> /dev/null || true

#/home/<user>/.opam/system/bin/google-drive-ocamlfuse -headless -label googledrive -id <OAuth Client ID> -secret <OAuth Client Secret>

It will give you one url ask you to visit that url and get the code from the webpage and provide it



Open that url in browser and copy paste  the verification code to termianl

create mount point

# mkdir /mnt/Google-drive

#  /home/<user>/.opam/system/bin/google-drive-ocamlfuse -label googledrive /mnt/Google-drive/

Above command will enable that mount only for the user and not others not even root

To enable mount point for user root

edit file /etc/fuse.conf and uncomment below line

#user_allow_other

Then run below command ( In case of ubuntu  exclude sudo -u <user> and run command directly as root.)

#sudo -u <user> /home/<user>/.opam/system/bin/google-drive-ocamlfuse -o allow_root -label googledrive /Google-drive/ > /var/log/gdrive_mount.log 2>&1 &

Add below line (/etc/rc.local)

sudo -u  <user> /home/<user>/.opam/system/bin/google-drive-ocamlfuse -o allow_root -label googledrive /Google-drive/  2>$1  & 

Thus only user and root will be able to use mounted drive.

 

Country settings in AWStats



By implementing this setting in AWStats you can track the Geo location of visitor of your website.

Follow the procedure to implement the settings
#mkdir /usr/local/share/GeoIP
#cd /tmp wget -N http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
#gunzip GeoIP.dat.gz mv GeoIP.dat /usr/share/GeoIP
#wget -N http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz

or or or

#yum install mod_geoip
#yum install geoipdate
#yum install geoip-devel

Now Edit the file configuration file for your website in /etc/awstats  and  search below lines

#LoadPlugin="geoip_city_maxmind GEOIP_STANDARD /pathto/GeoIPCity.dat"
#LoadPlugin="geoip GEOIP_STANDARD /pathto/GeoIP.dat"

and change to

GeoIPCity.dat LoadPlugin="geoip GEOIP_STANDARD /usr/share/GeoIP/GeoIP.dat"
LoadPlugin="geoip_city_maxmind GEOIP_STANDARD /usr/share/GeoIP/"

It may give you error like below



















To Fix this error run below commands
#yum install perl-CPAN
#cpan YAML
#cpan Geo::IP::PurePerl Geo::IP
#ldconfig -v

  

Web based terminal : shellinabox

First install epel repository  
#yum install epel-release

Install shellinabox by below command

#yum install shellinabox

The shellinabox config file is located in /etc/default/shellinabox file by default in Debian/Ubuntu systems. In RHEL/CentOS/Fedora, the default location of config file is /etc/sysconfig/shellinaboxd.

To change web terminal color scheme to white on black follow the following steps

#vim /etc/sysconfig/shellinaboxd
 comment below line
 OPTS="--disable-ssl-menu -s /:LOGIN"

uncomment below line
 OPTS="--user-css Normal:+black-on-white.css,Reverse:-white-on-black.css --disable-ssl-menu -s /:LOGIN"

This enables right change in profile on right click on browser.

restart the service

note* sometimes the white-on-black.css is not installed. then service will not restart/start and will give error

workaround :

cd /usr/share/shellinabox/

#cp black-on-white.css white-on-black.css
#sed -i s/ffffff/111111/g white-on-black.css
#sed -i s/000000/ffffff/g white-on-black.css
#sed -i s/111111/000000/g white-on-black.css

now restart the service.

(you can change colours by changing the hex colour code in css.)

now for web terminal open a link

https://ip-address:4200 (default port is 4200 you can change it to any by editing config file i.e. /etc/sysconfig/shellinaboxd)



  

Encrypting a shell script

Go to the link https://www.datsi.fi.upm.es/~frosal/sources/ and download latest stable source for shc
in this case the latest source is  shc-3.8.9b.tgz

#cd /usr/local 
#sudo wget  https://www.datsi.fi.upm.es/~frosal/sources/shc-3.8.9b.tgz
#sudo tar -zxvf shc-3.8.9b.tgz
#cd shc-3.8.9
#make
#make test
#make strings
#make expiration
#mkdir -p /usr/local/man/man1
#make install

now shc in installed on your system in /usr/local/bin

to encrypt the script

#shc -help gives the complete information about how we can use the package

shc -help
shc Version 3.8.9b, Generic Script Compiler
shc Copyright (c) 1994-2015 Francisco Rosales <frosal@fi.upm.es>
shc Usage: shc [-e date] [-m addr] [-i iopt] [-x cmnd] [-l lopt] [-rvDTCAh] -f script

 -e %s Expiration date in dd/mm/yyyy format [none]
 -m %s Message to display upon expiration ["Please contact your provider"]
 -f %s File name of the script to compile
 -i %s Inline option for the shell interpreter i.e: -e
 -x %s eXec command, as a printf format i.e: exec('%s',@ARGV);
 -l %s Last shell option i.e: --
 -r Relax security. Make a redistributable binary
 -v Verbose compilation
 -D Switch ON debug exec calls [OFF]
 -T Allow binary to be traceable [no]
 -C Display license and exit
 -A Display abstract and exit
 -h Display help and exit

 Environment variables used:
 Name Default Usage
 CC cc C compiler command
 CFLAGS <none> C compiler flags

 Please consult the shc(1) man page.


###### simple encryption ####
write a test script like below

#!/bin/bash 
echo Test Script

#shc -f test
This will create 2 files test.x (executable binary) and test.x.c(C source code) 

test.x is an executable file
test.x may or may not run on system depending upon kernel as it is non traceable

to overcome this problem we must compile our script as traceable and redistributable 
so that it can run on any system by any user.

#shc -Tf test (or you can write it in simple way shc -T -f test)

***** Encryption with expiration date and message *****

provide expiration date with -e in dd/mm/yyyy format and with -m type a message which you want to display after script expiration

#shc -e 01/01/2000 -m "This script expired.Contact your admin" -Tf test

Since the date is in past the encrypted script is already expired and after running ./test.x it will give message "This script expired.Contact your admin"

### The one more thing you can do with this is compile the C source code into the binary by below command.##

#gcc -o <binary-file-name> test.x.c

This will create c compiled binary file of your script. 
The only difference between the shc compiled binary and c compiled binary is shc compiled binary is stripped while the c compiled binary is not stripped. (non stripped binaries have debugging information built into it)
This is something different that you should try. 

Realtime monitor your friends are online / offline status in office (notification)

Suppose I have three friends Rupesh Parag and Rizwan having their laptops then
the following script will help me to check whether they are online or offline and notify me if any change in the status

save this script as notification 
and set cron to run this script every minute

#!/bin/bash
rupesh=192.168.100.102
parag=192.168.100.101
rizwan=192.168.100.110
 
rupeshstatus=/tmp/rupeshstatus
paragstatus=/tmp/paragstatus
rizwanstatus=/tmp/rizwanstatus

start_time=$(date +%s)

######### Checking for Rupesh ##########################3

if [[  ! -f $rupeshstatus ]];
then
echo offline > $rupeshstatus
fi

ping -c1 $rupesh
rustatus=`echo $?`
#echo sstatus is $rustatus

if [ "$rustatus" -eq 0 ];
then
rup=online
else
rup=offline
fi

#echo $rup

ru=`cat $rupeshstatus`

#echo $ru
#echo "$rup" -ne "$s"
if [[ "$rup" != "$ru" ]];
then
#xmessage  -center "Rupesh $rup" -display :0
sshpass -p a ssh -o StrictHostKeyChecking=no akash.l@192.168.100.199 "xmessage -timeout 2 -c "Rupesh is $rup" -display :0 & "
echo $rup > $rupeshstatus
fi

if [[  ! -f $rupeshstatus ]];
then
echo offline > $rupeshstatus
fi
############ checking for Parag #############################################


if [[  ! -f $paragstatus ]];
then
echo offline > $paragstatus
fi

ping -c1 $parag
pstatus=`echo $?`
#echo sstatus is $pstatus

if [ "$pstatus" -eq 0 ];
then
prg=online
else
prg=offline
fi

#echo $prg

p=`cat $paragstatus`

#echo $v
#echo "$prg" -ne "$v"
if [[ "$prg" != "$p" ]];
then
#xmessage  -center "Parag is $prg" -display :0
sshpass -p a ssh -o StrictHostKeyChecking=no akash.l@192.168.100.199 "xmessage -timeout 2 -c Parag is $prg -display :0 &"
echo $prg > $paragstatus
fi

######## checking for Rizwan ##########################

if [[  ! -f $rizwanstatus ]];
then
echo offline > $rizwanstatus
fi

ping -c1 $rizwan
ristatus=`echo $?`
#echo sstatus is $sstatus

if [ "$ristatus" -eq 0 ];
then
riz=online
else
riz=offline
fi

#echo $rup

ri=`cat $rizwanstatus`

#echo $s
#echo "$rup" -ne "$s"
if [[ "$riz" != "$ri" ]];
then
#xmessage  -center "Rizwan is $riz" -display :0
sshpass -p a ssh -o StrictHostKeyChecking=no akash.l@192.168.100.199 "xmessage -timeout 2 -c Rizwan is $riz -display :0 &"

echo $riz > $rizwanstatus
fi


finish_time=$(date +%s)
echo "Time duration: $((finish_time - start_time)) secs."


 

How to setup AWStats for apache log analyzer (CentOS 6.8)

 awstats
 
1) Installation
 
 #yum install epel-release
 #yum install httpd
 #chkconfig httpd on
 #service httpd start
 #yum install awstats

2) Configuration

 a) Changes in log format in httpd.conf
 
 check the below entry for log format. if it does not exist then add it.    
 LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
    

 b) changes in virtualhosts 

 change your log configuration like below
 CustomLog logs/yourdomain.com-access_log combined
 
 c) configuring awstats.conf for apache 
 
 #vim /etc/httpd/conf.d/awstats.conf

  add below entries in <Directory "/usr/share/awstats/wwwroot">
  </Directory> container

  AuthUserFile "/path-to-file/.htpasswd"
  AuthName "Restriceted Access"
  AuthType Basic
  Require user "htuser"


 Then create password file by below command
 #htpasswd -c /path-to-file/.htpasswd htuser
 #chmod 404 /path-to-file/.htpasswd htuser

d) creating configuation file for awstat

 #cp /etc/awstats/awstats.localhost.localdomain.conf /etc/awstats/yourdomain.com.conf
 
 then edit the file /etc/awstats/yourdomain.com.conf

# vim /etc/awstats/yourdomain.com.conf and change the below entries according to domain

LogFile="/var/log/httpd/yourdomain.com-access_log"
SiteDomain="yourdomain.com"
HostAliases="yourdomain.com www.yourdomain.com"
( for multiple domains its better to create seperate files for each domain)

e) setting permissions 


now change permissions for the awstat folder

#find  /usr/share/awstats -type d -exec chmod 701 '{}' \;
#find /usr/share/awstats  -type f  -exec chmod 404 '{}' \;

#chmod +x  /usr/share/awstats/wwwroot/cgi-bin/awstats.pl
#chmod 400 /etc/awstats/*.conf


Once you make all the changes make sure to restart apache to get configuration changes into effect

#/etc/init.d/httpd stop
#/etc/init.d/httpd start

or #/etc/init.d/httpd restart

make necessary changes according to your domain in below url  to check the statistics
http://www.yourdomain.com/awstats/awstats.pl?config=yourdomain.com

Image to pdf

#!/bin/bash
 #tiff-to-pdf is 6 to 7 times faster script than this script
 start_time=`date +%s`
 ########## Section 1 ##########
 # This command can convert any image to PDF document, version 1.3 ( very slow as compared to tiff2pdf ) but slightly faster than section 2

mogrify -format pdf *

######### Section 2 ##########
 # this command can convert any image to PDF document, version 1.3

#ls  | while read filename; do
 #file=`echo $filename |sed 's/\(.*\)\..*/\1/'`
 #convert $filename $file.pdf
 #done

finish_time=`date +%s`

echo "Time duration: `expr $finish_time - $start_time` secs."

Check up/down hosts in your network

To check how many systems are up and how many systems are down in network.use below script

I used nohup to avoid ping output on screen

for i in {1..254} ; do ip=192.168.0.$i && nohup ping -c1 $ip 2> /dev/null && echo "$ip online" || echo "$ip offline" ; done

tiff to pdf

#!/bin/bash
# this file converts tif image to pdf file in present directory and all subdirectoris.
start_time=$(date +%s)

find . -type f -iname "*.tif*" | while read tif; do
 pdf=`echo $tif |sed 's/\(.*\)\..*/\1/'`
 if [[ ! -f "$pdf.pdf" ]];
 then
 tiff2pdf -o "$pdf.pdf" "$tif"
 else
   tstamp=`date +%N`
   pdf="$pdf-n$tstamp"
   if [[ ! -f "$pdf.pdf" ]];
   then
   tiff2pdf -o "$pdf.pdf" "$tif"
   else 
   tstamp=`date +%S`
   pdf="$pdf-n$tstamp"
   tiff2pdf -o "$pdf.pdf" "$tif"
   fi
fi
done
finish_time=$(date +%s)
echo done
echo "Time duration: $((finish_time - start_time)) secs."

create tar archive from the list of files

If you want to create tar file from the list (The list may contain files from different folders).

then use below command

#tar -zcvf  mytar.tar.gz -T list

This command will read list line by line and keep adding the file mentioned in list to the tar file