141c0ae38640a099b64190e3bf66f5e5270f3f2f
[glcas.git] / bin / scanhelper.php
1 <?php
2
3 $WEB_ROOT_FS = realpath(dirname(__FILE__));
4 $BASE_URL = dirname($_SERVER["PHP_SELF"]);
5
6 global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
7
8 // add libglcas as if it were a path in ../libglcas
9 if(file_exists("../libglcas")) {
10         $path = realpath($WEB_ROOT_FS."/../");
11         error_log("added glcas path as $path");
12         set_include_path(get_include_path().PATH_SEPARATOR.$path);
13 }
14
15 // include the based library
16 require_once("libglcas/lib.php");
17
18 // load plugins
19 glcas_pluginLoader();
20
21 // find our config
22 $configpath = "";
23
24 // TODO: do this better
25 $configpath = glcas_getWebConfigPath();
26 $glconfig = new GLCASConfig();
27 $glconfig->loadConfig($configpath);
28
29
30 $range = "";
31 if(isset($argv[1])) {
32         $range = $argv[1];
33 }
34
35 if(isset($argv[2])) {
36         $hostgroup = $argv[2];
37 }
38
39
40 if($range == "") {
41         echo "range makes no sense\n";
42         exit(0);
43 }
44
45
46 $ips_v = explode(".", $range);
47
48 $ips = $ips_v[0].".".$ips_v[1].".".$ips_v[2];
49
50 for($i = 1; $i < 255; $i++) {
51         $hostname = "";
52         $ips_me = "$ips.$i";
53         echo "Scanning $ips_me<br>";
54         error_log("Scanning $ips_me<br>");
55         flush();
56         $hostname = gethostbyaddr($ips_me);
57         if($hostname != $ips_me) {
58                 echo "Found host on $ips_me as $hostname<br>";
59                 error_log("Found host on $ips_me as $hostname");
60                 flush();
61                 $hosts = $glconfig->getData("hosts");
62                 $exists = false;
63                 foreach($hosts as $key => $val) {
64                         if($val["category"] == $hostname && $val["name"] == $ips_me) {
65                                 echo "Host in db already<br>";
66                                 $exists = true;
67                                 flush();
68                         }
69                         
70                 }
71                 if(!$exists) $glconfig->addData("hosts", "$hostname", "$ips_me", "$hostgroup");
72                 
73                 
74         }
75 }
76 echo "Finished";
77
78 ?>