Advertisement
Guest User

Untitled

a guest
Feb 29th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use DBI;
  4. use 5.010;
  5. use Getopt::Long;
  6.  
  7. # Set defaults
  8. my $host = 'localhost';
  9. my $db = 'information_schema';
  10. my $user = '';
  11. my $pass = '';
  12.  
  13. # Get commandline options
  14. GetOptions (
  15. "host=s" => \$host,
  16. "db=s" => \$db,
  17. "user=s" => \$user,
  18. "pass=s" => \$pass,
  19. ) or die ( "Valid options\n--host\n--user\n--password\n--db database" );
  20.  
  21. # Create database connection
  22. my $dbh = DBI->connect( "DBI:mysql:$db:$host", $user, $pass, {'PrintError'=>0} )
  23. or die "** Connection error!\nTry different options:\n--host\n--user\n--password\n--db database\n\n $DBI:: +++errstr\n";
  24.  
  25. # Get the current processes to check user
  26. my $sql = "show processlist";
  27. my $sth = $dbh->prepare($sql);
  28. $sth->execute() or die "SQL error: $DBI::errstr\n";
  29.  
  30. # Create an array of all the users
  31. my %user;
  32. while (my @row = $sth->fetchrow_array()) {
  33. $user{$row[1]}++;
  34. }
  35.  
  36. # Display User details
  37. print "MySQL - Logged in users\n";
  38. print "=" x 15, "\n";
  39.  
  40. for my $x (keys %user) {
  41. print "$x - $user{$x} connections\n";
  42. }
  43.  
  44.  
  45. print "\n";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement