Jump to content

Big question 2


PhateX

Recommended Posts

Well sorry to be back here again, but my friend insisted that the project could be done in php and this was his solution:

 

"It can be done, I'm sure of it.  If not with php alone, then with SAMBA in linux.  This is why I don't like forms.  You get incompetent know-it-alls who think there is a better solution to anything.  I wouldn't have posted that you were doing a ping test.

 

Here's how I KNOW it can be done.

 

Fact: PHP has an exec() function that preforms the first arg in the shell.  I used this for my ping test in PHP: exec("ping $ip -c 1 | grep \"100%\"");

 

Fact: Linux has SAMBA connectivity to windows machines.  Though proper configuration and with administrative rights, linux can connect the C: of every windows machine on the domain.

 

Fact: smbmount makes the SAMBA connectivity act like NFS; it allows you to 'mount' external SAMBA shares localy.

 

I knew all this without the need to do research.  What I need YOU to do is figure out how to combine them, and write a script that will use exec() to make a temporary smbmount for that ip address. Then it'll go into the Documents and Settings folder, and return a directory listing.  Then it will disconnect from the share.  Place that in a for loop from 1-255, and it's done.

 

I was hoping that PHP had a built-in function, but the dude above says it does not.  I don't even entirely trust him, but if you couldn't find anything and he couldn't find anything, then I guess we'll just have to write the hack as listed above."

 

Any comments that could prove helpful would be awesome.

Link to comment
Share on other sites

Okay I'm new here and looking for an answer that will hopefully be available hehe. Here is the problem in our work we developed a program to test ipaddresses and return whether they were used or not so that we could find which IP addresses were available, but we need to add on to this program. We need it to, if that ipaddress returns available, access that ipaddress login through the windows login with a set admin login and password and then access the c drive to see which users have been on that computer or to somehow return the folders in there so we know the names of the users that have accessed it. Any sort of information would be helpful. Thanks in advance.

 

http://www.phpfreaks.com/forums/index.php/topic,126306.0.html

Link to comment
Share on other sites

If only everyone could lack as much tact as jesi seems to. Oh well, I guess it's just a gift.

 

If you're going to do network administration, why not use something more suited?

 

Just because you have the slight chance of weezling out a project with one language it doesn't mean it could have been done much better a different way.

 

Here's where I could make examples of making games in Fortran, but I think The point is clear enough.

Link to comment
Share on other sites

I chose PHP because it affords the opportunity to run from any computer with a web browser.

 

My knowledge of programming languages is limited to just a few--PHP being one of them.  Could you enlighten me as to just which programing platform would be most reasonable for this task while still keeping the ability to run from any computer without a client piece of software (outside the norm of a web browser)?

Link to comment
Share on other sites

I don't consider the JRE as standard as a web browser.  Any other suggestions?

 

If not, I'll just go ahead and write it in PHP...I'll hope to post the results here.

 

Knock yourself out.  By Java I meant servlets, so the JRE is dependent on the server,  just as PHP is dependent of the version installed on the server.

 

Best of luck,

 

Patrick

Link to comment
Share on other sites

I chose PHP because it affords the opportunity to run from any computer with a web browser.

 

PHP runs server-side and has nothing to do with browsers. This is also where part of your problem lies. Anyway... on to the point....

 

I knew all this without the need to do research.  What I need YOU to do is figure out how to combine them, and write a script that will use exec() to make a temporary smbmount for that ip address. Then it'll go into the Documents and Settings folder, and return a directory listing.  Then it will disconnect from the share.  Place that in a for loop from 1-255, and it's done.

 

Ive never worked with samba, though I know a bit about Linux. I do believe your task is achievable. This is just some pseudo code, if you can find the correct samba commands this may get you started.

 

<?php
  $ips = array(range(1,255);
  foreach($ips as $ip) {
    $fip = '10.1.1.'.str_pad($ip, 3, "0", STR_PAD_LEFT);
    exec("smbmount $fip /mnt ; ls -l /mnt/Doc* > $fip.docsdir ; smbumount /mnt");
  }
?>

 

As I said, its pseudo code so obviously not working. But, in theory this would cycle through all ips in the 10.1.1.* range, mount them temporarily on /mnt, list the contents of the Doc* directory and place this list in a file within the same directory the script is run from with a name like 10.1.1.1.docsdir.

 

Hope this helps some.

Link to comment
Share on other sites

Thanks for all the information. Sorry about the bluntness of my friend, he is very confident in his own abilities and doesn't like to be told that he can't do something when he believes he can. He meant it in the nicest possible way  ;D , if you have any other ideas then feel free to post me and guttersnipe will be checking. And if you would like we can post it when done so to add it to the interwebs as possible hehe.

Link to comment
Share on other sites

I'm sure he does know the commands, he is probably working on it as we speak now that I think about it haha. But he will post the code when it is done so I guess until then I think we are okay. Thanks again for the info, hope to see you all around.

Link to comment
Share on other sites

I finished the script.  It does exactly as I wanted; it mounts computers in the subnet, and returns the users who logged into the computer.

 

Here’s how:

 

First, it mounts the drive:

exec("sudo mount -t smbfs -o username=<DOMAIN ADMIN USERNAME>,password=\"<PASSWORD>\" //$ip/c$ /mnt/pingtest");

 

Then it goes through and grabs each user:

function getUsers() {
$path='/mnt/pingtest/Documents\ and\ Settings/';
$start=strlen($path)-2;
//-N shows raw output (no forward slashes preceding spaces)
//-d and /*/ will return only directories (not files), but it will precede our directories with their full paths...so we have to substr it
//-1 returns a single result per line
exec("ls -N -d $path*/",$ls);
$iSentinel=sizeof($ls);
for($i=0;$i<$iSentinel;$i++) {
	$user=strtolower( substr( $ls[$i] , $start , strlen($ls[$i])-($start+1) ) );
	if( $user!='administrator' && $user!='administrator.<DOMAIN>' && $user!='networkservice' && $user!='localservice' && $user!='all users' && $user!='default user' ) {
		if(!$users)
			$users='';
		else
			$users.=';';
		$users.=$user;
	}
}

 

Finally, it unmounts the computer

exec("sudo umount /mnt/pingtest");

 

Some of you linux-gurus out there would have already noticed that the mount command requires root access.  I used the ‘sudo’ command on the ‘mount’ and ‘umount’ to achieve this task.  The problem with using sudo is that it’s designed so that a USER types it.  As far as I know, you cannot have a script auto-enter the password to execute a sudo command. (You can with gksudo, but I’m running a server without X).  To overcome this, I had to configure sudo for the apache user with the ability to execute ‘/bin/mount’ and ‘/bin/umount/’.  This can be done by using the command ‘visudo’.

 

Execute visudo.  Find the line

root	ALL=(ALL) ALL

Add this line below it

www-data ALL=NOPASSWD: /bin/mount,NOPASSWD: /bin/umount

 

Note: the user www-data may vary depending on the flavor of linux you’re running.  I know that ‘www-data’ is used by Ubuntu, but Gentoo uses ‘apache’.

 

Naturally, there could be security issues by allowing a user to run a command as root without the need to enter a password.

 

All in all, it works smooth--not nearly as horrible as writing a Fortran video game.

 

The purpose of this was to add to a network diagnostic/inventory web-app.  Basically, we wanted to be able to know what ips are taken and which are not.  We wanted to know the computer name that occupies an IP address, and the users on that particular machine.

 

I’m going to post the whole app thus far; maybe someone will google it on and find it useful ;).  The script is in early development, and does not look ‘pretty’, so don’t expect anything more than the functionality listed above.  As genericnumber1 pointed out, this is more of a php hack then it should be; it is not platform independent.  In order to run it, you must be using linux.

 

<?php
$conn=mysql_connect('<SERVER>','<USERNAME>','<PASSWORD>');
$link=mysql_select_db('<DATABASE>',$conn);

function setInfo($ip,$type) {
$ping='ping '.$ip.' -c 1';
if ( exec($ping.' | grep "100%"') ) { //100% packet LOSS
	$active=false;
} else {
	$active=true;
	$nslookup=exec("nslookup $ip | grep \"name =\"");
	if ( $nslookup!==FALSE ) {
		$strpos=strpos($nslookup,'=');
		$name=substr($nslookup,$strpos+2);
		unset($strpos);
	}
	mount($ip);
	$users=getUsers();
	umount();
}
$timestamp=date("y-m-d h:i:s A");
if($type=='insert') {
	mysql_query("INSERT INTO ips (ip,active,name,users,timestamp) VALUES('$ip','$active','$name','$users','$timestamp')",$GLOBALS['conn']);
} else {
	mysql_query("UPDATE ips SET active='$active',name='$name',users='$users',timestamp='$timestamp' WHERE ip='$ip'",$GLOBALS['conn']);
}
}
function getInfo($ip) {
$result=mysql_query("SELECT * FROM ips WHERE ip='$ip'");
$GLOBALS['active']=mysql_result($result,0,'active');
$GLOBALS['name']=mysql_result($result,0,'name');
$GLOBALS['users']=mysql_result($result,0,'users');
$GLOBALS['timestamp']=mysql_result($result,0,'timestamp');
}
function mount($ip) {
exec("sudo mount -t smbfs -o username=<DOMAIN ADMIN USERNAME>,password=\"<PASSWORD>\" //$ip/c$ /mnt/pingtest");
}
function umount() {
exec("sudo umount /mnt/pingtest");
}
function getUsers() {
$path='/mnt/pingtest/Documents\ and\ Settings/';
$start=strlen($path)-2;
//-N shows raw output (no forward slashes preceding spaces)
//-d and /*/ will return only directories (not files), but it will precede our directories with their full paths...so we have to substr it
//-1 returns a single result per line
exec("ls -N -d $path*/",$ls);
$iSentinel=sizeof($ls);
for($i=0;$i<$iSentinel;$i++) {
	$user=strtolower( substr( $ls[$i] , $start , strlen($ls[$i])-($start+1) ) );
	if( $user!='administrator' && $user!='administrator.<DOMAIN>' && $user!='networkservice' && $user!='localservice' && $user!='all users' && $user!='default user' ) {
		if(!$users)
			$users='';
		else
			$users.=';';
		$users.=$user;
	}
}
return $users;
}

if ($_GET['update']) {
if($_GET['update']=='all') {
	for($i=0;$i<256;$i++) {
		$ip=$_GET['subnet'].'.'.$i;
		setInfo($ip,'update');
		header('Location: '.$_SERVER['PHP_SELF'].'?subnet='.$_GET['subnet']);
	}
} else {
	$ip=$_GET['subnet'].'.'.$_GET['update'];
	setInfo($ip,'update');
	header('Location: '.$_SERVER['PHP_SELF'].'?subnet='.$_GET['subnet']);
}
} else if ($_GET['subnet']) {
if (!preg_match("/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/",$_GET['subnet'])) {
	echo "Incorrect format";
} else {
	?>
	<a href="<?=$_SERVER['PHP_SELF']?>?subnet=<?=$_GET['subnet']?>&update=all">UPDATE ALL IN SUBNET</a>
	<table cellpadding="3">
		<tr>
			<td>IP</td>
			<td>State</td>
			<td>Name</td>
			<td>Users</td>
			<td>Current of</td>
			<td>Update</td>
		</tr>
	<?
	for($i=1;$i<255;$i++) {
		?><tr><?
		$ip=$_GET['subnet'].'.'.$i;
		if( mysql_num_rows( mysql_query("SELECT ip FROM <TABLE NAME> WHERE ip='$ip'") )==0 ) {
			setInfo($ip,'insert');
		}
		getInfo($ip);
		if ($active==1)
			$active='<font color="red">Occupied</font>';
		else
			$active='<font color="green">Vacant</font>';
		?>
		<td><?=$ip?></td>
		<td><?=$active?></td>
		<td><?=$name?></td>
		<td><?=$users?></td>
		<td><?=$timestamp?></td>
		<td><a href="<?=$_SERVER['PHP_SELF']?>?subnet=<?=$_GET['subnet']?>&update=<?=$i?>">UPDATE</a></td>
		<?
	}
	?></tr></table><?
}
} else {
?>
	Enter Subnet to search (ex: 192.168.0)
	<form method="get">
		<input type="text" name="subnet"/>
		<input type="submit" value="Begin"/>
	</form>
<?
}
?>

Link to comment
Share on other sites

This is not a question.  This is a follow-up to my previous post.

 

My network has a corrupt DNS, and we needed our pingtest application (see previous post) to detect not only the network reported computer name (nslookup/host), but also the ACTUAL computer as reported by the COMPUTER, not the DNS.  To do this, I had to find a location where windows stores the computer’s name.

 

After much google searching, I repeatedly found the answer to my question: right click on my computer, click properties etc.  Unfortunately, I cannot write an efficient script with mouse coordinates and an rdesktop connection.

 

Eventually, I came across this windows tookit called PsTools.  In this, there is a file called PsExec (  http://www.microsoft.com/technet/sysinternals/utilities/psexec.mspx  ).  This nifty little program allows you to execute commands remotely from a client that act upon the computer you’re logged into.  Basically, I was able to say ‘ipconfig /all’ to a remote connection, and it returns the information of the computer I’ve remoted into.  Because the windows ‘ipconfig /all’ command returns the host name of the computer, BINGO!

 

However, PsExec is a windows executable.  I did not want to emulate it with wine, so I continued my searching to find a linux based alternative called winexe (  http://eol.ovh.org/winexe/  ).

 

The best thing about PsExec and winexe is that they do not require you to install any software.  Somehow, default windows installs support this connection without even the need to drop a single file onto the ‘server’.

 

Using winexe, I was successfully able to obtain the computers actual name:

 

function getRealName($ip) {
        exec('sudo /usr/bin/winexe -U "<DOMAIN>\<USER>%<PASSWORD>" //'.$ip.' "ipconfig /all"',$result);
        return substr( $result[4], strlen($result[4]) - strpos( strrev($result[4]) ,' ') );
}

 

Naturally, as with the previous post, I had to use visudo to make www-data  have NOPASSWD access to the winexe program.  My /etc/sudoers file now looks like:

 

# User privilege specification
root    ALL=(ALL) ALL
www-data ALL= NOPASSWD: /bin/mount,NOPASSWD: /bin/umount,NOPASSWD: /usr/bin/winexe 

 

Hope this is useful to someone; I know it was a difficult for me to find (many “bang-head-against-wall” moments).

 

Also, if anyone knows of a better way to find out this information, please suggest it.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.