Hardcore Linux

Anything about Ubuntu, Centos, openSuSe and Fedora

Category Archives: bash

tail -f with highlighting

A little help with perl

http://sysadminnotebook.blogspot.com/2012/09/tail-f-with-highlighting.html?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+SysadminNotebook+%28Sysadmin+Notebook%29

Advertisements

Getting WAN IP on console or shell script.

There are few other guide in getting your current WAN IP on Linux console or fetching it within a shell script. But this one works for me.

1. To get the current WAN IP:

$> echo "$(wget http://automation.whatismyip.com/n09230945.asp -O - -o /dev/null)"

2. For LAN IP address/es:

$> ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'

 

 

Folder Monitoring Script using Inotifywait

Though it is not failsafe nor flawless, but currently it is working great for me. Here’s a script that enables to monitor the events from a specified folder/directory.

First install the inotify-tools:

apt-get install inotify-tools

Here’s my current code, feel free to enhance.


#!/bin/bash
# monitor file changes in a target directory

if [ "$*" = "" ]; then
 echo "File Monitoring"
 echo "USAGE: $0 <target directory>" | sed "s/.\///g"
 echo ""
 exit 0
fi

LOGDIR="$HOME/.log"

if [ ! -d $LOGDIR ]; then
 mkdir -p $LOGDIR
fi

TARGET_DIR="$*"
LOGNAME="$LOGDIR/`date -I`.log"

inotifywait -m -r --format '%T %e %w%f' --timefmt '%F %T' -e modify -e move -e create -e delete $TARGET_DIR | while read line
do
echo "$line" >> $LOGNAME
done

exit 0

Converting Multiple Images via BASH Script

Previously, I posted a flawed script that be able to convert images and lower their resolution as well, though it works for me, but found it very problematic sometimes. Here’s another script I made to perform same purpose: Convert the image to either jpg|png|pdf|tif and has an option to modify the quality [1% to 100%].

example:

converting all images in /home/user/Desktop/rawimg to PDF with 50% quality

$> convert2.sh /home/user/Desktop/rawimg pdf 50

Here’s my script:

 

#!/bin/bash
TARGET_LOCATION="$1"
TARGET_TYPE="$2"
QUALITY="$3"
if [ "$1" = "" ] ; then
 echo "$0 <image location> <image type> [quality] "
 echo "convert a specific folder that contains images to different format and quality"
 echo "[imagetype] - can be either pdf jpg png tif"
 echo "[quality] - from 1 to 100%, default is 100%"
 exit
fi
if [[ $2 =~ jpg|png|pdf|tif ]] ; then
 MODE=1
else
 echo "please select image type from jpg,png,pdf or tif"
 exit
fi
if [ "$3" = "" ]; then
 QUALITY="100"
fi
find $TARGET_LOCATION -maxdepth 1 -type f > /tmp/tmpfile
cat /tmp/tmpfile | \
while read FILENAME; do
# echo "convert '$FILENAME' -quality $QUALITY '$FILENAME.$TARGET_TYPE'"
 BARENAME=$(echo "$FILENAME" | cut -d'.' -f1)
 EXTENSIONNAME=$(echo ${FILENAME: -4} | tr '[A-Z]' '[a-z]')
 if [[ $EXTENSIONNAME =~ jpg|png|pdf|tif ]]; then
 convert "$FILENAME" -quality $QUALITY "$BARENAME.$TARGET_TYPE"
 fi
done
rm -f /tmp/tmpfile
exit 0

 

  

Mount All Available CIFS Shares using GVFS

Here’s a bash script that will automatically mount all available SAMBA shares that has access permission for the current user.   The script composes of two part: (1) The script itself and (2) the user’s credentials.

But current problem is that the user must store the password to access the SAMBA shares in the keyring before the script runs smoothly. It requires the user to access the any of the permitted shares and store the password permanently to the keyring, then add the script to the Startup Applications(System Settings -> Startup Applications)

Autfile is a simple text file contain the following:

username = myuser
password = mypass
domain = localdomain
servername = localserver

Here’s the code:

#!/bin/bash
ARGS="$*"

BASEDIR="/home/rnartos/.scripts/uicsmb"
OPTYPE=""
CMDTYPE=""
FILTERARGS=`echo "$ARGS" | sed 's/ /#/g' | sed 's/-/ -/g'`
AUTHFILE="$BASEDIR/authfile"
NETBASENAME="WinShares"



if [ ! -f $AUTHFILE ]; then
   echo "ERROR: neither default or user-defined authfile not exist."
   exit 0
fi

for CMD in $FILTERARGS; do
   MCMD=`echo $CMD | sed 's/#/ /g'| awk '{print $1}' `

   #umount
   if [ "$MCMD" = "-u" ]; then
      OPTYPE=" -u "
   fi

   #help
   if [ "$CMD" = "-h"  ]; then
     echo "GFVS WinShares - rnartos"
     echo "$0 -u -a <authfile> -h"
     echo "-u umount current shares"
     echo "-a process authentication file"
     echo "-h this help message."
     exit 0
   fi

   #authfile
   if [ "$MCMD" = "-a"  ]; then
     PCMD=`echo $CMD | sed 's/#/ /g'| awk '{print $2}' `
     if [ "$PCMD" = ""  ]; then
        echo "ERROR:  parameters for -a <authfile>"
     else
         if [ -f $PCMD ]; then
             AUTHFILE="$PCMD"
         else
           echo "$PCMD authfile not exist."
           exit 0
         fi
     fi
   fi
done


SERVERNAME=`cat $AUTHFILE | grep servername | sed 's/=//g' | awk '{print $2}'`
DOMAINNAME=`cat $AUTHFILE | grep domain | sed 's/=//g' | awk '{print $2}'`
USERNAME=`cat $AUTHFILE | grep username | sed 's/=//g' | awk '{print $2}'`
PASSWORD=`cat $AUTHFILE | grep password | sed 's/=//g' | awk '{print $2}'`
CHECKMOUNT=`gvfs-mount -l | grep "smb:"`
GETIP=`nmblookup $SERVERNAME | grep -v "query" | awk '{print $1}'`
GETSHARES=`smbclient --debuglevel=0 -A $AUTHFILE -L $SERVERNAME -I $GETIP -g  2>/dev/null | grep "Disk" | sed 's/|/ /g' | awk '{print $2}'`
WINSHARENAME="$HOME/$NETBASENAME/$DOMAINNAME/$SERVERNAME"

if [ ! -d $WINSHARENAME ];  then
   mkdir -p $WINSHARENAME
fi


for NAMES in $GETSHARES; do

   CHECKSHARE=`smbclient //$SERVERNAME/$NAMES -A $AUTHFILE -c '' 2> /dev/null | grep 'DENIED'`

   if [ "x$CHECKSHARE" = "x" ]; then
     if [ "$OPTYPE" = "" ]; then
         if [ "$CHECKMOUNT" = ""  ]; then
           CMDTYPE="mounting"
         fi
     else
        CMDTYPE="umounting"
     fi
     if [ "$CMDTYPE" = "" ]; then
        echo "Shares Already mounted."
        exit 0
     else
        echo "$CMDTYPE $NAMES"
	if [ "$OPTYPE" = "" ]; then
           gvfs-mount $OPTYPE smb://$SERVERNAME/$NAMES 2> /dev/null
           GVFSNAME=`echo $NAMES | tr '[:upper:]' '[:lower:]'`
           ln -sf "$HOME/.gvfs/$GVFSNAME on $SERVERNAME" $WINSHARENAME/$NAMES
        else
           gvfs-mount $OPTYPE smb://$SERVERNAME/$NAMES 2> /dev/null
        fi
     fi
   fi
done

exit 0 

Ubuntu 11.04 Shutdown and Restart Problem with CIFS

UPDATE(07/29/2011): The GRUB thing didn’t work at all, back to basic trapping signal via upstart scripts in /etc/init/dbus.conf.  I tried it before using /etc/init/network-manager.conf but on Ubuntu 10.10, it’s not working anymore. Here’s another test and works for me.

1. Modify the current /etc/init/dbus.conf.

$> sudo vi /etc/init/dbus.conf

2. Add a pre-stop script, which looks like this:

pre-stop script
       trap "TERM signal" TERM
      /bin/umount -a -t cifs -l -f
      trap - TERM
end script 

3. Save the script and have a test.

4. Done.

UPDATE(07/28/2011): Found a better alternative via GRUB, link here. Mainly you just need to modify the  /etc/default/grub. Here’s the details:

1. Edit the /etc/default/grub

$> sudo vi /etc/default/grub

2. Add “reboot=pci” on the GRUB_CMDLINE_LINUX line, it should look something like this:

... GRUB_CMDLINE_LINUX="reboot=pci" ...

3. Update the grub

$> sudo update-grub

 

4. Done

—————

It’s an ancient bug (here), which still exist in Ubuntu 11.04, or even in other distros. When you got a mounted samba shares before the shutdown or restart process, the machine waits for around 10 minutes before it complete the operation. Very troublesome that’s why I tried various workaround but none of them  works with Ubuntu 11.04. Not even the /etc/rc6.d/K*  or the upstart /etc/init configurations, nor the old python script I posted before (here’s the link).

After few considerations, I made a desperate workaround, creating a script that triggers before the /sbin/shutdown, /sbin/reboot and /sbin/restart commands.

1. Rename the current shutdown, reboot and restart commands in /sbin.

#> mv /sbin/shutdown /sbin/shutdown2
#> mv /sbin/reboot /sbin/reboot2
#> mv /sbin/restart /sbin/restart2 

2. Then create scripts with names of the previous commands in /sbin, which contains the following:

#!/bin/sh
umount -t cifs -a -f -l
/sbin/shutdown2 $@
exit 0 

3. Make similar script for reboot and restart command which also points to /sbin/reboot2 and /sbin/restart2.

4.  Until the dbus implementation of stop on deconfiguring-networking comes to Ubuntu 11.04, which I think working with
Ubuntu 11.10 oneiric. I think this is the least workaround that works for me.

5. Done

Running SSS R3 File Generator (R3FileGen) in Linux

Currently, Social Security System (http://www.sss.gov.ph) has this tool called R3 File Generator, which previously a DOS-based program which has been updated now  to java, a good news for non-Windows OS users. But still it’s needs some minor fixes to be able to run it, in my example in Linux.

The Folder structure:

I didn’t alter anything but added two (2) scripts that’s needed to be able to run in in Linux.

1. runSSS.sh, bash script to be able to run the java application.

#!/bin/bash
cd /home/USER/bin/SSS
java -Xmx256M -jar R3*.jar run

2. wordpad, also a bash script which simulate the WORDPAD.EXE, which used in the application to generate on-screen report.

#!/bin/bash
gedit $1 $2 $3

Note: Remember, the files should be located in /home/USER/bin/SSS, update the path to the actual location of the application. JRE should also be installed to run this program.

Toggle USB Flash Disk access on/off on Ubuntu

In a series of test I’ve made on the built-in access permission control regarding the USB Flash Drives in Ubuntu, in all of my test it fails. The “Access External Storage Devices” control in Users & Groups Management, for me is not working (or I haven’t discovered how it really works). Then I just came up with this script:

#!/bin/bash
# Toggle on/off for USB Storage
# by https://hardc0l2e.wordpress.com
# KERNEL=="sd*[!0-9]|sr*", ENV{ID_SERIAL}!="?*", SUBSYSTEMS=="usb", IMPORT{program}="usb_id --export %p"
# ------------------------------

TARGET_NAME="/etc/udev/rules.d/10-disable-usb.rules"
PATTERN='SUBSYSTEMS=="usb"'
CHECK_ENABLED=`grep "$PATTERN" /etc/udev/rules.d/* | grep "OPTIONS"`
ENV_CHECK="no"

if [ "$ENV_CHECK" = "yes" ]; then
 if [ "x$CHECK_ENABLED" = "x" ]; then
 echo 'KERNEL=="sd*[!0-9]|sr*", ENV{ID_SERIAL}!="?*", SUBSYSTEMS=="usb", IMPORT{program}="usb_id --export %p", OPTIONS+="ignore_device"' > $TARGET_NAME
 fi
else
 if [ "x$CHECK_ENABLED" != "x" ]; then
 echo 'KERNEL=="sd*[!0-9]|sr*", ENV{ID_SERIAL}!="?*", SUBSYSTEMS=="usb", IMPORT{program}="usb_id --export %p"' > $TARGET_NAME
 fi
fi

To toggle ON/OFF just replace the variable ENV_CHECK=”yes” to disable and ENV_CHECK=”no” to enable USB Flash Disk access.

Done.

Autorefresh CUPS printers

This cron script will automatically reactive all disabled CUPS printers. In our office it’s a daily problem when a faulty start or  a minor operational problem might deactivate the  said printer/s. This scripts will help you automatically enable all disabled printer Hope this work 100% for your CUPS configuration.

Updates: fixes the LIST_PRINTERS value.(03/16/2010)

#!/bin/bash
# Re-activate all paused or faulty printers
# by https://hardc0l2e.wordpress.com
#==========================================
LIST_PRINTERS=`lpstat -p | grep printer | grep -v enable | awk '{print $2}' | sed '/^$/d'`
if [ "x$LIST_PRINTERS" !=  "x" ]; then
  for printername in $LIST_PRINTERS; do
    echo "Activating $printername ..."
    cupsenable $printername > /dev/null
  done
fi
sleep 1

Reduce image resolution BASH script

One of my problem in System Administration is auto-converting images to a slight lower resolution when its stored in our File Server, Most of our JPGs are gathered from numbers of digital cameras which they only intend to store in their private network folder. A high resolution image consume too much resources.

I also impost a policy which restrict of storing unrelated documents in our File Server, but still some refuse to abide and still storing images in high resolution. So here’s one of my script that might be useful to you in case we are on same scenario.

#!/bin/bash
# JPGCONVERT
#----------------------------
# by http://twitter.com/c0l2e
#    ronartos@gmail.com
#============================
# converts images with resolution higher than 1024x1024 to lower at least 50% or original

LOCATION=$1
TARGET_FILES=`find "$LOCATION" -iname "*.JPG" | sed 's/ /|/g'`

for files in $TARGET_FILES
  do
       #TESTFILE=`echo "$files" | sed 's/|/ /g' | xargs file -b | awk '{print $1}'`
       TEST1=`echo $files | sed 's/|/ /g'`  #$TESTFILE
       TEST2=`file "$TEST1" -b | awk '{print $1}'`
       if [ "$TEST2" = "JPEG" ]; then
            #echo $TEST1
            OLDEXT="jpg"
            NEWEXT="png"
            find "$TEST1" -iname "*.$OLDEXT" |
            while read F
            do
                 NEWFILE="${F/%${OLDEXT}/${NEWEXT}}"
                 #echo "mv \"${F}\" \"${NEWFILE}\""
                 convert "$F" "$NEWFILE"
                 CHECKSIZE=`file "$NEWFILE" -b | sed 's/ //g' | sed 's/,/ /g' | awk  '{print $2}' | sed 's/x/ /g' | awk '{print $1}'`
                 if [ $CHECKSIZE -ge  1024  ]; then
                    convert  "$F" -resize 50% "$F"
                 fi
                 rm "$NEWFILE"
            done
       fi
  done

%d bloggers like this: