LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 03-15-2005, 10:02 AM   #1
Erhnam
Member
 
Registered: Jan 2005
Posts: 54

Rep: Reputation: 15
[php] comparing found username with string


I'm trying to build a server script to add local system users. Before creating a user the system must be sure that the user does not already exist. I'm trying to do this with the following code:

PHP Code:
$temp passthru ("ssh root@$_SESSION[server] 'cat /etc/passwd | grep $user'");
if (!empty(
$temp)) {
   echo 
"A match was found, user exists.";
} else { 
Somewhere there is a mistake. The script is always passing and no matches are found.. I debuged this with echo'ing the result of $temp. What could be wrong?

PS: I'm using a copy of the file /etc/passwd.. This is only an example!
 
Old 03-15-2005, 10:30 AM   #2
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,506

Rep: Reputation: 116Reputation: 116
Passthru is just supposed to display the output directly to the screen. You probably want to be using exec here. Also, your script sent shivers down my spine from a security standpoint.
 
Old 03-15-2005, 02:07 PM   #3
keefaz
Senior Member
 
Registered: Mar 2004
Distribution: Slackware
Posts: 4,282

Rep: Reputation: 66
I would do :
PHP Code:
$users file_get_contents("/etc/passwd");
if((
$pos strpos($users$user)) === false) {
    echo 
"$user was not found in /etc/passwd\n";
} else {
    echo 
"$user was found in /etc/passwd\n";

Now if you want to get user values like shell, uid... better is to
use an user Array and loop its values
 
Old 03-15-2005, 04:40 PM   #4
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,506

Rep: Reputation: 116Reputation: 116
strpos is dangerous in that context! suppose the following:
Someone attempts to sign up with the username "ack". In the /etc/passwd, there exists the following user:
Code:
jack:*:..... remainder of line
strpos would match on this, but there is no user ack. the following regular expression would be more suited to finding the matches:
/^$username:/, though you would want to make sure username contains no colons first (not that they should be there in a username anyway)
 
Old 03-15-2005, 09:34 PM   #5
keefaz
Senior Member
 
Registered: Mar 2004
Distribution: Slackware
Posts: 4,282

Rep: Reputation: 66
In this case, better is to do a strict string compare
PHP Code:
$fd fopen("/etc/passwd""r");
$found false;

while( (
$infos fgetcsv($fd256":")) !== FALSE ) {
    if(
$user == $infos[0]) {
        
$found true;
        echo 
"$user was found\n";
        echo 
"$user infos are: \n";
        
print_r($infos);
        break;
    }
}
fclose($fd);
if(!
$found) {
    echo 
"$user was not found\n";

 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
username could not be found - samba drafter Linux - Networking 6 01-06-2008 05:36 PM
use grep and execute a command if string is found plisken Linux - General 6 07-28-2005 11:06 AM
PHP More than one space in a string Boffy Programming 4 05-01-2004 02:54 PM
PHP and Username Authentication Graanco Linux - Software 0 07-31-2003 03:35 PM
storing and comparing a string Randall Programming 18 03-10-2003 10:05 PM


All times are GMT -5. The time now is 09:13 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration