Custom Splash Screen on Login RHEL - linux

I was wondering if it was possible to display a splash screen when you log into an account in a RHEL server? not via /etc/motd as that is global but to an specific user.
Ideally I would like to show uptime and services running when a service account is logged in.
Kind regards,
R,

Check manual page for sshd_config. There is Banner option and you can customize it per-user.
Also motd can be dynamic. Check this answer on ServerFault:
However, it's possible to execute a shell script at login time that will have the same result. This is usually achieved by adapting the /etc/profile script that is executed each time a user logs in. A useful practice is to put the command you want to be executed in a script named /etc/motd.sh and call this script from /etc/profile, usually at about the end of it.

You can modify the target user's .Bash_profile and put inside of it the commands to show stuffs like cpuinfo meminfo...
Here's an example.
let upSeconds="$(/usr/bin/cut -d. -f1 /proc/uptime)"
let secs=$((${upSeconds}%60))
let mins=$((${upSeconds}/60%60))
let hours=$((${upSeconds}/3600%24))
let days=$((${upSeconds}/86400))
UPTIME=`printf "%d days, %02dh%02dm%02ds" "$days" "$hours" "$mins" "$secs"`
echo "`date +"%A, %e %B %Y, %r"`
`uname -srmo`
Uptime.............: ${UPTIME}
Memory.............: `cat /proc/meminfo | grep MemFree | awk {'print $2'}`kB (Free) / `cat /proc/meminfo | grep MemTotal | awk {'print $2'}`kB (Total)
Load Averages......: ${one}, ${five}, ${fifteen} (1, 5, 15 min)
Running Processes..: `ps ax | wc -l | tr -d " "`
IP Addresses.......: `/sbin/ifconfig eth0 | /bin/grep "inet addr" | /usr/bin/cut -d ":" -f 2 | /usr/bin/cut -d " " -f 1`"
this example will show you something like this:
Friday, 15 April 2016, 04:47:41 PM
Linux 2.6.18-128.el5 x86_64 GNU/Linux
Uptime.............: 2 days, 02h05m06s
Memory.............: 1805240kB (Free) / 4037732kB (Total)
Load Averages......: 0.77, 0.74, 0.89 (1, 5, 15 min)
Running Processes..: 230
IP Addresses.......: X.X.X.X
ENJOY!

Related

How to get the memory and cpu usage of a remote server?

My intent is to log into several servers and print out their memory & cpu usage one by one. I wrote the follow scripts
START=1
END=5
for i in {$START..$END}
do
echo "myserver$i"
ssh myserver$i
free -m | awk 'NR==2{printf "Memory Usage: %s/%sMB (%.2f%%)\n", $3,$2,$3*100/$2 }'
top -bn1 | grep load | awk '{printf "CPU Load: %.2f\n", $(NF-2)}'
logout
done
But it doesn't work. Who can give a solution to this? Thanks a lot!
Look carefully at your code.
After the SSH command, you are on the remote server, in an SSH shell. And obviously your script now wants you to talk (via keyboard) to the remote server. When it is finished, e.g. if you hit ctrl-c or ctrl-d, then the next commands like "free" and "top" are running on your local machine.
You have to tell ssh with a kind of "-exec" argument that it should execute free and top on the remote server :D
I'm sure you figure it out yourself how to do that, have fun.
There is one useful command for CPU/mem usage - top.
To get the result, run this command.
CPU Usage - top -b -n 1 | grep Cpu
Mem Usage - top -b -n 1 | grep 'KiB Mem'
After searching online and combining a few answers from other questions on stackflow. I get the following solution.
Solution
On your local computer, you might want to have the following bash script, named, say, usage_ssh
START=1
END=3
date
for i in $(seq $START $END)
do
printf '=%.0s' {1..50};
printf '\n'
echo myservery$i
ssh myserver$i -o LogLevel=QUIET -t "~/bin/usage"
done
printf '=%.0s' {1..50};
printf '\n'
printf 'CPU Load: \n'
printf 'First Field\tprocesses per processor\n'
printf 'Second Filed\tidling percentage in last 5 minutes\n'
printf '\n'
printf '\n'
On your remote server, you should have the following bash script named usage. This script should be located in ~/bin.
free -m | awk 'NR==2{printf "Memory Usage\t%s/%sMB\t\t%.2f%\n", $3, $2, $3/$2*100}';
top -n 1 | grep load | awk '{printf "CPU Load\t%.2f\t\t\t%.2f\n", $(NF-2), $(NF-1)}';
Explanation
The idea is that You will call the use ssh -t <your command> to run executable on your remote file and get the output on the screen of your local computer.
Output
Sat Mar 28 10:32:34 CDT 2020
==================================================
myserver1
Memory Usage 47418/48254MB 98.27%
CPU Load 0.01 0.02
==================================================
myserver2
Memory Usage 47421/48254MB 98.27%
CPU Load 0.01 0.02
==================================================
myserver3
Memory Usage 4300/84541MB 5.09%
CPU Load 0.02 0.02
==================================================
CPU Load:
First Field processes per processor
Second Filed idling percentage in last 5 minutes

Linux finding the mean of the memory usage of last hour

i am trying to write a script that finds the mean of the memory usage of last hour and if it's above %60, mails to someone thats relevant.
I am trying this for days and i am completely lost. On the other hand i can't get any updates for my Ubuntu so i can't try someting like atop. I need this to work on other computers as well.
As far as i know ;
free -m | awk 'NR==2{printf "Memory Usage: %s/%sMB (%.2f%%)\n", $3,$2,$3*100/$2 }'
I am trying to use something like this in my code. Any help would appreciated.
Thanks.
EDIT
So i've built my scripts basics. But in this script i am getting the current ram usage.
#!/bin/sh
used=$(free -m | grep '^Mem' | awk '{print $3}')
total=$(free -m | grep '^Mem' | awk '{print $2}')
perct=$((($used*100)/$total))
echo "$perct%"
if [ $perct -gt 60 ] ; then
echo "Ram usage: $perct is above 60%" | mail -s "Critical Ram Usage" "example#example.com"
fi
#end
From this point , what can i do to improve my code ?

Junk characters being printed with tee command

Script I'm utilizing is below:
#!/bin/bash
lla=$(top -n 1 | grep "load average" | awk '{print $13,$14,$15}')
mem_usage=$(free | grep Mem | awk '{print $3/$2 * 100.0}')
now=`date`
cur_time=$(echo $now | awk '{print $4}')
for i in {1..60}
do
echo "System Performance Statistics at: "$cur_time | tee -a hp.txt
echo "Linux Load Average: "$lla | tee -a hp.txt
echo "Memory Usage: "$mem_usage | tee -a hp.txt
echo "" | tee -a hp.txt
sleep 3
done
Results:
System Performance Statistics at: 19:00:29
Linux Load Average: 0.13, 0.11, 0.14^[(B^[[m^[[39;49m^[[K
Memory Usage: 82.7672
Standard out isn't showing any garbage characters at all. What do I need to change?
Added -b to top command and it works.
From man top:
-b :Batch-mode operation
Starts top in Batch mode, which could be useful for sending output
from top to other programs or to a file. In this mode, top will not
accept input and runs until the iterations limit you've set with the
`-n' command-line option or until killed.

How to select vendor when using macchanger in linux

Currently, I am using this command to spoof my MAC address:
macchanger eth0 -A
However, I want to be able to select which vendor I use. I know that the switch -l displays a list of all available vendors, but say I want to use one from Cisco, what command do I use to actually use that specific vendor rather than the system choosing a random vendor for me each time?
You could add a function like this to .bashrc
Then you can: macspoof Apple eth0
macspoof () {
Vendor=$(macchanger -l | grep $1 | shuf -n 1 | awk '{print $3}')
Tail=$(echo $RANDOM | md5sum | sed 's/.\{2\}/&:/g' | cut -c 1-8)
Mac="$Vendor:$Tail"
sudo macchanger -m $Mac $2
}

Detect inactivity from a scheduled task on Linux

I need to detect user inactivity on my Linux system, to poweroff it (quite headless wife, and quite expensive electric bills... :-).
I need to schedule the script (in crontab), so no X-depending tool will work, I suppose (no $DISPLAY available).
Any thoughts?
UPDATE
For "user inactivity" I mean user input inactivity (mouse and keyboard).
Xautolock may the right tool for you. It allows you to specify a amount of minutes of inactivity after which a command should get triggered.
You might consider checking how long the screen saver has been running.
#!/bin/bash
screensaver="atlantis"
t=$(
# check for the screensaver
ps h -o start -C $screensaver |\
# hh:mm:ss -> seconds
awk -F: '{print $1"*3600+"$2"*60+"$3}' |\
bc -l 2>/dev/null | sort -n | tail -1
)
if [ "$t" == "" ]
then
exit 0
fi
n=$(
date "+%T" |\
awk -F: '{print $1"*3600+"$2"*60+"$3}' |\
bc -l 2>/dev/null
)
runtime=$(( $n - $t ))
if [ $runtime -gt 3600 ] || [ $runtime -lt 0 ]
then
echo shutdown -h now
fi
Using the time value requires subtracting now from then to get the run time.
Also, in my case, the screensaver program which appears in the process table will vary depending on which screensaver is selected. So, the above program assumes that 'atlantis' is the current screen saver.

Resources