Results 1 to 8 of 8

Thread: Shell scripting     submit to reddit submit to twitter

  1. #1
    Rainbow Dash was here,
    Applejack is a silly filly.

    Join Date
    Jan 2008
    Posts
    1,425
    BG Level
    6

    Shell scripting

    I don't have any immediate questions, but I wanted to post this as an open discussion for thoughts and ideas. More simple or maybe even more complex ways of what I'm doing would be appreciated to see how things work.

    I'm trying to create a script thats purpose is to create a series of daily logs of failed login attempts so that you can see on any single day how many failed login attempts any user has had. This should be useful for seeing trends in failed login attempts and if you might have any hacking attempts, or some bullshit reason like that.

    I wrote this pseudocode to sort of help actualize my idea:
    Code:
     Check if last two lines of /etc/pam.d/system-auth
                      If last two lines are right then proceed
                      If last two lines are not right then write appropriately
      Check if logs folder exists
                    If no then – create
                    If yes then proceed
      Get date 12/31/2009
      Check if day exists already
                      If no then – create
                      If yes then proceed
      Append faillog –a to file
      Faillog –r to reset the faillog

  2. #2
    Rainbow Dash was here,
    Applejack is a silly filly.

    Join Date
    Jan 2008
    Posts
    1,425
    BG Level
    6

    So I'm trying to complete the first section at the moment, check if the last two lines of the file matches a certain string to verify that failloging is enabled.

    I'm testing this with the following enviroment; I have a file named test with four lines:
    Code:
    line 1
    line 2
    line 3
    line 4
    After a lot of playing around I got this bit of code to work:
    Code:
    if [ "`tail -2 test`" != "`echo -e "line 3\nline 4"`" ] 
    then
         echo -e "Requirements not found\nAppending..."
         echo -e "line 3\nline 4" >> test
         echo "Next Step"
    else
         echo -e "Requirements found\nNext Step"
    fi
    It tests to see if the last two lines of file test are NOT "line 3\nline 4", if it is does NOT match then it adds "line 3\nline 4" as well as some other stuff just so I can see what the script is doing. If it DOES match then it moves onto the next step, which I think I'll start on tomorrow.

  3. #3
    Rainbow Dash was here,
    Applejack is a silly filly.

    Join Date
    Jan 2008
    Posts
    1,425
    BG Level
    6

    This probably looks very very basic to some of you out there, but it's been a few years since I've programmed in any language and not only am I learning a new one (sort of, it's pretty much just C, as far as I know) I'm relearning all thought processes of how to program.

  4. #4
    Rainbow Dash was here,
    Applejack is a silly filly.

    Join Date
    Jan 2008
    Posts
    1,425
    BG Level
    6

    Step 2, check if logs directory exists:
    Code:
    if [ -e /var/log/fla ]
    then
        echo -e "Logs directory found.\nNext Step"
    else
        echo -e "Logs directory not found\nCreating directory."
        mkdir /var/log/fla
        echo "Next Step"
    fi

  5. #5
    Rainbow Dash was here,
    Applejack is a silly filly.

    Join Date
    Jan 2008
    Posts
    1,425
    BG Level
    6

    Third step, check if the log for that day exists already:
    Code:
    if [ -e "/var/log/fla/`date +%m-%d-%Y`" ]
    then
        echo -e "Log for this day exists\nNext Step"
    else
        echo -e "Log for this day does not exist\nCreating..."
        touch "/var/log/fla/`date +%m-%d-%Y`"
        echo "Next Step"
    fi

  6. #6
    Rainbow Dash was here,
    Applejack is a silly filly.

    Join Date
    Jan 2008
    Posts
    1,425
    BG Level
    6

    Fourth final step:
    Code:
    echo "Writing to log..."
    faillog -a >> /var/log/fla/`date +%m-%d-%Y`
    echo "Resetting failed login attempts"
    faillog -r

  7. #7
    Rainbow Dash was here,
    Applejack is a silly filly.

    Join Date
    Jan 2008
    Posts
    1,425
    BG Level
    6

    Finished product, completely tested, no bugs:
    Code:
    if [ "`tail -2 /etc/pam.d/system-auth`" != "`echo -e "auth required pam_tally.so no_magic_root\naccount required pam_tally.so deny=3 no_magic_root lock_time=180"`" ]
    then
        echo -e "Requirements not found\nAppending..."
        echo -e "auth required pam_tally.so no_magic_root\naccount required pam_tally.so deny=3 no_magic_root lock_time=180" >> /etc/pam.d/system-auth
        echo -e "Next Step"
    else
        echo -e "Requirements found\nNext Step"
    fi
    if [ -e /var/log/fla ]
    then
        echo -e "Log directory found.\nNext Step"
    else
        echo -e "Log directory not found\nCreating directory."
        mkdir /var/log/fla
        echo "Next Step"
    fi
    if [ -e "/var/log/fla/`date +%m-%d-%Y`" ]
    then
        echo -e "Log for this day exists\nNext step"
    else
        echo -e "Log for this day does not exist\nCreating..."
        touch "/var/log/fla/`date  +%m-%d-%y`"
        echo "Next Step"
    fi
    echo "Writing to log..."
    faillog -a >> /var/log/fla`date +%m-%d-%Y`
    echo "Rresetting failed login attempts"
    faillog -r
    echo "Script complete"

  8. #8
    Rainbow Dash was here,
    Applejack is a silly filly.

    Join Date
    Jan 2008
    Posts
    1,425
    BG Level
    6

    If anyone has any comments on how I could've done this better, easier, or even more complex please leave them.

Similar Threads

  1. Shell Scripting
    By #686578 in forum Tech
    Replies: 7
    Last Post: 2009-02-14, 11:51