LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-06-2010, 06:34 PM   #1
Treikayan
Member
 
Registered: Oct 2008
Location: Albany Park, Chicago IL
Distribution: RHEL 5.1 i386
Posts: 75

Rep: Reputation: 15
Bash script help


Hello,

I need assistance making a bash script that searches for current logged on members for FTP. I would like to be notified in e-mail when a user logs on. I tried something on my own and it didn't work. Below is my code. Thank you for any help.

Code:
#!/bin/bash
declare -a username
file="/var/scripts/ftp-logged-`date +%y-%m-%d`.log"
username=(`ls /home | grep -v /home`)
logged="$(lsof -ni TCP:21 | grep $username[*])"
  if [ -n "$logged" ]
  then
    if [ ! -f "$file" ]
    then
     lsof -ni TCP:21 | /bin/mail -s "User Login Notice" root
    fi
   lsof -ni TCP:21 >> $file
  fi
/usr/bin/find /var/scripts/*.log -type f -mmin 360 -exec rm {} \;
 
Old 03-06-2010, 11:31 PM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
What were the symptoms of "didn't work"?

What is the output of
Code:
ls /home | grep -v /home
and, when you have an FTP user, of
Code:
username=(`ls /home | grep -v /home`)
lsof -ni TCP:21 | grep ${username[*]}
EDIT: and
Code:
username=(`ls /home | grep -v /home`)
echo grep ${username[*]}

Last edited by catkin; 03-06-2010 at 11:34 PM.
 
Old 03-07-2010, 03:33 PM   #3
Treikayan
Member
 
Registered: Oct 2008
Location: Albany Park, Chicago IL
Distribution: RHEL 5.1 i386
Posts: 75

Original Poster
Rep: Reputation: 15
Quote:
ls /home | grep -v /home
command output:
Code:
userA
userB
userC
userD
userE
...

Quote:
username=(`ls /home | grep -v /home`)
lsof -ni TCP:21 | grep ${username[*]}
With UserE logged onto FTP...

command output:
Code:
grep: userB: No such file or directory
grep: userC: No such file or directory
grep: userD: No such file or directory
grep: userE: No such file or directory
...
Quote:
username=(`ls /home | grep -v /home`)
echo grep ${username[*]}
With UserE logged onto FTP...

command output:
Code:
grep UserA UserB UserC UserD UserE [...]
 
Old 03-07-2010, 04:28 PM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by Treikayan View Post
command output:
Code:
grep UserA UserB UserC UserD UserE [...]
That tells grep to search files UserB UserC UserD UserE [...] for string UserA and that ain't never going to do what you want.

You could save a sample lsof output into a file
Code:
lsof -ni TCP:21 > my_test.input
and then experiment with grep commands until you get what you want
Code:
cat my_test.input | grep <grep options>
Chances are you will need the -E option.

That general technique, of experimenting with command formats at the command line, is a powerful way to establish what can later be programmed into a script.

If you get stuck, ask again and post what you have tried.

Last edited by catkin; 03-07-2010 at 04:28 PM. Reason: Errant comma
 
Old 03-13-2010, 01:01 AM   #5
Treikayan
Member
 
Registered: Oct 2008
Location: Albany Park, Chicago IL
Distribution: RHEL 5.1 i386
Posts: 75

Original Poster
Rep: Reputation: 15
Thanks catkin. I think I got it to work.

Code:
#!/bin/bash
declare -a username
file="/var/lock/ftp-logged.lock"
username=( userA userB userC userD userE )
lsof -ni TCP:21 > $file
for name in ${username[@]}
do
ftplog="/var/log/ftplog-$name-`date +%y-%m-%d`.log"
# echo $name
  logged="$(cat $file | grep $name)"
    if [ -n "$logged" ]
    then
      if [ ! -f "$ftplog" ]
      then
       lsof -ni TCP:21 | /bin/mail -s "User Login Notice: $name" root
      fi
     lsof -ni TCP:21 >> $ftplog
   fi
  /usr/bin/find /var/log/ftplog-$name-* -type f -mmin 60 -exec rm {} \;
 done
/bin/rm -rf $file
I was going to put this into crontab.

Last edited by Treikayan; 03-13-2010 at 01:02 AM.
 
Old 03-13-2010, 02:09 AM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by Treikayan View Post
Thanks catkin. I think I got it to work.

I was going to put this into crontab.
Glad you got it working

A few caveats regards running a script tested at the command line from cron:
  • The $PATH value is different; either set it explicitly or don't rely on it. The latter is more secure and transparent.
  • When running under cron there is no terminal to connect stdin, stdout and stderr to; ensure they are not required. One solution is to redirect stdout and stderr to /dev/null in the crontab. That's OK but not helpful for debugging.
With those in mind ...
Code:
#!/bin/bash
declare -a username
exec 1>"/tmp/${0##*/}.stdout.$$"
exec 2>"/tmp/${0##*/}.stderr.$$"
file="/var/lock/ftp-logged.lock"
username=( userA userB userC userD userE )
/usr/bin/lsof -ni TCP:21 > $file
for name in ${username[@]}
do
  ftplog="/var/log/ftplog-$name-`date +%y-%m-%d`.log"
  # echo $name
  logged="$(/usr/bin/cat $file | /usr/bin/grep $name)"
    if [ -n "$logged" ]
    then
      if [ ! -f "$ftplog" ]
      then
       lsof -ni TCP:21 | /bin/mail -s "User Login Notice: $name" root
      fi
     lsof -ni TCP:21 >> $ftplog
   fi
  /usr/bin/find /var/log/ftplog-$name-* -type f -mmin 60 -exec /bin/rm {} \;
 done
/bin/rm -rf $file
Notes:
  • file=/var/lock/ftp-logged.lock suggests a lock file but is it not an intermediate results file? If that is right then variable, directory and file names better reflecting its use would be more legible. Maybe something like first_lsof_out="/tmp/${0##*/}.tmp.$$"
  • You could avoid using an intermediate results file by storing output from the first lsof in a shell variable and then using a case statement in lieu of the grep to search it. Pros: tidier and (trivially?) less resource usage. Cons: a few more lines of script.
  • If you want consistency, you might prefer not to mix $( <command> ) and ` <command> ` in the same script; the former is preferred for reasons explained here.
EDIT: Ooops! There are a couple of "lsof"s up there not prefixed with /usr/bin

Last edited by catkin; 03-13-2010 at 02:11 AM.
 
Old 03-27-2010, 08:22 PM   #7
Treikayan
Member
 
Registered: Oct 2008
Location: Albany Park, Chicago IL
Distribution: RHEL 5.1 i386
Posts: 75

Original Poster
Rep: Reputation: 15
Thanks cakin.

The script works fine when I run it from the command, but when I put it in crontab and STDOUT > /dev/null STDERROR 2>&1 , it's doesn't run. Please help.

Crontab entry:
Code:
*/5 * * * * /bin/bash /var/scripts/ftp.sh > /dev/null 2>&1
Thanks again.
 
Old 03-27-2010, 11:58 PM   #8
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Please post the script.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
passing variable from bash to perl in a bash script quadmore Programming 6 02-21-2011 05:11 AM
[SOLVED] Using a long Bash command including single quotes and pipes in a Bash script antcore Linux - General 9 07-22-2009 12:10 PM
Strange if statement behaviour when using bash/bash script freeindy Programming 7 08-04-2008 07:00 AM
Bash script to create bash script jag7720 Programming 10 09-10-2007 08:01 PM
[bash] having trouble debugging this bash script. jons Programming 4 02-08-2007 07:51 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:14 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration