Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I am writing a script to let me know the database login info for each PHP-based web site on a server that I am migrating. The login information is usually done either like this:

$servername='localhost';
$database_username='someName';
$database_password='p@ssw0rd';
$database_name='someDatabase';

mysql_connect($servername, $database_username, $database_password);
mysql_select_db($database_name);

Or like this:

$pdo = new PDO("mysql: host={$servername}; dbname={$$database_name}", $database_username, $database_password);

The script is searching for each line which contains one of the PHP connection functions:

grep -irE "mysql_connect\|mysql_select_db\|new PDO" *

Now, how can I get each string that begins with $ and ends with a non-alphanumeric? Additionally, if no ; character is found on the screen I would like to be notified so that I could handle it manually, as some coders might do this:

$pdo = new PDO("mysql: host={$servername};
                dbname={$$database_name}",
                $database_username,
                $database_password
               );

After I get these tokens (PHP variables), I'll grep the file again for the tokens in hope to get their values;

share|improve this question

1 Answer 1

up vote 1 down vote accepted

I am not really sure what you are trying to do here, but you can grep "all strings that begin with $ and end with a non-alphanumeric character" with this:

grep -oP '\$.+?\W' 

In response to your comment, how about something like this:

for f in `find . -name "*.php"`; do \
  echo "----- $f -----";
  for s in $(grep -iE "mysql_connect|mysql_select_db|new PDO" $f | 
     grep  -oP '\$\w+?\W' | sed 's/.$//' | sort | uniq); do \
       grep -m 1 $s $f; 
 done; 
done

This scriptlet will find all .php files, grep each of them for the variables you are interested in and then print the first instance of each variable in the file it was found in. For example, I created 2 .php files, aa.php and bb.php, their contents are:

$ cat aa.php 
$servername='localhost';
$database_username='someName';
$database_password='p@ssw0rd';
$database_name='someDatabase';
mysql_connect($servername, $database_username, $database_password);
mysql_select_db($database_name);

$ cat bb.php 
$servername='localhost2';
$database_username='some2Name';
$database_password='p@ssw0rd2';
$database_name='someDatabase2';
$pdo = new PDO("mysql: host={$servername}; dbname={$$database_name}", $database_username, $database_password);

The output of the script above is:

----- ./bb.php -----
$database_name='someDatabase2';
$database_password='p@ssw0rd2';
$database_username='some2Name';
$pdo = new PDO("mysql: host={$servername}; dbname={$$database_name}", $database_username, $database_password);
$servername='localhost2';
----- ./aa.php -----
$database_name='someDatabase';
$database_password='p@ssw0rd';
$database_username='someName';
$servername='localhost';
share|improve this answer
    
My goal is to find the MySQL login information. It is often not provided directly as arguments to mysql_connect() but rather as variables. –  dotancohen Apr 14 '13 at 15:01
    
@dotancohen see my updated answer, it should do what you want. –  terdon Apr 14 '13 at 17:16
    
Thank you very much! Although the function doesn't get the job done for me, it teaches me quite a few things and therefore is helping me learn. That is most important. Thanks! –  dotancohen Apr 18 '13 at 14:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.