Notification email when someone SSH Linux Centos7 workstation (output of the command who)

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












My Linux workstation is part of a shared Network. I would like to receive a notification email every time someone SSH my workstation. For that purpose, I opened the file ~/.bash_profile in gedit (my ~/.bash_profile file is completely empty) and I wrote the following lines:



IP="$(echo $SSH_CONNECTION | awk 'print $1')"
HOST=$(hostname)
TIME_DATE_STAMP=$(date +"%e %b %Y, %a %r")

echo 'The '$IP' logged into '$HOST' on '$ TIME_DATE_STAMP'.' | mail -s 'SSH
Login Notification' MY_EMAIL_ADDRESS


I still don't get a notification email! Please notify me what I am doing wrong?



To get around this issue:
When I use the command "who -H" int terminal. It pulls out a list for all the incoming ssh actions to my workstation. My questions is: How the command "who" generate this information? what is the source of its data, so I can rely on it to get the ssh data that I want?










share|improve this question



















  • 2




    You have edited your own ~/.bash_profile, which is only executed when you log in; it has no effect on other users! (Commenting as to your attempted direction, as it's not an answer to the possible question of sending an actual notification email for those circumstances)
    – Jeff Schaller
    Aug 31 at 1:22










  • Thank you so much Jeff! In this case should I make the code a bash and scheduale it to run periodically (i.e. using crontab -e)?
    – Goro
    Aug 31 at 1:27














up vote
0
down vote

favorite












My Linux workstation is part of a shared Network. I would like to receive a notification email every time someone SSH my workstation. For that purpose, I opened the file ~/.bash_profile in gedit (my ~/.bash_profile file is completely empty) and I wrote the following lines:



IP="$(echo $SSH_CONNECTION | awk 'print $1')"
HOST=$(hostname)
TIME_DATE_STAMP=$(date +"%e %b %Y, %a %r")

echo 'The '$IP' logged into '$HOST' on '$ TIME_DATE_STAMP'.' | mail -s 'SSH
Login Notification' MY_EMAIL_ADDRESS


I still don't get a notification email! Please notify me what I am doing wrong?



To get around this issue:
When I use the command "who -H" int terminal. It pulls out a list for all the incoming ssh actions to my workstation. My questions is: How the command "who" generate this information? what is the source of its data, so I can rely on it to get the ssh data that I want?










share|improve this question



















  • 2




    You have edited your own ~/.bash_profile, which is only executed when you log in; it has no effect on other users! (Commenting as to your attempted direction, as it's not an answer to the possible question of sending an actual notification email for those circumstances)
    – Jeff Schaller
    Aug 31 at 1:22










  • Thank you so much Jeff! In this case should I make the code a bash and scheduale it to run periodically (i.e. using crontab -e)?
    – Goro
    Aug 31 at 1:27












up vote
0
down vote

favorite









up vote
0
down vote

favorite











My Linux workstation is part of a shared Network. I would like to receive a notification email every time someone SSH my workstation. For that purpose, I opened the file ~/.bash_profile in gedit (my ~/.bash_profile file is completely empty) and I wrote the following lines:



IP="$(echo $SSH_CONNECTION | awk 'print $1')"
HOST=$(hostname)
TIME_DATE_STAMP=$(date +"%e %b %Y, %a %r")

echo 'The '$IP' logged into '$HOST' on '$ TIME_DATE_STAMP'.' | mail -s 'SSH
Login Notification' MY_EMAIL_ADDRESS


I still don't get a notification email! Please notify me what I am doing wrong?



To get around this issue:
When I use the command "who -H" int terminal. It pulls out a list for all the incoming ssh actions to my workstation. My questions is: How the command "who" generate this information? what is the source of its data, so I can rely on it to get the ssh data that I want?










share|improve this question















My Linux workstation is part of a shared Network. I would like to receive a notification email every time someone SSH my workstation. For that purpose, I opened the file ~/.bash_profile in gedit (my ~/.bash_profile file is completely empty) and I wrote the following lines:



IP="$(echo $SSH_CONNECTION | awk 'print $1')"
HOST=$(hostname)
TIME_DATE_STAMP=$(date +"%e %b %Y, %a %r")

echo 'The '$IP' logged into '$HOST' on '$ TIME_DATE_STAMP'.' | mail -s 'SSH
Login Notification' MY_EMAIL_ADDRESS


I still don't get a notification email! Please notify me what I am doing wrong?



To get around this issue:
When I use the command "who -H" int terminal. It pulls out a list for all the incoming ssh actions to my workstation. My questions is: How the command "who" generate this information? what is the source of its data, so I can rely on it to get the ssh data that I want?







bash ssh scripting email notifications






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 31 at 8:10

























asked Aug 31 at 1:01









Goro

4,41452356




4,41452356







  • 2




    You have edited your own ~/.bash_profile, which is only executed when you log in; it has no effect on other users! (Commenting as to your attempted direction, as it's not an answer to the possible question of sending an actual notification email for those circumstances)
    – Jeff Schaller
    Aug 31 at 1:22










  • Thank you so much Jeff! In this case should I make the code a bash and scheduale it to run periodically (i.e. using crontab -e)?
    – Goro
    Aug 31 at 1:27












  • 2




    You have edited your own ~/.bash_profile, which is only executed when you log in; it has no effect on other users! (Commenting as to your attempted direction, as it's not an answer to the possible question of sending an actual notification email for those circumstances)
    – Jeff Schaller
    Aug 31 at 1:22










  • Thank you so much Jeff! In this case should I make the code a bash and scheduale it to run periodically (i.e. using crontab -e)?
    – Goro
    Aug 31 at 1:27







2




2




You have edited your own ~/.bash_profile, which is only executed when you log in; it has no effect on other users! (Commenting as to your attempted direction, as it's not an answer to the possible question of sending an actual notification email for those circumstances)
– Jeff Schaller
Aug 31 at 1:22




You have edited your own ~/.bash_profile, which is only executed when you log in; it has no effect on other users! (Commenting as to your attempted direction, as it's not an answer to the possible question of sending an actual notification email for those circumstances)
– Jeff Schaller
Aug 31 at 1:22












Thank you so much Jeff! In this case should I make the code a bash and scheduale it to run periodically (i.e. using crontab -e)?
– Goro
Aug 31 at 1:27




Thank you so much Jeff! In this case should I make the code a bash and scheduale it to run periodically (i.e. using crontab -e)?
– Goro
Aug 31 at 1:27










3 Answers
3






active

oldest

votes

















up vote
2
down vote



accepted










You can pull out the information you want from the command "who", then pipe it to the command mail. An example command:



 who | awk 'print $1" "$3" "$4" " $5' | tail -1 | mail -s 'WARNING: SSH Notification' EMAIL_ADDRESS


Save the previous command as a bash, and schedule it in crontab to run every specific minutes (e.g. every 30 minutes).






share|improve this answer



























    up vote
    2
    down vote













    you can pipe the output of the command last .



    last | head -1 | mail -s 'WARNING:' EMAIL_ADRESS





    share|improve this answer



























      up vote
      1
      down vote













      A couple things:



      • Your ~/.bash_profile is only kicked off when you login. These other users may not be logging in as you.


      • For observing who is logging into your CentOS machine over ssh, parse the output of /var/log/secure.


      It'd look something like this:



      tail -f /var/log/secure | while read LOGLINE
      do
      [[ "$LOGLINE" == *"logged in"* ]] && mail -s 'SSH Notification' $MY_EMAIL_ADDRESS
      done


      EDIT: You've edited your question to ask about how the who command works. Look I'm not a C developer but here's the source code of the who command if you really want a peek at how it works. I don't think this is what you're looking for, however - and I know've no other way to assist you. Even if you could create a script to check if someone is logged in leveraging cron & who, there's always a possibility that someone could login & logout between your runs. Logs are the way to go.






      share|improve this answer






















      • Thank you Rakaim. Nice solution, but I don't have permissions to read "var/log/secure"!
        – Goro
        Aug 31 at 1:31






      • 1




        @goro Then you do not have the ability to see ssh sessions into your machine.
        – Rakaim
        Aug 31 at 1:33










      Your Answer







      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "106"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: false,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













       

      draft saved


      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f465890%2fnotification-email-when-someone-ssh-linux-centos7-workstation-output-of-the-com%23new-answer', 'question_page');

      );

      Post as a guest






























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      2
      down vote



      accepted










      You can pull out the information you want from the command "who", then pipe it to the command mail. An example command:



       who | awk 'print $1" "$3" "$4" " $5' | tail -1 | mail -s 'WARNING: SSH Notification' EMAIL_ADDRESS


      Save the previous command as a bash, and schedule it in crontab to run every specific minutes (e.g. every 30 minutes).






      share|improve this answer
























        up vote
        2
        down vote



        accepted










        You can pull out the information you want from the command "who", then pipe it to the command mail. An example command:



         who | awk 'print $1" "$3" "$4" " $5' | tail -1 | mail -s 'WARNING: SSH Notification' EMAIL_ADDRESS


        Save the previous command as a bash, and schedule it in crontab to run every specific minutes (e.g. every 30 minutes).






        share|improve this answer






















          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          You can pull out the information you want from the command "who", then pipe it to the command mail. An example command:



           who | awk 'print $1" "$3" "$4" " $5' | tail -1 | mail -s 'WARNING: SSH Notification' EMAIL_ADDRESS


          Save the previous command as a bash, and schedule it in crontab to run every specific minutes (e.g. every 30 minutes).






          share|improve this answer












          You can pull out the information you want from the command "who", then pipe it to the command mail. An example command:



           who | awk 'print $1" "$3" "$4" " $5' | tail -1 | mail -s 'WARNING: SSH Notification' EMAIL_ADDRESS


          Save the previous command as a bash, and schedule it in crontab to run every specific minutes (e.g. every 30 minutes).







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Sep 1 at 10:43









          Kasper

          14912




          14912






















              up vote
              2
              down vote













              you can pipe the output of the command last .



              last | head -1 | mail -s 'WARNING:' EMAIL_ADRESS





              share|improve this answer
























                up vote
                2
                down vote













                you can pipe the output of the command last .



                last | head -1 | mail -s 'WARNING:' EMAIL_ADRESS





                share|improve this answer






















                  up vote
                  2
                  down vote










                  up vote
                  2
                  down vote









                  you can pipe the output of the command last .



                  last | head -1 | mail -s 'WARNING:' EMAIL_ADRESS





                  share|improve this answer












                  you can pipe the output of the command last .



                  last | head -1 | mail -s 'WARNING:' EMAIL_ADRESS






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Sep 1 at 14:32









                  TNT

                  314112




                  314112




















                      up vote
                      1
                      down vote













                      A couple things:



                      • Your ~/.bash_profile is only kicked off when you login. These other users may not be logging in as you.


                      • For observing who is logging into your CentOS machine over ssh, parse the output of /var/log/secure.


                      It'd look something like this:



                      tail -f /var/log/secure | while read LOGLINE
                      do
                      [[ "$LOGLINE" == *"logged in"* ]] && mail -s 'SSH Notification' $MY_EMAIL_ADDRESS
                      done


                      EDIT: You've edited your question to ask about how the who command works. Look I'm not a C developer but here's the source code of the who command if you really want a peek at how it works. I don't think this is what you're looking for, however - and I know've no other way to assist you. Even if you could create a script to check if someone is logged in leveraging cron & who, there's always a possibility that someone could login & logout between your runs. Logs are the way to go.






                      share|improve this answer






















                      • Thank you Rakaim. Nice solution, but I don't have permissions to read "var/log/secure"!
                        – Goro
                        Aug 31 at 1:31






                      • 1




                        @goro Then you do not have the ability to see ssh sessions into your machine.
                        – Rakaim
                        Aug 31 at 1:33














                      up vote
                      1
                      down vote













                      A couple things:



                      • Your ~/.bash_profile is only kicked off when you login. These other users may not be logging in as you.


                      • For observing who is logging into your CentOS machine over ssh, parse the output of /var/log/secure.


                      It'd look something like this:



                      tail -f /var/log/secure | while read LOGLINE
                      do
                      [[ "$LOGLINE" == *"logged in"* ]] && mail -s 'SSH Notification' $MY_EMAIL_ADDRESS
                      done


                      EDIT: You've edited your question to ask about how the who command works. Look I'm not a C developer but here's the source code of the who command if you really want a peek at how it works. I don't think this is what you're looking for, however - and I know've no other way to assist you. Even if you could create a script to check if someone is logged in leveraging cron & who, there's always a possibility that someone could login & logout between your runs. Logs are the way to go.






                      share|improve this answer






















                      • Thank you Rakaim. Nice solution, but I don't have permissions to read "var/log/secure"!
                        – Goro
                        Aug 31 at 1:31






                      • 1




                        @goro Then you do not have the ability to see ssh sessions into your machine.
                        – Rakaim
                        Aug 31 at 1:33












                      up vote
                      1
                      down vote










                      up vote
                      1
                      down vote









                      A couple things:



                      • Your ~/.bash_profile is only kicked off when you login. These other users may not be logging in as you.


                      • For observing who is logging into your CentOS machine over ssh, parse the output of /var/log/secure.


                      It'd look something like this:



                      tail -f /var/log/secure | while read LOGLINE
                      do
                      [[ "$LOGLINE" == *"logged in"* ]] && mail -s 'SSH Notification' $MY_EMAIL_ADDRESS
                      done


                      EDIT: You've edited your question to ask about how the who command works. Look I'm not a C developer but here's the source code of the who command if you really want a peek at how it works. I don't think this is what you're looking for, however - and I know've no other way to assist you. Even if you could create a script to check if someone is logged in leveraging cron & who, there's always a possibility that someone could login & logout between your runs. Logs are the way to go.






                      share|improve this answer














                      A couple things:



                      • Your ~/.bash_profile is only kicked off when you login. These other users may not be logging in as you.


                      • For observing who is logging into your CentOS machine over ssh, parse the output of /var/log/secure.


                      It'd look something like this:



                      tail -f /var/log/secure | while read LOGLINE
                      do
                      [[ "$LOGLINE" == *"logged in"* ]] && mail -s 'SSH Notification' $MY_EMAIL_ADDRESS
                      done


                      EDIT: You've edited your question to ask about how the who command works. Look I'm not a C developer but here's the source code of the who command if you really want a peek at how it works. I don't think this is what you're looking for, however - and I know've no other way to assist you. Even if you could create a script to check if someone is logged in leveraging cron & who, there's always a possibility that someone could login & logout between your runs. Logs are the way to go.







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Aug 31 at 15:05

























                      answered Aug 31 at 1:26









                      Rakaim

                      1265




                      1265











                      • Thank you Rakaim. Nice solution, but I don't have permissions to read "var/log/secure"!
                        – Goro
                        Aug 31 at 1:31






                      • 1




                        @goro Then you do not have the ability to see ssh sessions into your machine.
                        – Rakaim
                        Aug 31 at 1:33
















                      • Thank you Rakaim. Nice solution, but I don't have permissions to read "var/log/secure"!
                        – Goro
                        Aug 31 at 1:31






                      • 1




                        @goro Then you do not have the ability to see ssh sessions into your machine.
                        – Rakaim
                        Aug 31 at 1:33















                      Thank you Rakaim. Nice solution, but I don't have permissions to read "var/log/secure"!
                      – Goro
                      Aug 31 at 1:31




                      Thank you Rakaim. Nice solution, but I don't have permissions to read "var/log/secure"!
                      – Goro
                      Aug 31 at 1:31




                      1




                      1




                      @goro Then you do not have the ability to see ssh sessions into your machine.
                      – Rakaim
                      Aug 31 at 1:33




                      @goro Then you do not have the ability to see ssh sessions into your machine.
                      – Rakaim
                      Aug 31 at 1:33

















                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f465890%2fnotification-email-when-someone-ssh-linux-centos7-workstation-output-of-the-com%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Popular posts from this blog

                      Peggy Mitchell

                      Palaiologos

                      The Forum (Inglewood, California)