Difference between revisions of "TerminalScripts"
(5 intermediate revisions by the same user not shown) | |||
Line 3: | Line 3: | ||
image =Warning.png| | image =Warning.png| | ||
heading =WARNING| | heading =WARNING| | ||
− | message = This is recommended only for advanced users, proceed with caution.}}The scripts below are now included in the [https://www.amahi.org/apps/terminal-scripts Terminal Scripts] | + | message = This is recommended only for advanced users, proceed with caution.}}The scripts below are now included in the Amahi [https://www.amahi.org/apps/terminal-scripts Terminal Scripts] application. You might also find [https://www.amahi.org/apps/shellcheck ShellCheck] helpful when developing scripts. |
Use of script files provides many useful results. Below is a small collection that should enhance your command line experience. | Use of script files provides many useful results. Below is a small collection that should enhance your command line experience. | ||
− | ==Disk Space== | + | == README1ST == |
+ | <b><u>Terminal Scripts Application README FILE</u></b> | ||
+ | |||
+ | The following scripts are included: | ||
+ | * automysqlbackup: Backup multiple MySQL databases on a given schedule. | ||
+ | * diskspace: Displays graphical depiction of all drive space used and remaining. | ||
+ | * dnflog: Shows latest Fedora updates that have been applied. | ||
+ | * dsalert: Sends email if a disk has used a set percentage of space. Requires the system have outgoing email capability. | ||
+ | * folderspace: Displays how much space a specific folder occupies. | ||
+ | * hdaupdates: Shows latest Amahi updates to your HDA. | ||
+ | * smartchk: Provides health status of disks drives set in the script. | ||
+ | * systeminfo: Displays various criteria about the system, to include host name, IP address, uptime, etc. | ||
+ | * weather: Shows weather for a given location as preset in the script. | ||
+ | |||
+ | |||
+ | ALL the above scripts may require some modification for your system. | ||
+ | |||
+ | * systeminfo and weather script has a variable that must be changed for your geographical area. | ||
+ | * smartchk requires modification to list all mounted hard disk drives in your system. | ||
+ | * automysqlbackup has various settings will need adjusted for your system. See the [[https://wiki.amahi.org/index.php/Database_Backup:automysqlbackup]] page for details. | ||
+ | |||
+ | |||
+ | <b><u>Dependencies Installed for Scripts</u></b> | ||
+ | |||
+ | * sysinfo: pciutils and procinfo | ||
+ | * smartchk: smartmontools | ||
+ | * dsalert: smartmontools | ||
+ | |||
+ | |||
+ | <b><u>dos2unix (installed with this application)</u></b> | ||
+ | |||
+ | What is it? | ||
+ | <blockquote>dos2unix is a tool to convert text files from DOS line endings (carriage return + line feed) to Unix line endings (line feed). It is also capable of conversion between UTF-16 to UTF-8. Invoking the unix2dos command can be used to convert from Unix to DOS. This tool comes in handy when sharing files between Windows and Linux machines. </blockquote> | ||
+ | |||
+ | Why would I need this tool? | ||
+ | <blockquote>While editing files on a machine running some form of Windows and uploading them to a Linux server is convenient, it can cause unforeseen complications. Windows-based text editors put special characters at the end of lines to denote a line return or newline. Normally harmless, some applications on a Linux server cannot understand these characters and can cause the service to not respond correctly. There is a simple way to correct this problem: dos2unix.</blockquote> | ||
+ | |||
+ | The dos2unix command is a simple way to make sure that files that have been edited and uploaded from a Windows machine to a Linux machine work and behave correctly. | ||
+ | |||
+ | ===Disk Space=== | ||
<pre>#!/bin/bash | <pre>#!/bin/bash | ||
Black<nowiki>=</nowiki>'\e[0;30m' | Black<nowiki>=</nowiki>'\e[0;30m' | ||
Line 76: | Line 115: | ||
fi</pre> | fi</pre> | ||
− | ==System Info== | + | ===System Info=== |
<pre>#!/bin/sh | <pre>#!/bin/sh | ||
# Variables (server default) | # Variables (server default) | ||
Line 188: | Line 227: | ||
echo</pre> | echo</pre> | ||
− | ==Weather== | + | ===Weather=== |
<pre>#!/bin/sh | <pre>#!/bin/sh | ||
METRIC<nowiki>=</nowiki>0 # 0 for F, 1 for C | METRIC<nowiki>=</nowiki>0 # 0 for F, 1 for C | ||
Line 205: | Line 244: | ||
<nowiki>|</nowiki> sed -n '/Currently:/ s/.*: \(.*\): \([0-9]*\)\([CF]\).*/\2°\3, \1/p'</pre> | <nowiki>|</nowiki> sed -n '/Currently:/ s/.*: \(.*\): \([0-9]*\)\([CF]\).*/\2°\3, \1/p'</pre> | ||
− | == HDA Updates == | + | === HDA Updates === |
Here is a simple script that displays all of the file updates and additions made to your hda and the date they happened. It simply reads the hda-updates.log file and parses out only the relevant changes. | Here is a simple script that displays all of the file updates and additions made to your hda and the date they happened. It simply reads the hda-updates.log file and parses out only the relevant changes. | ||
Line 349: | Line 388: | ||
===============================================================================</pre> | ===============================================================================</pre> | ||
− | == Fedora Updates == | + | === Fedora Updates === |
<pre>#!/usr/bin/bash | <pre>#!/usr/bin/bash | ||
head=' PACKAGE NAME\t\t\t\t\tDATE' | head=' PACKAGE NAME\t\t\t\t\tDATE' | ||
Line 362: | Line 401: | ||
rpm -qa --last | head | rpm -qa --last | head | ||
echo -e ${white}</pre> | echo -e ${white}</pre> | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− |
Latest revision as of 15:37, 17 June 2020
WARNING | |
---|---|
This is recommended only for advanced users, proceed with caution. |
The scripts below are now included in the Amahi Terminal Scripts application. You might also find ShellCheck helpful when developing scripts.
Use of script files provides many useful results. Below is a small collection that should enhance your command line experience.
README1ST
Terminal Scripts Application README FILE
The following scripts are included:
- automysqlbackup: Backup multiple MySQL databases on a given schedule.
- diskspace: Displays graphical depiction of all drive space used and remaining.
- dnflog: Shows latest Fedora updates that have been applied.
- dsalert: Sends email if a disk has used a set percentage of space. Requires the system have outgoing email capability.
- folderspace: Displays how much space a specific folder occupies.
- hdaupdates: Shows latest Amahi updates to your HDA.
- smartchk: Provides health status of disks drives set in the script.
- systeminfo: Displays various criteria about the system, to include host name, IP address, uptime, etc.
- weather: Shows weather for a given location as preset in the script.
ALL the above scripts may require some modification for your system.
- systeminfo and weather script has a variable that must be changed for your geographical area.
- smartchk requires modification to list all mounted hard disk drives in your system.
- automysqlbackup has various settings will need adjusted for your system. See the [[1]] page for details.
Dependencies Installed for Scripts
- sysinfo: pciutils and procinfo
- smartchk: smartmontools
- dsalert: smartmontools
dos2unix (installed with this application)
What is it?
dos2unix is a tool to convert text files from DOS line endings (carriage return + line feed) to Unix line endings (line feed). It is also capable of conversion between UTF-16 to UTF-8. Invoking the unix2dos command can be used to convert from Unix to DOS. This tool comes in handy when sharing files between Windows and Linux machines.
Why would I need this tool?
While editing files on a machine running some form of Windows and uploading them to a Linux server is convenient, it can cause unforeseen complications. Windows-based text editors put special characters at the end of lines to denote a line return or newline. Normally harmless, some applications on a Linux server cannot understand these characters and can cause the service to not respond correctly. There is a simple way to correct this problem: dos2unix.
The dos2unix command is a simple way to make sure that files that have been edited and uploaded from a Windows machine to a Linux machine work and behave correctly.
Disk Space
#!/bin/bash Black='\e[0;30m' Blue='\e[0;34m' Green='\e[0;32m' Cyan='\e[0;36m' Red='\e[0;31m' Purple='\e[0;35m' Brown='\e[0;33m' Light_Gray='\e[0;37m' Dark_Gray='\e[1;30m' Light_Blue='\e[1;34m' Light_Green='\e[1;32m' Light_Cyan='\e[1;36m' Light_Red='\e[1;31m' Light_Purple='\e[1;35m' Yellow='\e[1;33m' White='\e[1;37m' Blink='\033[5m' Bold='\033[1m' Underline='\033[4m' No_Color='\033[0m' TermSize=`tput cols` if [ $TermSize -gt "81" ]; then TermSize=81 fi function Print { # Disk I don't care about. if [ $DISK = "none" ] || [ $DISK = "/dev/sda3" ] || [ $DISK = "/dev/pts" ] || [ $DISK = "/dev/shm" ] || [[ $DISK =~ /dev/sr.* ]]; then continue fi read SIZE USED FREE PERCENT <<< $(df -hP 2> /dev/null|grep $DISK|awk '{print $2" "$3" "$4" "$5}') PERCENT=$(echo $PERCENT|sed s/%//) BarSize=$(echo "($PERCENT*($TermSize-3))/100"|bc) if [ "$PERCENT" -gt "90" ]; then Color=$Light_Red elif [ "$PERCENT" -gt "70" ]; then Color=$Yellow else Color=$Light_Green fi echo "Partition: $DISK" #echo echo -ne "Total size: $SIZE | " echo -ne "Used space: $USED | " echo -ne "Free space: ${Color}$FREE${No_Color} | " echo -ne "Used space percent: $PERCENT%" echo echo -ne "[" for i in `seq 1 $BarSize`; do echo -ne "${Color}#" echo -ne "${No_Color}" done for i in `seq 1 $(echo "$TermSize-$BarSize-3"|bc)`; do echo -ne "=" done echo "]" echo -ne "${No_Color}" echo } echo if [ "$1" != "" ]; then DISK=$1 Print else for DISK in $(mount 2> /dev/null | grep "^/dev/" | awk '{print $1}'); do Print done fi
System Info
#!/bin/sh # Variables (server default) TEMP="0" # 0 for Farenheit, 1 for Celcius LOCCOD="95110" # Find your own LOCCOD (location code) at the end of # the url here: http://www.accuweather.com/rss-center.asp # If code has a space; replace with %20 # # Variables HOSTNAME=`hostname -f` IP_ADDRS=`ifconfig | grep 'inet addr' | grep -v '255.0.0.0' | cut -f2 -d':' | awk '{print $1}'` IP_ADDRS=`echo $IP_ADDRS | sed 's/\n//g'` CPUS=`cat /proc/cpuinfo | grep processor | wc -l | awk '{print $1}'` CPU_MHZ=`cat /proc/cpuinfo | grep MHz | tail -n1 | awk '{print $4}'` CPU_TYPE=`cat /proc/cpuinfo | grep vendor_id | tail -n 1 | awk '{print $3}'` CPU_TYPE2=`uname -m` OS_NAME=`uname -s` OS_KERNEL=`uname -r` BOOT=`procinfo | grep Bootup | sed 's/Bootup: //g' | cut -f1-6 -d' '` UPTIME=`uptime |cut -d , -f 1 | cut -c 2-` PCIINFO=`lspci | cut -f3 -d':'` # Read and process .motd.conf file and set variables if [ -s "$HOME/.motd.conf" ] ; then file=`cat "$HOME/.motd.conf" | grep -Ev '^#' | grep -Ev '^$'` motd=`echo "$file" | grep -Ei '^motd=.+' | sed -e 's/motd=//i' -re 's/.*(0).*/\1/'` if [ "$motd" == "0" ] ; then process="0" usage="0" weather="0" else process=`echo "$file" | grep -Ei '^process=.+' | sed -e 's/process=//i' -re 's/.*(0).*/\1/'` usage= `echo "$file" | grep -Ei '^disk usage=.+' | sed -e 's/disk usage=//i' -re 's/.*(0).*/\1/'` weather=`echo "$file" | grep -Ei '^weather=.+' | sed -e 's/weather=//i' -re 's/.*(0).*/\1/'` TEMPu=` echo "$file" | grep -Ei '^TEMP=.+' | sed -e 's/TEMP=//i' -re 's/.*([01]).*/\1/'` LOCCODu=`echo "$file" | grep -Ei '^LOCCOD=.+' | sed -e 's/LOCCOD=//i' -re 's/["?](.+)["?]/\1/'` if [ $TEMPu != "" ] ; then TEMP="$TEMPu" fi if [ $LOCCODu != "" ] ; then LOCCOD="$LOCCODu" fi fi fi #print it out echo echo "\033[0;36mHostname : \033[0;37m$HOSTNAME" echo "\033[0;36mHost Address(es) : \033[0;37m$IP_ADDRS" echo "\033[0;36mOS Name : \033[0;37m$OS_NAME" echo "\033[0;36mKernel Version : \033[0;37m$OS_KERNEL" echo "\033[0;36mCPU Type : \033[0;37m$CPU_TYPE $CPU_TYPE2 $CPU_MHZ MHz" echo "\033[0;36mNumber of CPUs : \033[0;37m$CPUS" # Display Memory Stats if [ "$memory" != "0" ] ; then memory=`free -m` echo "\033[0;36mMemory MB........: \033[0;37m`echo "$memory" | grep 'Mem: ' \ | awk '{print "Ram used: "$3" free: "$4}'` `echo "$memory" | grep 'Swap: ' \ | awk '{print "Swap used: "$3" free: "$4}'`" fi # Display Usage of Home Directory if [ "$usage" != "0" ] ; then du -sh $HOME | awk '{print "\033[0;36mDisk Usage.......: \033[0;37mYou\47re using "$1"B in "$2}' fi # Display Processes if [ "$process" != "0" ] ; then # (The 2 at end of the next two lines can be used to subtract out any 'offset') psa=$((`ps -A h | wc -l`-2)) psu=$((`ps U $USER h | wc -l`-2)) verb="are" if [ "$psu" -lt "2" ] ; then if [ "$psu" -eq "0" ] ; then psu=None else verb="is" fi fi echo "\033[0;36mProcesses........: \033[0;37m$psu of the $psa running $verb yours." echo fi # Display SSH Logins; System Uptime; System Load if [ "$LogLoad" != "0" ] ; then LogLoad=`uptime` echo "\033[0;36mBootup : \033[0;37m$BOOT" # Display System Uptime echo $LogLoad | grep -Eo 'up .+ user' \ | sed -e 's/:/ hours /' -e 's/ min//' -re 's/^up\s+/\x1b[0;36mUptime...........:\x1b[0;37m /' \ | sed -re 's/,\s+[0-9]+ user$/ minutes/' -e 's/,//g' -e 's/00 minutes//' \ | sed -re 's/0([1-9] minutes)/\1/' -e 's/(1 hour)s/\1/' -e 's/(1 minute)s/\1/' # Display System Load echo $LogLoad | grep -Eo 'average: .+' \ | sed -e 's/average:/\x1b[0;36mLoad.............:\x1b[0;37m/' -e 's/,//g' \ | awk '{print $1, $2" (1 minute) "$3" (5 minutes) "$4" (15 minutes)"}' # Display SSH Logins echo $LogLoad | grep -Eo '[0-9]+ users?' \ | awk '{print "\033[0;36mSSH Logins.......: \033[0;37m"$1, $2" currently logged in."}' fi # last login lastlog -u $USER | tail -1 | awk '{print "\033[0;36mLast Login.......: \033[0;37m"$4, $5, $6, $7" from "$3}' # Display Weather if [ "$weather" != "0" ] ; then weather=`curl -s http://rss.accuweather.com/rss/liveweather_rss.asp\?TEMP\=${TEMP}\&locCode\=$LOCCOD \ | grep -E '<description>(Currently|High)' \ | sed -e 's/.*<description>\(.*\)/\1/' -e 's/\(.*\) <.*/\1/' -e 's/\(°\)//'` if [ "`echo "$weather" | wc -l`" -eq "3" ] ; then echo echo "\033[0;35mWeather..........: \033[0;37m`echo "$weather" | head -1`" echo "\033[0;35mToday............: \033[0;37m`echo "$weather" | head -2 | tail -1`" echo "\033[0;35mTomorrow.........: \033[0;37m`echo "$weather" | tail -1`" fi fi echo
Weather
#!/bin/sh METRIC=0 # 0 for F, 1 for C # Find your own code at the end of the url here http://www.accuweather.com/rss-center.asp # If code has a space; replace with %20 LOCCOD="95113" #Example: ASI|RU|RS055|NIZHNY%20NOVGOROD if [ -z $1 ] && [ -x $LOCCOD ] ; then echo echo "USAGE: $0 [locationcode]" echo exit 0; elif [ ! -z $1 ] ; then LOCCOD=$1 fi curl -s http://rss.accuweather.com/rss/liveweather_rss.asp\?metric\=${METRIC}\&locCode\=$LOCCOD \ | sed -n '/Currently:/ s/.*: \(.*\): \([0-9]*\)\([CF]\).*/\2°\3, \1/p'
HDA Updates
Here is a simple script that displays all of the file updates and additions made to your hda and the date they happened. It simply reads the hda-updates.log file and parses out only the relevant changes.
I use this script periodically so I can see what might have changed recently.
Installation
Log into your hda and create a bin directory under your home directory (if you don't already have one). The reason for this is that ~/bin is already in your $PATH when you log in, so it will automatically find any scripts you put there:
cd /usr/bin
Open up your favorite editor (I suggest gedit for this) and copy the code that follows into the editor. Save the file as hda-updates (or whatever you want to name it) and exit the editor.
Code:
#!/usr/bin/python # # Dump update information from /var/log/hda-updates.log #!/usr/bin/python # # Display the date and files that were modified/installed. # # Reads the hda-updates log file and parses out just the file updates/additions. # It helps to see what has been modified and when. # # radioz # 11/8/2010 # # Updated by bigfoot65 # 9/26/2014 # log = '/var/log/hda-updates.log' line = '===============================================================================' head = 'Package Arch Version Repository Size' cyan = '\033[1;36m' blue = '\033[1;34m' purple = '\033[1;35m' white = '\033[1;37m' print blue + line + white print cyan + head + white print blue + line + white f = open(log, 'r') lines = f.readlines() f.close() gotUpdates = False firstUpdate = False dateInfo = '' for ll in lines : l = ll.strip() # Ignore dependency heading if l == 'Installing for dependencies:' : continue if l == '' : # A blank line terminates the updates for a particular date if gotUpdates : gotUpdates = False continue if gotUpdates : if firstUpdate : # Display the date above the first update print '%s:' % dateInfo firstUpdate = False # Reformat the file info slightly updateInfo = l.split() print purple + l + white print blue + line + white if 'hda update attempt' in l : # This header appears once per hour in the log. Very few represent real updates. # Save the date information in case this is a real update. dateInfo = ' '.join(l.split()[5:]) if 'Updating:' in l : # This is a real update, set up the flags gotUpdates = True firstUpdate = True
Make the file executable:
chmod +x hda-updates
Now you can run it.
Example
hda-updates
Here is a snippet of its output on my system:
=============================================================================== Package Arch Version Repository Size =============================================================================== Fri Sep 5 21:01:37 CDT 2014: amahi-anywhere x86_64 1.35-1 amahi 1.8 M =============================================================================== Mon Sep 15 23:03:04 CDT 2014: hda-ctl x86_64 5.4.0-1 amahi 154 k ===============================================================================
Fedora Updates
#!/usr/bin/bash head=' PACKAGE NAME\t\t\t\t\tDATE' line='============================================= ===============================' cyan='\033[1;36m' blue='\033[1;34m' purple='\033[1;35m' white='\033[1;37m' echo -e ${blue}${line}${white} echo -e ${purple}${head}${white} echo -e ${blue}${line}${cyan} rpm -qa --last | head echo -e ${white}