Help needed in case

Hi All,

I have a requirement to write shell script in which we have to test three cases considering ip connection which differentiate the tool's name as per as the ip address in the file.txt .

<ip1>,<ip2>,<ip3> address belongs to Tool1
<ip4>,<ip5>,<ip6> address belongs to Tool2

 if ip <ip1>,<ip2>,<ip3> address in <file.txt> will send mail -- connection -- append from <tool1>
 
If <ip4>,<ip5>,<ip6> address in file.txt will send mail --connection --append from <tool2>

Also considering the case where connection are coming from both the tool at the same time ..script can differentiate according to the ip address.

Please advice here.Thanks in advance.!

What have you tried so far?

Hi radoulov,

Thanks for response..!!

initially I'm checking the file.txt wc -l

if [$(wc -l < $file.txt -ge 3)]

then I tried grep <ip> from the file.txt depending on the ip send mail with hardcoded message -- append <tool>.but it doesn't sounds good specially when multiple process comes with different ip.Please advice how to use case command here with all the possibilities.

Please post a sample of the text file containing the IP addresses and the shell that you intend to use (es. bash, ksh or zsh) and its version.

Hi radoulov,

Please find file.txt output.

 
PLANID,SESSIONID,DATABASE,USER_NAME,IP,SQL,SUBMIT_TIME,START_TIME,RUN_TIME_MIN
"25319","45162","TEST","ADMIN","127.3.3.1","select * from test","2013-11-27 19:38:28","2013-11-27 19:38:28",11
(1 row)
 
GNU bash, version 3.2.25(1)-release (x86_64-redhat-linux-gnu)

Thanks! How can we know the tool name associated to the example IP?

Hi Radoulov,

I have sets of tools name with associated IP address.

Like Tool1 -- 127.3.3.1
Tool2 -- 127.3.3.5

OK, but how can we automate the script if we don't know the associations?
Is there another text file that contains the associations?

No.we can create a text file.We can hard code the ip address with the snedmail ?

Assuming your file.txt contains the following lines:

"25319","45162","TEST","ADMIN","127.3.3.1","select * from test","2013-11-27 19:38:28","2013-11-27 19:38:28",11
"25319","45162","TEST","ADMIN","127.3.3.4","select * from test","2013-11-27 19:38:28","2013-11-27 19:38:28",11

Assuming that there are no embedded commas in the filed values.
Something like this might work for you:

_ips=( 
  tool1_127.3.3.1
  tool1_127.3.3.2
  tool1_127.3.3.3 
  tool2_127.3.3.4
  tool2_127.3.3.5
  tool2_127.3.3.6  
  )


while IFS=, read j j j j _ip j; do
  _ip=${_ip//\"}
  for _t_ip in "${_ips[@]}"; do
    [[ $_ip == ${_t_ip##*_} ]] && {
      echo send mail -- connection -- append from "${_t_ip%_*}"
      break 
      }
  done
done < file.txt

I get the following output:

$ ./s
send mail -- connection -- append from tool1
send mail -- connection -- append from tool2

Remove the echo if the output matches your expectations.

Thanks a lot..!! Radoulov.

I guess we are not receiving the file.txt full line with this detail

"25319","45162","TEST","ADMIN","127.3.3.1","select * from test","2013-11-27 19:38:28","2013-11-27 19:38:28",11"25319","45162","TEST","ADMIN","127.3.3.4","select * from test","2013-11-27 19:38:28","2013-11-27 19:38:28",11

Can we add if ip address is this,this will append mail with full detail like above format in a file.

In this case, you could use something like this:

_ips=( 
  tool1_127.3.3.1
  tool1_127.3.3.2
  tool1_127.3.3.3 
  tool2_127.3.3.4
  tool2_127.3.3.5
  tool2_127.3.3.6  
  )

  
for _t_ip in "${_ips[@]}"; do
  grep -w "${_t_ip##*_}" file.txt > /dev/null &&
      echo send mail -- connection -- append from "${_t_ip%_*}"
done

To get the entire line containing the IP:

_ips=(
  tool1_127.3.3.1
  tool1_127.3.3.2
  tool1_127.3.3.3
  tool2_127.3.3.4
  tool2_127.3.3.5
  tool2_127.3.3.6
  )


for _t_ip in "${_ips[@]}"; do
  _record=$(
    grep -w "${_t_ip##*_}" file.txt
    ) &&
    echo send mail -- connection -- append from "${_t_ip%_*}" -- detail "$_record"
done