LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-22-2022, 07:13 AM   #1
LinuxRSA
Member
 
Registered: Apr 2015
Location: South Africa
Posts: 71

Rep: Reputation: Disabled
Python & Bash Code User Expire Scipts


Hi

Im new to Python coding.

The below code and works when executing via our program.

Code:
#**************************************#
#***   INSERT SHELL COMMANDS HERE   ***#
#**************************************# 
    command = "DATE_TODAY=$(date '+%Y-%m-%d'); EXPIRED_DATE=$(cat /home/Admin/scriptuser_expiry.txt); DATE_TODAY_SEC=$(date --date $DATE_TODAY +'%s'); EXPIRED_DATE_SEC=$(date --date $EXPIRED_DATE +'%s'); DAYS_EXPIRED=$(expr $EXPIRED_DATE_SEC - 2592000); if [ $DATE_TODAY_SEC -ge $DAYS_EXPIRED ]; then echo 'EXPIRED' ; else echo 'HEALTHY' ; fi"
    RESULTS['scriptuser_expired']=[(0,ssh_shell_request(conn,command))]                             
    
    conn.close()
    result_handler.update(RESULTS)

For some reason the below code returns with no data, im not sure if its written correct.

Code:
#**************************************#
#***   INSERT SHELL COMMANDS HERE   ***#
#**************************************# 
    command = "LOG='/home/Admin/user_expiry.txt' ; while read LIST ; do DATE=$(echo $LIST | awk '{print $3}') ; CHECK=$(echo $DATE | grep -v \":\") ; if [[ ! -z '$CHECK' ]]; then if [[ \'$CHECK' != 'HEALTHY' ]] ; then if [[ $DATE -le 10 ]] ; then echo 'EXPIRED' ; else echo 'HEALTHY' ; fi ; else echo 'HEALTHY' ; fi ; else echo 'EXPIRED' ; fi ; done < $LOG"
    RESULTS['Scriptuser_Expired_Critical']=[(0,ssh_shell_request(conn,command))]                      
    
    conn.close()
    result_handler.update(RESULTS)
Please advise what is wrong in the second code and how to fix ?

Both these checks calls on the below main bash script:

Code:
user@server:/etc/cron.d$ cat /usr/local/bin/expiry-check.sh
#!/bin/bash
LOG='/home/Admin/user_expiry.txt'
LOG2='/home/Admin/temp_user_expiry.txt'
CLEANLOG='/home/Admin/all_user_expiry.txt'
touch $LOG
touch $LOG2
touch $CLEANLOG
CONTAINER_ID=$(docker ps -a | grep -i "postgresql" | awk '{print $1}')

if [ ! -z "$CONTAINER_ID" ]
then
          EXPIRE_DATE=$(docker exec -i $CONTAINER_ID psql -h localhost -p 5432 -U user -c "select to_timestamp(c.created_date/1000 + 90*24*60*60)::date from credential c join user_entity u on u.id=c.user_id where u.email='support@
company.ai' and c.type='password';" | grep -B 1 "1 row" | head -1)
            echo $EXPIRE_DATE > $LOG

              ALL_EXPIRE_DATE=$(docker exec -i $CONTAINER_ID psql -h localhost -p 5432 -U user -c "select u.email, now() - to_timestamp(c.created_date/1000)::date password_age from credential c join user_entity u on u.id=c.user_id
 where now() - to_timestamp(c.created_date/1000)::date > interval '0 days' and c.type='password'" | grep -v "-" | grep -A 5000 -v "email" | grep -v "rows")
                echo $ALL_EXPIRE_DATE > $LOG2
        else
                  echo "HEALTHY" > $LOG
                    echo "HEALTHY HEALTHY HEALTHY" > $LOG2
fi

sed 's/\r$//' $LOG2 > $CLEANLOG
sed -i '/^$/d' $CLEANLOG
user@server::/etc/cron.d$
Thanks

Last edited by LinuxRSA; 02-22-2022 at 07:17 AM.
 
Old 02-22-2022, 07:20 AM   #2
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,616

Rep: Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555

ssh_shell_request is not a built-in function for Python, and you've not shared the code, so nobody here knows how you're executing those two scripts - my guess is they are NOT being executed as Bash scripts but you're using Bash-specific features.

I haven't actually looked at either script to determine this - too much effort with them formatted as a single line. Python (like Bash) supports multiline strings if you delimit them with """ - you should be making use of that functionality.

(Similarly, the formatting of your Bash script is all over the place - such an easy thing to fix and makes a huge difference to readability.)

Once you've fixed the formatting, running the scripts through ShellCheck will almost certainly highlight the issues.


Last edited by boughtonp; 02-22-2022 at 07:24 AM.
 
Old 02-22-2022, 07:34 AM   #3
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,677

Rep: Reputation: Disabled
What a horrible way to use Python. Why don't you write it all in Bash?
Quote:
Originally Posted by LinuxRSA View Post
For some reason the below code returns with no data, im not sure if its written correct.
It isn't. You escaped a quote on the left, but not on the right
Quote:
Code:
... if [[ \'$CHECK' != 'HEALTHY' ]] ...
As it is, ' != ' is parsed as a string by Bash. Escaping a quote or quoting anything doesn't make sense there at all. Probably should have been
Code:
... if [[ $CHECK != 'HEALTHY' ]] ...
Similarly, before that
Quote:
Code:
... if [[ ! -z '$CHECK' ]] ...
probably should have been
Code:
... if [[ $CHECK ]] ...

Last edited by shruggy; 02-22-2022 at 08:18 AM.
 
1 members found this post helpful.
Old 02-22-2022, 08:02 AM   #4
LinuxRSA
Member
 
Registered: Apr 2015
Location: South Africa
Posts: 71

Original Poster
Rep: Reputation: Disabled
Hi @boughtonp, thanks for the advise, i managed to clean up the main script, results are looking better after following the spellcheck advise.
Attached Thumbnails
Click image for larger version

Name:	expiry.PNG
Views:	9
Size:	54.8 KB
ID:	38438  
 
Old 02-22-2022, 08:11 AM   #5
LinuxRSA
Member
 
Registered: Apr 2015
Location: South Africa
Posts: 71

Original Poster
Rep: Reputation: Disabled
shruggy & boughtonp

Thanks Guys, main script is already executing without errors, How do i fix the below ones ?

Code:
$ shellcheck myscript
 
Line 2:
    command="DATE_TODAY=$"(date +%Y-%m-%d); EXPIRED_DATE=$(cat /home/Admin/user_expiry.txt); DATE_TODAY_SEC=$(date --date $DATE_TODAY +'%s); EXPIRED_DATE_SEC=$(date --date "$EXPIRED_DATE +'%s'); DAYS_EXPIRED=$(expr $EXPIRED_DATE_SEC - 864000); if [ $DATE_TODAY_SEC -ge $DAYS_EXPIRED ]; then echo 'EXPIRED' ; else echo 'HEALTHY' ; fi"
    ^-- SC2034 (warning): command appears unused. Verify use (or export if used externally).
                          ^-- SC1036 (error): '(' is invalid here. Did you forget to escape it?
                          ^-- SC1088 (error): Parsing stopped here. Invalid use of parentheses?


Code:
$ shellcheck myscript
 
Line 2:
command=LOG='/home/Admin/user_expiry.txt' ; while read LIST ; do DATE=$(echo "$LIST | awk '{print $3}') ; CHECK=$(echo $DATE | grep -v \":\") ; if [[ ! -z '$CHECK' ]]; then if [[ \"$CHECK\" != \"HEALTHY\" ]] ; then if [[ $DATE -le 10 ]] ; then echo \"EXPIRED\" ; else echo \"HEALTHY\" ; fi ; else echo \"HEALTHY\" ; fi ; else echo \"EXPIRED\" ; fi ; done < $LOG"
>>                                                                          ^-- SC1009 (info): The mentioned syntax error was in this variable assignment.
>>                                                                               ^-- SC1073 (error): Couldn't parse this command expansion. Fix to allow more checks.
 
Line 3:
    RESULTS['Scriptuser_expired_critical']=[(0,ssh_shell_request(conn,command))]                      
                                          ^-- SC1036 (error): '(' is invalid here. Did you forget to escape it?
                                          ^-- SC1072 (error): Expected end of $(..) expression. Fix any mentioned problems and try again.

Last edited by LinuxRSA; 02-22-2022 at 08:26 AM.
 
Old 02-22-2022, 08:23 AM   #6
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,677

Rep: Reputation: Disabled
ShellCheck cannot check Python. You have to extract Bash code from the scripts and feed only it to ShellCheck.
 
1 members found this post helpful.
Old 02-22-2022, 08:39 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,986

Rep: Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337
those looks like simple typos for me, although shellcheck cannot check python/awk and other languages:
Code:
$"( should be "$(
$(echo "$LIST | awk   ==> missing closing "
 
Old 02-22-2022, 10:44 AM   #8
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,677

Rep: Reputation: Disabled
Also, the log file name in your second Python script may be wrong. Judging by your main Bash script, you're generating two log files: 1) user_expiry.txt which only contains one value, password expiration date for a user, and 2) all_user_expiry.txt that contains two columns of data, user email and password age, for multiple users.

I guess that in your second Python script, you'd want to evaluate all_user_expiry.txt rather than user_expiry.txt. That said,
Code:
if [[ $DATE -le 10 ]]; then echo 'EXPIRED';...
doesn't make sense in either case.

The main script makes a strange impression. There's a striking contrast between a competent use of SQL and a helpless Bash code in the same script. Almost as if those parts were written by different persons.

Last edited by shruggy; 02-22-2022 at 10:56 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Ping died, scipts(with HTTP::Request) stoped.. help alex1000 Linux - Networking 5 12-02-2009 12:59 PM
Howto enable php scipts to run only for certain website on homeserver firedancer Linux - Server 12 11-09-2007 12:07 PM
rc.local and scipts - command not found simonmorley Slackware 3 07-04-2007 03:31 AM
Gotta love those &#1649;&#1649;&#1649;&#1649;&#1649;&#1649;&#1649;&# iLLuSionZ Linux - General 5 11-18-2003 07:14 AM
users/passwords scipts Anthony Programming 1 03-27-2002 07:40 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:03 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration