Jump to content

TomT

Members
  • Posts

    48
  • Joined

  • Last visited

Profile Information

  • Gender
    Not Telling

TomT's Achievements

Member

Member (2/5)

0

Reputation

  1. Thanks using fgets has helped and looks like it could work in this scenario.
  2. Ideally I'd like to do this as a self contained script, not calling any external applications. The code I've got works, but does't display the results one by one..
  3. Hi I'm trying to watch a plain text file for any changes. I want to use PHP and read the file line by line as new lines are added. Some times 1 line may be added, other times dozens of lines could be added. I want to read each line and check if it matches any keywords.. This is what I've got so far, but it's not line by line... it's buffer size based. <?php $file='log.txt'; $line = $lastpos = 0; while (true) { usleep(500000); clearstatcache(false, $file); $len = filesize($file); if ($len < $lastpos) { $lastpos = $len; } elseif ($len > $lastpos) { $f = fopen($file, "rb"); if ($f === false) die(); fseek($f, $lastpos); while (!feof($f)) { $buffer = fread($f, 4096); if ($lastpos > 0) { if (strpos($buffer, 'MATCH') !== false) echo "$line\r\n"; } flush(); $line++; } $lastpos = ftell($f); fclose($f); } } ?> Can anyone help point me in the right direction. Ideally I don't want to keep the log in memory as it could grow to a reasonable size. Thanks
  4. Thanks this is now working and returning the results correctly Array ( [5] => gmail-smtp-in.l.google.com [10] => alt1.gmail-smtp-in.l.google.com [20] => alt2.gmail-smtp-in.l.google.com [30] => alt3.gmail-smtp-in.l.google.com [40] => alt4.gmail-smtp-in.l.google.com ) Thanks much appreciated.
  5. Thanks I've copied the Net_DNS files across and it is working.. This is the test code I'm using: <?php require_once 'Net/DNS.php'; $resolver = new Net_DNS_Resolver(); $response = $resolver->rawQuery('gmail.com', 'MX'); if ($response) { if (count($response->answer)) { foreach ($response->answer as $rr) { $rr->display(); } } } ?> and it returns: gmail.com. 3524 IN MX 30 alt3.gmail-smtp-in.l.google.com. gmail.com. 3524 IN MX 10 alt1.gmail-smtp-in.l.google.com. gmail.com. 3524 IN MX 40 alt4.gmail-smtp-in.l.google.com. gmail.com. 3524 IN MX 20 alt2.gmail-smtp-in.l.google.com. gmail.com. 3524 IN MX 5 gmail-smtp-in.l.google.com. Which is all good, How can I change this so it only returns the preference and the address ? Doing a var_dump on $rr returns : object(net_dns_rr_mx)( { ["name"]=> string(9) "gmail.com" ["type"]=> string(2) "MX" ["class"]=> string(2) "IN" ["ttl"]=> int(3365) ["rdlength"]=> int(32) ["rdata"]=> string(32) "alt3 gmail-smtp-inlgoogleÀ" ["preference"]=> int(30) ["exchange"]=> string(31) "alt3.gmail-smtp-in.l.google.com" } object(net_dns_rr_mx)( { ["name"]=> string(9) "gmail.com" ["type"]=> string(2) "MX" ["class"]=> string(2) "IN" ["ttl"]=> int(3365) ["rdlength"]=> int(9) ["rdata"]=> string(9) " alt1À." ["preference"]=> int(10) ["exchange"]=> string(31) "alt1.gmail-smtp-in.l.google.com" } object(net_dns_rr_mx)( { ["name"]=> string(9) "gmail.com" ["type"]=> string(2) "MX" ["class"]=> string(2) "IN" ["ttl"]=> int(3365) ["rdlength"]=> int(9) ["rdata"]=> string(9) "(alt4À." ["preference"]=> int(40) ["exchange"]=> string(31) "alt4.gmail-smtp-in.l.google.com" } object(net_dns_rr_mx)( { ["name"]=> string(9) "gmail.com" ["type"]=> string(2) "MX" ["class"]=> string(2) "IN" ["ttl"]=> int(3365) ["rdlength"]=> int(9) ["rdata"]=> string(9) "alt2À." ["preference"]=> int(20) ["exchange"]=> string(31) "alt2.gmail-smtp-in.l.google.com" } object(net_dns_rr_mx)( { ["name"]=> string(9) "gmail.com" ["type"]=> string(2) "MX" ["class"]=> string(2) "IN" ["ttl"]=> int(3365) ["rdlength"]=> int(4) ["rdata"]=> string(4) "À." ["preference"]=> int(5) ["exchange"]=> string(26) "gmail-smtp-in.l.google.com" } I've tried to echo $rr['answer']['preference'] and $rr['preference']but that doesn't return anything. Thank again for all your help
  6. Unfortunately I don't have the ability to add any new packages to this box. I'm still think the original script does get the correct data, but is formatting it wrong.. I'll try to find time to day to test this again.
  7. Hi It's Linux based and I've no idea what. There are no tools on it to compile or make a new version of PHP. I was hoping to enable getmxrr as that should be supported by PHP4, but I can't see how to do that. I've just found this, which from the comments sounds like it should work, but it returns nothing for me. http://stackoverflow.com/questions/24965134/raw-socket-dns-request-in-php# Any ideas ? Thanks all help is very much appreciated.
  8. It's only connecting to the internet to send mail, it will be firewalled and port restricted. I'll try the wireshark option, only issue will be how I decode the resulting packets. can getmxrr() be enabled in php.ini, or does it need to be compiled in ? Thanks
  9. Hi. Thanks for the reply. I've running this on a very old closed source box running 4.3.10 that doesn't support getmxrr. I can dumb the box but I'd like to try and use it for one task.. There is a chr(15) in there with is the MX query. When I added var_dump(substr($this->dns_reply, $this->cIx-1, $bytes)); The output was the correct data as individual strings, it seems to be how this is reassembled is wrong in how it joins the data back up. I'd be grateful for any help with this. Thanks
  10. Hi I have an old php4 box that I'm trying to make some use of. I don't have nslookup, php getmxrr etc. I found on the php.net site a function to return the MX records for a domain. In this example I'm using gmail, but I get different results compared to getmxrr(). getmxrr shows: Array ( [0] => alt1.gmail-smtp-in.l.google.com [1] => alt3.gmail-smtp-in.l.google.com [2] => alt2.gmail-smtp-in.l.google.com [3] => gmail-smtp-in.l.google.com [4] => alt4.gmail-smtp-in.l.google.com ) yet this function returns: Array ( [0] => google.com [1] => alt3.google.com [2] => alt4.google.com [3] => alt2.google.com [4] => alt2.google.com.google.com ) The code below shows how I've called getmxrr and the function, can anyone advise if it's possible to get the results to match getmxrr ?? It's querying the same server on port 53. <?php getmxrr ( "gmail.com", $mxhosts); echo "<pre>"; print_r($mxhosts); echo "</pre>"; class mxlookup { var $dns_socket = NULL; var $QNAME = ""; var $dns_packet= NULL; var $ANCOUNT = 0; var $cIx = 0; var $dns_repl_domain; var $arrMX = array(); function mxlookup($domain, $dns="8.8.8.8") { $this->QNAME($domain); $this->pack_dns_packet(); $dns_socket = fsockopen("udp://$dns", 53); fwrite($dns_socket,$this->dns_packet,strlen($this->dns_packet)); $this->dns_reply = fread($dns_socket,1); $bytes = stream_get_meta_data($dns_socket); $this->dns_reply .= fread($dns_socket,$bytes['unread_bytes']); fclose($dns_socket); $this->cIx=6; $this->ANCOUNT = $this->gord(2); $this->cIx+=4; $this->parse_data($this->dns_repl_domain); $this->cIx+=7; for($ic=1;$ic<=$this->ANCOUNT;$ic++) { $QTYPE = ord($this->gdi($this->cIx)); if($QTYPE!==15){print("[MX Record not returned]"); die();} $this->cIx+=9; $mxPref = ord($this->gdi($this->cIx)); $this->parse_data($curmx); $this->arrMX[] = $curmx; $this->cIx+=3; } } function parse_data(&$retval) { $arName = array(); $byte = ord($this->gdi($this->cIx)); while($byte!==0) { if($byte==192) //compressed { $tmpIx = $this->cIx; $this->cIx = ord($this->gdi($cIx)); $tmpName = $retval; $this->parse_data($tmpName); $retval=$retval.".".$tmpName; $this->cIx = $tmpIx+1; return; } $retval=""; $bCount = $byte; for($b=0;$b<$bCount;$b++) { $retval .= $this->gdi($this->cIx); } $arName[]=$retval; $byte = ord($this->gdi($this->cIx)); } $retval=join(".",$arName); } function gdi(&$cIx,$bytes=1) { $this->cIx++; return(substr($this->dns_reply, $this->cIx-1, $bytes)); } function QNAME($domain) { $dot_pos = 0; $temp = ""; while($dot_pos=strpos($domain,".")) { $temp = substr($domain,0,$dot_pos); $domain = substr($domain,$dot_pos+1); $this->QNAME .= chr(strlen($temp)).$temp; } $this->QNAME .= chr(strlen($domain)).$domain.chr(0); } function gord($ln=1) { $reply=""; for($i=0;$i<$ln;$i++){ $reply.=ord(substr($this->dns_reply,$this->cIx,1)); $this->cIx++; } return $reply; } function pack_dns_packet() { $this->dns_packet = chr(0).chr(1). chr(1).chr(0). chr(0).chr(1). chr(0).chr(0). chr(0).chr(0). chr(0).chr(0). $this->QNAME. chr(0).chr(15). chr(0).chr(1); } } /* Exampe of use: */ $mx = new mxlookup("gmail.com"); echo "<pre>"; print_r($mx->arrMX); echo "</pre>"; ?> adding a var_dump to the gdi function is showing the correct DNS entries, so it's something to do with how the script is joining the results back together. function gdi(&$cIx,$bytes=1) { $this->cIx++; var_dump(substr($this->dns_reply, $this->cIx-1, $bytes)); return(substr($this->dns_reply, $this->cIx-1, $bytes)); } anyone any ideas ? Thanks Thanks
  11. Thanks. I'll test that and see what it does for me
  12. Doing this seems to have worked: function custom_sort($a, $b) { return $b['id'] - $a['id']; }
  13. Thanks using usort works perfectly One last question, how easy is it to do the same but sort in reverse order ? Thanks
  14. Hi My array is made using : $group[] = array("ctime"=>$ctime,"name"=>$name that results in : [4]=> array(2) { ["ctime"]=> int(1334597042) ["name"]=> string(7) "John.dat" } [3]=> array(2) { ["ctime"]=> int(1334935095) ["name"]=> string(4) "mark" } [2]=> array(2) { ["ctime"]=> int(1340634768) ["name"]=> string(7) "lol.txt" } [1]=> array(2) { ["ctime"]=> int(1334597041) ["name"]=> string( "dave.txt" } [0]=> array(2) { ["ctime"]=> int(1335193200) ["name"]=> string(4) "paul" } How would I sort this array ascending based on the ctime values ? Thanks
×
×
  • 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.