[PHP] Pingen + resultaat afdrukken

Pagina: 1
Acties:
  • 3.363 views sinds 30-01-2008

  • imp4ct
  • Registratie: November 2003
  • Laatst online: 29-10 10:59
Ik ben op zoek naar een scriptje in PHP dat kan pingen naar een ingegeven IP en dat mij de ping waarde kan teruggeven.

Nu ik heb al wat hier op't forum gezocht maar niet echt iets gevonden wat ik zoek, ook op php.net kom ik niet echt veel verder. Weet hier iemand of er een functie ofzo bestaat in PHP die dit kan ?
Of moet ik hiervoor meerdere functies gaan aapsreken ?

Thx in advance.

Bedrijf : Webtrix

Foto materiaal:
Nikon D7100 | Nikor AF-S DX 18-105mm | Nikor AF-S 50mm | Nikon SB600


  • Nielsz
  • Registratie: Maart 2001
  • Niet online
exec()
system()

  • imp4ct
  • Registratie: November 2003
  • Laatst online: 29-10 10:59
Heb die functies al bekeken, maar denk niet dat het hiervoor geschikt is.

[ Voor 48% gewijzigd door imp4ct op 08-02-2006 23:02 ]

Bedrijf : Webtrix

Foto materiaal:
Nikon D7100 | Nikor AF-S DX 18-105mm | Nikor AF-S 50mm | Nikon SB600


  • Nielsz
  • Registratie: Maart 2001
  • Niet online
van php.net:

// outputs the username that owns the running php/httpd process
// (on a system with the "whoami" executable in the path)
echo exec('whoami');

  • BtM909
  • Registratie: Juni 2000
  • Niet online

BtM909

Watch out Guys...

imp4ct schreef op woensdag 08 februari 2006 @ 23:01:
[...]


Heb die functies al bekeken, maar denk niet dat het hiervoor geschikt is.
1. Waarom staat dat niet in je TS?
2. En waarom zou het niet geschikt zijn?

Wellicht ten overvloede, maar lees alsjeblieft ook even Welkom in P&W: FAQ en Beleid door. Denk dat je mijn collega's een aardige plezier doet ;)

Ace of Base vs Charli XCX - All That She Boom Claps (RMT) | Clean Bandit vs Galantis - I'd Rather Be You (RMT)
You've moved up on my notch-list. You have 1 notch
I have a black belt in Kung Flu.


  • imp4ct
  • Registratie: November 2003
  • Laatst online: 29-10 10:59
Wel wil niet moeilijk doen, maar de system() functie staat niet altijd aan bij je webhost. Dit meestal om vieligheidsredenen.

BTW ik ken het beleid, etc... Daarom dat ik ook altijd eerst ga zoeken voordat ik hier iets post. Ik weet maar als te goed dat ze hier niet tuk zijn op mensen die hier weer de zoveelste RTFM vraag komen stellen.

Nu om te verduidelijken, wat ik wil terugkrijgen van die "ping" is een numerieke waarde. Als je online gamed heb je een PING zoals ze dat noemen, nu wat ik dus wil doen is dus pingen naar een server en dus die ping te weten komen, niet of de server online of offline is.

[ Voor 76% gewijzigd door imp4ct op 08-02-2006 23:14 ]

Bedrijf : Webtrix

Foto materiaal:
Nikon D7100 | Nikor AF-S DX 18-105mm | Nikor AF-S 50mm | Nikon SB600


Verwijderd

http://www.php.net/sockets > usercomments > "class Net_Ping".

Niets met system/etc, gewoon puur met PHP pingen, werkt veel beter dan via een wrapper naar het OS.

  • Sv3n
  • Registratie: Mei 2002
  • Laatst online: 21:41
ik heb hier een (zelfgeschreven)script liggen die dat voor je doet, tis wel in java(en moet dus als app op de server draaien), weet niet of je geintresseerd bent, maar dit is de output: http://www.sv3n.gotdns.org/stats/

Last.fm
Films!


  • orf
  • Registratie: Augustus 2005
  • Laatst online: 22:14

orf

Uit de comments van php.net

PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php

class Net_Ping
{
  var $icmp_socket;
  var $request;
  var $request_len;
  var $reply;
  var $errstr;
  var $time;
  var $timer_start_time;
  function Net_Ping()
  {
   $this->icmp_socket = socket_create(AF_INET, SOCK_RAW, 1);
   socket_set_block($this->icmp_socket);
  }
  
  function ip_checksum($data)
  {
     for($i=0;$i<strlen($data);$i += 2)
     {
         if($data[$i+1]) $bits = unpack('n*',$data[$i].$data[$i+1]);
         else $bits = unpack('C*',$data[$i]);
         $sum += $bits[1];
     }
     
     while ($sum>>16) $sum = ($sum & 0xffff) + ($sum >> 16);
     $checksum = pack('n1',~$sum);
     return $checksum;
  }

  function start_time()
  {
   $this->timer_start_time = microtime();
  }
  
  function get_time($acc=2)
  {
   // format start time
   $start_time = explode (" ", $this->timer_start_time);
   $start_time = $start_time[1] + $start_time[0];
   // get and format end time
   $end_time = explode (" ", microtime());
   $end_time = $end_time[1] + $end_time[0];
   return number_format ($end_time - $start_time, $acc);
  }

  function Build_Packet()
  {
   $data = "abcdefghijklmnopqrstuvwabcdefghi"; // the actual test data
   $type = "\x08"; // 8 echo message; 0 echo reply message
   $code = "\x00"; // always 0 for this program
   $chksm = "\x00\x00"; // generate checksum for icmp request
   $id = "\x00\x00"; // we will have to work with this later
   $sqn = "\x00\x00"; // we will have to work with this later

   // now we need to change the checksum to the real checksum
   $chksm = $this->ip_checksum($type.$code.$chksm.$id.$sqn.$data);

   // now lets build the actual icmp packet
   $this->request = $type.$code.$chksm.$id.$sqn.$data;
   $this->request_len = strlen($this->request);
  }
  
  function Ping($dst_addr,$timeout=5,$percision=3)
  {
   // lets catch dumb people
   if ((int)$timeout <= 0) $timeout=5;
   if ((int)$percision <= 0) $percision=3;
   
   // set the timeout
   socket_set_option($this->icmp_socket,
     SOL_SOCKET,  // socket level
     SO_RCVTIMEO, // timeout option
     array(
       "sec"=>$timeout, // Timeout in seconds
       "usec"=>0  // I assume timeout in microseconds
       )
     );

   if ($dst_addr)
   {
     if (@socket_connect($this->icmp_socket, $dst_addr, NULL))
     {
     
     } else {
       $this->errstr = "Cannot connect to $dst_addr";
       return FALSE;
     }
     $this->Build_Packet();
     $this->start_time();
     socket_write($this->icmp_socket, $this->request, $this->request_len);
     if (@socket_recv($this->icmp_socket, &$this->reply, 256, 0))
     {
       $this->time = $this->get_time($percision);
       return $this->time;
     } else {
       $this->errstr = "Timed out";
       return FALSE;
     }
   } else {
     $this->errstr = "Destination address not specified";
     return FALSE;
   }
  }
}

$ping = new Net_Ping;
$ping->ping("www.google.ca");

if ($ping->time)
  echo "Time: ".$ping->time;
else
  echo $ping->errstr;

?>


edit: veel te laat 8)7

[ Voor 8% gewijzigd door orf op 09-02-2006 01:11 ]


  • imp4ct
  • Registratie: November 2003
  • Laatst online: 29-10 10:59
Thx, dat is wat ik zocht !!
Fel bedankt !! _/-\o_

[ Voor 6% gewijzigd door imp4ct op 09-02-2006 03:16 ]

Bedrijf : Webtrix

Foto materiaal:
Nikon D7100 | Nikor AF-S DX 18-105mm | Nikor AF-S 50mm | Nikon SB600


  • iH8
  • Registratie: December 2001
  • Laatst online: 17-06-2024

iH8

ter aanvulling; mocht er nog iemand een system() voorbeeldje nodig hebben;

demo: http://www.theworldsend.net/php-ping.php
source: http://www.theworldsend.net/ping_src.php

hede ten dage niet erg netjes maar wel goud van oud :)

[ Voor 3% gewijzigd door iH8 op 09-02-2006 03:41 ]

Aunt bunny is coming to get me!


Verwijderd

orf schreef op donderdag 09 februari 2006 @ 01:11:
Uit de comments van php.net

PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php

class Net_Ping
{
  var $icmp_socket;
  var $request;
  var $request_len;
  var $reply;
  var $errstr;
  var $time;
  var $timer_start_time;
  function Net_Ping()
  {
   $this->icmp_socket = socket_create(AF_INET, SOCK_RAW, 1);
   socket_set_block($this->icmp_socket);
  }
  
  function ip_checksum($data)
  {
     for($i=0;$i<strlen($data);$i += 2)
     {
         if($data[$i+1]) $bits = unpack('n*',$data[$i].$data[$i+1]);
         else $bits = unpack('C*',$data[$i]);
         $sum += $bits[1];
     }
     
     while ($sum>>16) $sum = ($sum & 0xffff) + ($sum >> 16);
     $checksum = pack('n1',~$sum);
     return $checksum;
  }

  function start_time()
  {
   $this->timer_start_time = microtime();
  }
  
  function get_time($acc=2)
  {
   // format start time
   $start_time = explode (" ", $this->timer_start_time);
   $start_time = $start_time[1] + $start_time[0];
   // get and format end time
   $end_time = explode (" ", microtime());
   $end_time = $end_time[1] + $end_time[0];
   return number_format ($end_time - $start_time, $acc);
  }

  function Build_Packet()
  {
   $data = "abcdefghijklmnopqrstuvwabcdefghi"; // the actual test data
   $type = "\x08"; // 8 echo message; 0 echo reply message
   $code = "\x00"; // always 0 for this program
   $chksm = "\x00\x00"; // generate checksum for icmp request
   $id = "\x00\x00"; // we will have to work with this later
   $sqn = "\x00\x00"; // we will have to work with this later

   // now we need to change the checksum to the real checksum
   $chksm = $this->ip_checksum($type.$code.$chksm.$id.$sqn.$data);

   // now lets build the actual icmp packet
   $this->request = $type.$code.$chksm.$id.$sqn.$data;
   $this->request_len = strlen($this->request);
  }
  
  function Ping($dst_addr,$timeout=5,$percision=3)
  {
   // lets catch dumb people
   if ((int)$timeout <= 0) $timeout=5;
   if ((int)$percision <= 0) $percision=3;
   
   // set the timeout
   socket_set_option($this->icmp_socket,
     SOL_SOCKET,  // socket level
     SO_RCVTIMEO, // timeout option
     array(
       "sec"=>$timeout, // Timeout in seconds
       "usec"=>0  // I assume timeout in microseconds
       )
     );

   if ($dst_addr)
   {
     if (@socket_connect($this->icmp_socket, $dst_addr, NULL))
     {
     
     } else {
       $this->errstr = "Cannot connect to $dst_addr";
       return FALSE;
     }
     $this->Build_Packet();
     $this->start_time();
     socket_write($this->icmp_socket, $this->request, $this->request_len);
     if (@socket_recv($this->icmp_socket, &$this->reply, 256, 0))
     {
       $this->time = $this->get_time($percision);
       return $this->time;
     } else {
       $this->errstr = "Timed out";
       return FALSE;
     }
   } else {
     $this->errstr = "Destination address not specified";
     return FALSE;
   }
  }
}

$ping = new Net_Ping;
$ping->ping("www.google.ca");

if ($ping->time)
  echo "Time: ".$ping->time;
else
  echo $ping->errstr;

?>


edit: veel te laat 8)7
Dit is de code waar ik al een tijdje naar op zoek ben !

Ik heb echter één probleem als ik dit in een lus wil gebruiken, om een te zien of onze pda toestellen bereikbaar zijn dan loopt het telkens vast, als ik de @ wegneem bij de socket_recv dan zie ik na 11 pogingen de boodschap : "Fatal error: Maximum execution time of 30 seconds exceeded in C:\Inetpub\wwwroot\test\ping\ping.php on line 97" lijn 97 is bij mij de lijn net na de socket_recv :?

Als ik de timeout tijd wijzig naar 1 sec in plaats van 2 dan lukt het wel om de lus te vervolledigen, dit doet zich ook enkel voor als er verschillende ip adressen niet bereikbaar zijn.

  • TheRookie
  • Registratie: December 2001
  • Niet online

TheRookie

Nu met R1200RT

set_time_limit is waar je naar op zoek bent

  • Creepy
  • Registratie: Juni 2001
  • Laatst online: 30-11 15:10

Creepy

Tactical Espionage Splatterer

Hier op GoT is het de gewoonte voor een vraag die niet zoveel van doen heeft met het originele topic een nieuwe topic te openen. Daarbij komt ook nog dat je door te zoeken op de foutmelding je vrij snel een oplossing kan vinden en je vraag hierdoor eigenlijk geen eigen topic waard is. Een topic van een jaar oud hiervoor kicken is niet echt nodig ;)

"I had a problem, I solved it with regular expressions. Now I have two problems". That's shows a lack of appreciation for regular expressions: "I know have _star_ problems" --Kevlin Henney

Pagina: 1

Dit topic is gesloten.