Script to alert when Root Password Expiry in AIX - shell

I am trying to build a script in AIX which I am planing to run as cron job.
I want the script to check if the root password will expire with in 10 days and trigger an email. I have written a script to trigger mail but I am not sure how to write a script for password expiry for root.
This is script for sending mail .
#!/bin/sh
sendmail -t -F 'ABC ' -f 'abc#xyz.com' << test.mail
From: ABC <abc#xyz.com>
To: def#xyz.com
Subject:
Password expired in 10 days
This script works fine .
But i want a script for AIX that will check root password expiry within 10 days of expiry date .

you can do it like,
lastupdate=lssec -f /etc/security/passwd -a lastupdate -s <username> | cut -d " " -f2 |cut -d "=" -f2
maxage=lsuser -a maxage itimadm | cut -d " " -f2 |cut -d "=" -f2
maxage=$(($maxage*7))
expires=$(($lastupdate+(60*60*24*$maxage)))
expire_date=perl -le 'print scalar localtime $expires
daysremaining=ceil((($expires - $now) / (60*60*24)) - 1)
echo $username,$maxage,$expire_date,$daysremaining
Though this is not full fledged script but logic is present (improvement is possible :) ) and you can add if clause for checking condition (daysremaining<10) then call your mailing script which will send mail to respective users.

Related

How to check for lowest IDLE time of specific user if logged on more than one terminal?

If a specific user is logged on more than one terminal, how can I check for the line where the IDLE time is the lowest?
I used the following command:
who -u -H | grep 'user1' | tr -s " " | cut -f '1 4' -d " "
And the output is:
user1 13:22
user1 13:50
That means user1 is logged on two terminals, and the second column shows the IDLE time.
I'm working on a shell script and I want to use only the first line because the IDLE time is the lowest.

Curl To Variable Not Working

I am attempting to grab a specific field from a curl request. It works perfectly fine via the shell, but as soon as I try to assign a variable it stops working.
This is the stand-alone command.
curl --silent 'http://alerts.weather.gov/cap/wwaatmget.php?x=MNC131&y=0' | grep -E ' <title>' | awk -F '[<>]' '{print $3}'
This is the small bit from the script. It echos a blank variable.
weatheralert=$(curl --silent 'http://alerts.weather.gov/cap/wwaatmget.php?x=MNC131&y=0' | grep -E ' <title>' | awk -F '[<>]' '{print $3}')
echo "Current Weather Alert: $weatheralert"
Note that there is a tab character exists before <title> tag not 3 or 4 whitespace. So copy paste a tab character or use grep -P '\t<title>' command.
$ weatheralert=$(curl --silent 'http://alerts.weather.gov/cap/wwaatmget.php?x=MNC131&y=0' | grep -P '\t<title>'|awk -F '[<>]' '{print $3}')
$ echo "Current Weather Alert: $weatheralert"
Current Weather Alert: There are no active watches, warnings or advisories

Shell script to run a command and send the commands output as a report in email

I am trying to write a shell script which will run a command to ssh into multiple machines and store the output in a variable and send it as a report via email.Here is what I have in the script as of now:
#!/bin/bash
DcEmitterConn='yinst ssh -H test.out "netstat -a | grep ES | grep 25019 | wc"'
SUBJECT="DC-Connections"
EMAIL="abc#abc.com"
EMAILMESSAGE="report.out"
echo $DcEmitterConn> $EMAILMESSAGE
#send email using /bin/mail
/bin/mail -s "$SUBJECT" "$EMAIL"< $EMAILMESSAGE
After executing the above command in the script it would ask me for a password and then would print the requested output. The problem i am facing in the above script is that I am not able to store the command output in the variable and print it in the email body. Can someone please let me know if I am missing something.
the output would look something like this:
abc#dh1.data.ac2.abc.com's password: (yinst-pw)
40 240 3560
abc#dh2.data.ac2.abc.com's password: (supplied by yinst-pw)
50 300 4450
Thanks in advance!
You should put double quotes around $DcEmitterConn on line 8. And you can avoid the temporary file:
SUBJECT="DC-Connections"
EMAIL="abc#abc.com"
yinst ssh -H test.out "netstat -a | grep ES | grep 25019 | wc" | /bin/mail -s "$SUBJECT" "$EMAIL"

OpenWRT ASH script

I am trying to write an ASH script to run on my OpenWRT router.
I have installed onto it nodogsplash, which displays a login page when you first try to authenticate with the router.
nodogsplash comes with a command line utility which allows you to change the password:
ndsctl password newpassword
So I am trying to write a script which I can setup as a cron job to run once a day to change the password to something new, however I am struggling to get it to output correctly. My script atm:
#!/bin/ash
local randompassLength
local pass
randompassLength=8
pass=</dev/urandom tr -dc A-Za-z0-9 | head -c $randompassLength
ndsctl password "$pass"
When I run this I get the output:
miqM2Ah6Password set to .
Which seems to chuck the password at the start of the echo and set the password to blank.
Any ideas what I am doing wrong here?
You're missing command substitution:
pass=$(</dev/urandom tr -dc A-Za-z0-9 | head -c $randompassLength)
or using backquotes:
pass=`</dev/urandom tr -dc A-Za-z0-9 | head -c $randompassLength`

Bash while loop iterates only once whenever body contains ssh [duplicate]

This question already has answers here:
ssh breaks out of while-loop in bash [duplicate]
(2 answers)
Closed 7 years ago.
I'm reading host information from a text file and pass it to an ssh command:
The text file contains the host, user and password for the ssh command
while read LINE
do
R_USER=$(echo $LINE | cut -d ',' -f 1)
R_HOST=$(echo $LINE | cut -d ',' -f 2)
PY_SCRIPT=$(echo $LINE | cut -d ',' -f 4)
ssh $R_USER#$R_HOST 'touch /home/user/file_name.txt'
done </path_name/file_name
As it turns out the while loop is only executed once even if the host text file contains multiple host information.
When I remove the ssh command the while loop gets executed as much as there are lines in the host information text file.
Not sure why this is so.
Any information on this?
Roland
The default standard input handling of ssh drains the remaining line from the while loop.
To avoid this problem, alter where the problematic command reads standard input from. If no standard input need be passed to the command, read standard input from the special /dev/null device:
while read LINE
do
R_USER=$(echo $LINE | cut -d ',' -f 1)
R_HOST=$(echo $LINE | cut -d ',' -f 2)
PY_SCRIPT=$(echo $LINE | cut -d ',' -f 4)
ssh $R_USER#$R_HOST 'touch /home/user/file_name.txt' < /dev/null
done </path_name/file_name
Or alternatively, try using ssh -n which will prevent ssh from reading from standard input. For instance:
ssh -n $R_USER#$R_HOST 'touch /home/user/file_name.txt'
If the file is white space separated
host1 user password
host2 user password
then a simple read loop:
while read -r Server User Password
do
/usr/bin/ssh -n $User#$Server touch /home/user/file_name.txt
done </path/to/file.list
But you will be prompted for the password. You cannot pass the "Password" to ssh so I'd suggest storing passwordless ssh-keys and placing them on each host for the user you are connecting as. If you are running this command from a script you can ssh as these users on each host by placing your public key in the user's ~/.ssh/authorized_keys (or authorized_keys2) file. If you have the ssh-copy-id command you could do this by:
ssh-copy-id user#hostname
which would copy YOUR ssh-key to their authorized_keys file so you could then ssh as them. This is assuming you have permission but then again you have their password so what permission do you need?

Resources