Solved

I need to validate script so error messages are returned

Posted on 2015-02-09
7
153 Views
Last Modified: 2015-02-09
My code cycles through several enviroments looking for a first/last name combination. If that is found it will print out the user_id. If not 'NO USER FOUND' is output.

I have a third issue that I have to check for, and am not quite sure how to do this. If for some reason the connection to the database fails, I need to produce a message like "DATABASE CONNECTION FAILED"

echo "environment"
sshpass -p $ldappw ssh $ldapuser@12.345.678.901  'mysql --skip-column-names -h host -u user -ppassword database -e "select user_id from users where first like '"'%${first_name}%' and last like '%${last_name}%';"'"' >$out_file 
sql_string=$(cat $out_file)
if [[ ! -z $sql_string ]]; then
   echo "$sql_string"
else
   echo "NO USER FOUND"
fi

Open in new window

0
Comment
Question by:TampaJay
  • 5
  • 2
7 Comments
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
ID: 40599430
Why didn't you use my suggestion from our previous thread? Are you a big fan of temporary files?

Anyway, change my solution from the previous thread to

#log it all
{
if UID=$(sshpass -p $ldappw ssh $ldapuser@12.345.67.89 'mysql --skip-column-names -hhost -u user -ppassword database -e \
"select user_id from users where first like '"'%${first_name}%' and last like '%${last_name}%';"'"')
then
   if [[ ! -z $UID ]]; then
      echo "environment"
      echo "$UID"
        else
           echo "NO USER FOUND"
   fi
else
  echo  "DATABASE CONNECTION FAILED"
fi 
} > $log

Open in new window


You can take out "echo "environment"" and put it above the line containing "sshpass" as in your question, if you like, yet this would somehow contradict what we discussed in that famous previous thread.
0
 

Author Comment

by:TampaJay
ID: 40599484
I  must have missed that first time around. Sorry about that.

Thanks for the help with these few problems - been a great assistance!
0
 

Author Closing Comment

by:TampaJay
ID: 40599485
Excellent contributor to the site!
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:TampaJay
ID: 40599545
I actually spoke a little too soon...

The logic you provided will now give me 'DATABASE CONNECTION FAILED' instead of 'NO USER FOUND'
0
 

Author Comment

by:TampaJay
ID: 40599555
Error I'm getting is as follows:

ERROR 2005 (HY000): Unknown MySQL server host 'database' (0)

However, this isn't being recognized properly.
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 40599604
Did you put all if's, then's and else's in the right places?

It might be that the "mysql" executable would issue a nonzero returncode when a "no record found" occurs (yet I don't believe so).
I'll check that.
0
 

Author Comment

by:TampaJay
ID: 40599617
NVM, user error :-)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Creating and Managing Databases with phpMyAdmin in cPanel.
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now