user_password_change_post.sh

Djunity

Verified User
Joined
Mar 9, 2008
Messages
243
Location
Holland
Hi all,

Im currently working on a sync function for the account passwords from all accounts on multiple da servers. Da support pointed me to a feature http://www.directadmin.com/features.php?id=581

there are a few environmental variables:
Code:
username=username
passwd=password
home=/home/username

Im writing the script in php so my problem is how can i use the above variables in my php script.
Im not really familiar with shell scripts

i got this for testing so far:
PHP:
#!/usr/local/bin/php

<?
define("DB_HOST","localhost");
define("DB_USERNAME","dbuser");
define("DB_PASSWORD","dbpass");
define("DB_NAME","dbname");

$username = '';
$password = '';

$dbhandle = mysql_connect(DB_HOST, DB_USERNAME, DB_PASSWORD);
$selected = mysql_select_db(DB_NAME ,$dbhandle);

$sql = mysql_query("SELECT * FROM `database_table` WHERE `Username` = '".$username."' ");

echo mysql_num_rows($sql);
?>

Thanks alot.
 
Hello,

In user_password_change_post.sh execute your php script:

Code:
 /usr/local/bin/php /usr/local/directadmin/scripts/custom/user_password_change_post.php $username $passwd $home

In the script user_password_change_post.php you can get them with $argv:

Code:
$username=(isset($argv[1])) ? $argv[1] : false;
$passwd=(isset($argv[2])) ? $argv[2] : false;
$home=(isset($argv[3])) ? $argv[3] : false;
 
Thanks alot this does the trick indeed.

Got one question left if i now run the .sh file from command line it updates the password of the account in the internal database but when i log in in the account and change the password then it doens do anything do you have an idea ?
 
You should ensure, that no error occurs either in shell or php script. Add some logging to your PHP script. For testing and debugging purposes add a function to save in a file results of MySQL query and some other important things, that will help you to understand what is going on.

Make sure, that permissions are correct on both scripts:

-rwx------ diradmin diradmin
 
You should ensure, that no error occurs either in shell or php script. Add some logging to your PHP script. For testing and debugging purposes add a function to save in a file results of MySQL query and some other important things, that will help you to understand what is going on.

Make sure, that permissions are correct on both scripts:

Thanks but as far as i can see chmod is ok but stil get:
Code:
./user_password_change_post.sh
Could not open input file: /usr/local/directadmin/scripts/custom/user_password_change_post.php $username $passwd

Chmod is:
Code:
ls -la user_password_change_post.sh
-rwx------ 1 diradmin diradmin 119 Feb 27 15:10 user_password_change_post.sh

Sorry for my ignorance but im not used to work with shell scripts
 
I suggested that you can use /usr/local/directadmin/scripts/custom/user_password_change_post.php. Did you create it?

Update it to feet your needs.
 
I suggested that you can use /usr/local/directadmin/scripts/custom/user_password_change_post.php. Did you create it?

Update it to feet your needs.

Yes the file is created:
Code:
ls -la user_password_change_post.php
-rwx------ 1 diradmin diradmin 593 Feb 27 15:12 user_password_change_post.php

Code in the php is:
PHP:
<?
define("DB_HOST","localhost");
define("DB_USERNAME","dbuser");
define("DB_PASSWORD","dbpass");
define("DB_NAME","dbname");

$username=(isset($argv[1])) ? $argv[1] : false;
$password=(isset($argv[2])) ? $argv[2] : false;

$dbhandle = mysql_connect(DB_HOST, DB_USERNAME, DB_PASSWORD);
$selected = mysql_select_db(DB_NAME ,$dbhandle);

$sql = mysql_query("SELECT * FROM `database_table` WHERE `Username` = '".$username."' ");
if(mysql_num_rows($sql)>0) {
mysql_query("UPDATE `database_table` SET `Password` = '".$password."' WHERE `Username` = '".$username."' ");
}?>

The php code does not result in error check that allready with running /usr/local/bin/php /usr/local/directadmin/scripts/custom/user_password_change_post.php testuser testpasswd from command line

sh has:
Code:
#!/usr/local/bin/php /usr/local/directadmin/scripts/custom/user_password_change_post.php $username $passwd
 
That's not correct.

Should be like:

Code:
#!/bin/sh

/usr/local/bin/php /usr/local/directadmin/scripts/custom/user_password_change_post.php $username $passwd
 
That's not correct.

Should be like:

Code:
#!/bin/sh

/usr/local/bin/php /usr/local/directadmin/scripts/custom/user_password_change_post.php $username $passwd

That to gave me an error im now running:
Code:
#/bin/bash

/usr/local/bin/php /usr/local/directadmin/scripts/custom/user_password_change_post.php $username $passwd

When i now run ./user_password_change_post.sh i dont have an error but its not updating the database when i change the $username and $ passwd to hardcoded values for example testuser testpasswd then the database is updated so it looks to me that /usr/local/bin/php /usr/local/directadmin/scripts/custom/user_password_change_post.php $username $passwd isnt the rigth way to get the values passed over any other idea ?
 
Hello,

If you ignore my words fully or partly, I'm out of here, because it's a waste of my time.

1. Shell scripts should start with:

Code:
#!/bin/bash

or

Code:
#!/bin/sh

not with

Code:
#/bin/bash

2. I suggest using debugging. Simple use var_dump(); in your PHP script.
It's the first time (and the last of course), I'm modifying PHP scripts here:

PHP:
<?
define("DB_HOST","localhost");
define("DB_USERNAME","dbuser");
define("DB_PASSWORD","dbpass");
define("DB_NAME","dbname");

$username=(isset($argv[1])) ? $argv[1] : false;
$password=(isset($argv[2])) ? $argv[2] : false;

// what do we have:
var_dump($argv);
// var_dump($username, $password);

$dbhandle = mysql_connect(DB_HOST, DB_USERNAME, DB_PASSWORD);
$selected = mysql_select_db(DB_NAME ,$dbhandle);

// did we connect to MySQL?
var_dump($dbhandle, $selected);

$result = mysql_query("SELECT * FROM `database_table` WHERE `Username` = '".$username."' ");

// what did we get from MySQL?
var_dump($result);

if(mysql_num_rows($sql)>0) {
$result = mysql_query("UPDATE `database_table` SET `Password` = '".$password."' WHERE `Username` = '".$username."' ");
}

// what did we get from MySQL?
var_dump($result);

?>
 
Back
Top