Wednesday, June 27, 2018

Creating a file with server name and timestamp in python and shell script on Linux server.


Every time when we write a script to automate some task we create the log file to store the required output from the script.

Here I am showing you all how to create a log file with timestamp and server name with python and shell script on Linux servers.  So that you don't need to check in all the logs files when you look for a log of particular server.

Python Script:

root@linuxserver:/root> cat logfile_timestamp.py
#!/usr/bin/python

import os, time
logfile = "logfile_" + os.uname()[1] + "_" + str(time.strftime("%Y%m%d_%H%M%S")) + ".log"
f = open(logfile,'w')
f.close()

Run the script:
root@linuxserver:/root> python logfile_timestamp.py

Check if the log file created with server name and timestamp.
root@linuxserver:/root> ls -ltr logfile*
-rw-r--r-- 1 root root   0 Jun 27 02:45 logfile_linuxserver_20180627_024509.log


Shell Script:
root@linuxserver:/root>  cat create_logfile.sh
#/bin/bash

logfile=logfile_`uname -n`_`date +%Y%m%d_%H%M%S`.log
touch $logfile

Run the script:
root@linuxserver:/root> ./create_logfile.sh


Check if the log file created with server name and timestamp.
root@linuxserver:/root> ls -ltr logfile*
-rw-r--r-- 1 root root   0 Jun 27 02:41 logfile_linuxserver_20180627_024142.log

Thursday, April 26, 2018

List all the Filesystems which are more than 80% used in linux with awk command


Monitoring file system utilization on linux production server is a very important thing.  We can automate the file system utilization monitoring and schedule it in crontab.  So that script will run at the scheduled time and send out the alert messages.

It's always a good idea to have multiple threshold set for file system and we can make sure it won't reach 100% and the file system crashes on the server.

Below are the few examples of monitoring the file system threshold with powerful awk command.

[santhosh@localhost ~]# df -hP | tr -d "%" | sed 1d | awk '$5 >80'
/dev/mapper/vg00-mysql   30G   25G  3.7G  87 /u01
/dev/mapper/vg00-yumrepo  30G   24G  5.8G  81 /yumrepsitory


[santhosh@localhost ~]# df -hP | tr -d "%" | sed 1d | awk '{if($5>80) print}'
/dev/mapper/vg00-mysql   30G   25G  3.7G  87 /u01
/dev/mapper/vg00-yumrepo  30G   24G  5.8G  81 /yumrepsitory


[santhosh@localhost ~]# df -hP | tr -d "%" | sed 1d | awk 'int($5) > 80 { print $0 }'
/dev/mapper/vg00-mysql   30G   25G  3.7G  87 /u01
/dev/mapper/vg00-yumrepo  30G   24G  5.8G  81 /yumrepsitory

Monday, April 16, 2018

How to convert string to Uppercase and Lower case in Linux using awk and tr commands.



Lowercase to Uppercase:

Using awk command.
root@linuxserver:/root> echo santhosh | awk '{ print toupper($0)}'
SANTHOSH

Using tr command.
root@linuxserver:/root> echo santhosh | tr '[a-z]' '[A-Z]'
SANTHOSH


Uppercase to Lowercase:

Using awk command.
root@linuxserver:/root> echo SANTHOSH | awk '{ print tolower($0)}'
santhosh

Using tr command.
root@linuxserver:/root> echo SANTHOSH | tr '[A-Z]' '[a-z]'
santhosh

Monday, January 8, 2018

Bash shell script to list Ethernet interfaces names and IPaddress on linux server.


If there are multiple IP addresses public, private, backup, etc are in use on linux server it takes a bit more of time to scroll down through the output of 'ifconfig' command and find the IP address assigned to it.

I have created a shell script nic.sh which list all the Ethernet interfaces and IP address assigned to them on Linux server.

#!/bin/bash

nic=$(ifconfig | cut -d" " -f1 | sed '/^$/d' | awk -vORS=',' '{ print $1}' | sed -e s/,$//g)

OFS=IFS
IFS=','
read -ra i <<< "$nic"
for i in "${i[@]}"; do
echo "$i ---> $(ifconfig | grep -A 1 -i $i | sed -n '2p' | awk -F" " '{ print $2 }' | cut -d":" -f2)"
done


root@localhost:/root>./nic.sh
eth0 ---> 192.168.1.19
lo ---> 127.0.0.1

Saturday, January 6, 2018

How to set a default tray for a printer in Linux?


When there are multiple trays available in the printer and we want the printer to pick papers from a specific tray we can use the below command.

Syntax:
# lpoptions -p <printer name> -o InputSlot=<TrayN>

Example:
# lpoptions -p billing -o InputSlot=Tray2

Here the printer billing will use Tray2 as the default tray.

You can check it to make sure if the printer billing is using the Tray2.
# lpoptions -p billing -l | grep  InputSlot
  InputSlot/Media Source: Auto Tray1 *Tray2 Tray3 Tray4

Note that the asterisk (*) in front of the Tray2.