LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Python & Bash Code User Expire Scipts (https://www.linuxquestions.org/questions/programming-9/python-and-bash-code-user-expire-scipts-4175708417/)

LinuxRSA 02-22-2022 07:13 AM

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

boughtonp 02-22-2022 07:20 AM


 
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.


shruggy 02-22-2022 07:34 AM

What a horrible way to use Python. Why don't you write it all in Bash?
Quote:

Originally Posted by LinuxRSA (Post 6332076)
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 ]] ...

LinuxRSA 02-22-2022 08:02 AM

1 Attachment(s)
Hi @boughtonp, thanks for the advise, i managed to clean up the main script, results are looking better after following the spellcheck advise.

LinuxRSA 02-22-2022 08:11 AM

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
.


shruggy 02-22-2022 08:23 AM

ShellCheck cannot check Python. You have to extract Bash code from the scripts and feed only it to ShellCheck.

pan64 02-22-2022 08:39 AM

those looks like simple typos for me, although shellcheck cannot check python/awk and other languages:
Code:

$"( should be "$(
$(echo "$LIST | awk  ==> missing closing "


shruggy 02-22-2022 10:44 AM

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.


All times are GMT -5. The time now is 09:56 PM.