indexClass.php (3303B)
1 <?php 2 include "libs/php-ssh/vendor/autoload.php"; 3 class main { 4 5 ################## 6 ################## 7 # main-functions # 8 ################## 9 ################## 10 11 ############ 12 # getNodes # 13 ############ 14 public static function getNodes($communityID){ 15 if(!empty($communityID)){ 16 $communities_str = file_get_contents("configs/communities.json"); 17 $json_communities = json_decode($communities_str); 18 $communityURL = $json_communities->{$communityID}->{"nodesURL"}; 19 20 $nodes = file_get_contents($communityURL); 21 $nodes_file = fopen("nodes/nodes".$communityID.".json", "w+") or die("Unable to open file!"); 22 fwrite($nodes_file, $nodes); 23 fclose($nodes_file); 24 }else { 25 echo "<b>ERROR!!!!!! | Missing $communityID </b>"; 26 } 27 } 28 29 ############ 30 # parseMAC # 31 ############ 32 public static function parseMAC($nodeName, $communityID){ 33 main::getNodes($communityID); 34 $communities_str = file_get_contents("configs/communities.json"); 35 $json_communities = json_decode($communities_str); 36 if ($json_communities->$communityID->supported == 1) { 37 $nodes_str = file_get_contents("nodes/nodes".$communityID.".json"); 38 $json_nodes = json_decode($nodes_str); 39 foreach($json_nodes->nodes as $nodes){ 40 if($nodes->name == $nodeName){ 41 $nodeMAC = $nodes->id; 42 } 43 } 44 return $nodeMAC; 45 }else{ 46 echo "<b> This Community is not yet supported!!"; 47 } 48 49 } 50 51 ############## 52 # get prefix # 53 ############## 54 public static function getV6Prefix($communityID){ 55 $communities_str = file_get_contents("configs/communities.json"); 56 $json_communities = json_decode($communities_str); 57 $prefix = $json_communities->{$communityID}->{"v6"}; 58 return $prefix; 59 } 60 61 ################# 62 # generate IPv6 # 63 ################# 64 public static function genV6($nodeName, $communityID){ 65 $mac = main::parseMAC($nodeName, $communityID); 66 $prefix = main::getV6Prefix($communityID); 67 68 $mac_array = explode(":", $mac); 69 if(!empty($mac)){ 70 if(!empty($prefix)){ 71 $host_id = ":".$mac_array[0].$mac_array[1].":".$mac_array[2]."ff:fe".$mac_array[3].":".$mac_array[4].$mac_array[5]; 72 $ipv6 = $prefix."0".$host_id; 73 } 74 } 75 if(!empty($ipv6)){ 76 return $ipv6; 77 } 78 79 } 80 81 ########## 82 # runSSH # 83 ########## 84 public static function openConnection($command, $user, $password, $nodeName, $communityID){ 85 $ip = main::genV6($nodeName, $communityID); 86 87 if(!empty($ip)){ 88 if(helper::pingPortOnServer("[".$ip."]","22") == 1){ 89 $localhost = new Ssh\Configuration("[".$ip."]"); 90 $authentication = new Ssh\Authentication\Password($user, $password); 91 $session = new Ssh\Session($ssh, $authentication); 92 $exec = $session->getExec(); 93 echo $exec->run($command); 94 }else{echo "ERROR!!! SSH IS NOT RUNNING ON ROUTER!!!";} 95 } 96 } 97 98 } 99 100 class helper{ 101 #################### 102 #################### 103 # Helper-functions # 104 #################### 105 #################### 106 107 ######################## 108 # Check for SSH-Server # 109 ######################## 110 public static function pingPortOnServer($ip,$port){ 111 $waitTimeoutInSeconds = 1; 112 if($fp = fsockopen($ip,$port,$errCode,$errStr,$waitTimeoutInSeconds)){ 113 return 1; 114 } else { 115 return 0; 116 } 117 fclose($fp); 118 119 120 } 121 122 123 } 124 ?>