πŸ“œ ⬆️ ⬇️

Time setting on the switches via SNMP

As we all read in the news. summer / winter time is expected to be a mess.
Having a base of switches in the amount of several thousand, handles are not very common, and you will change their configs.
Therefore, I came up with this bike.
By the way, this bike is not only suitable for setting the time, but also for anything that is configured via SNMP

So.
We have:
1. several thousand switches (I have dlink) about one series. Namely:
DES-1228 / ME
DES-3200
DES-3526
DES-3010G

These switches have the same oid for setting the time, so we will program them in packs of 253 pieces at a time.
')
Subnets:
Never mind. Let it be: 172.16.0.0/24
The subnet size / 24 was processed within 5 minutes, and since our script will have to .php, we need to reconfigure the server so that it does not drop the script after 30 seconds or reduce the size of the subnet being processed at once.

So, our config (config.php):
<? $startip = "172.16.0.2"; $endip = "172.16.0.254"; $ntp1 = "172.16.0.1"; $ntp2 = "172.16.0.1"; $gmt = "540"; $interval = "86400"; $community = "private"; $dissummertime = ".1.3.6.1.4.1.171.12.10.12.1.0"; //i $enantp = ".1.3.6.1.4.1.171.12.10.11.1.0"; //i $ntpupdateoid = ".1.3.6.1.4.1.171.12.10.11.5.0"; //i $ntp1oid = ".1.3.6.1.4.1.171.12.10.11.3.0"; //a> $ntp2oid = ".1.3.6.1.4.1.171.12.10.11.4.0"; //a $gmtoid = ".1.3.6.1.4.1.171.12.10.10.4.0"; //i $saveconfig = ""; ?> 

We will sort through the points:
$ startip - the beginning of our IP range of switches;
$ endip - The last switch in the range;
$ ntp1 β€” A time synchronization server on your network that is accessible to switches;
$ ntp2 - The second time synchronization server on your network available to the switches;
$ gmt - time zone shift, I have it +9, which means I entered 9 * 60 = 540;
$ interval - The period over which switches will update their time. The default value is 720, I set it once a day: 60 * 60 * 24 = 86400;
$ community - password with write access.

Now the script itself (ntp.php):
 <?. //   include_once('config.php'); //  IP     ,     $ip = ip2long($startip); $ip2 = ip2long($endip); //    IP  for($ip;$ip <= $ip2; $ip++) { //   IP     xxx.xxx.xxx.xxx  ,     snmp         $ipdec = long2ip($ip); //       80  $fp = @fsockopen ($ipdec, "80", $errno, $errstr, 2); //    ,      : IP - Error -   ( ) if (!$fp) { echo $ipdec." - Error - $errstr ($errno)<br />"; //     snmp  } else { //    snmpset($ipdec, $community, $dissummertime, "i", "0"); //     ntp snmpset($ipdec, $community, $enantp, "i", "3"); //     snmpset($ipdec, $community, $ntpupdateoid, "i", $interval); //    ntp  snmpset($ipdec, $community, $ntp1oid, "a", $ntp1); //    ntp  snmpset($ipdec, $community, $ntp2oid, "a", $ntp2); //     snmpset($ipdec, $community, $gmtoid, "i", $gmt); //      : IP - OKe echo $ipdec." - OKe<br />"; } } ?> 


After sending these values ​​to the first address pool, you can change the pool to the next one in the config and run the script again.

Script cons:
1. It is necessary to open the script body to change the address pool.
In principle, you can make the input field.
2. I have IP switches located in the ERP database, so the script will take them from there so that you can exclude empty addresses.
3. The script does not send the command to save the switch config, i.e. after reboot (power failure) these settings will not be saved. Fix this error is your homework. Those. you need to find the save oid of the configs and add the snmtset line to the script body.
Well, how to write to decent people: The method does not claim to originality and you can use it as you wish and at your own peril and risk.

PS to save the config you need to send the line: .1.3.6.1.4.1.171.12.1.2.6.0 i 2

Source: https://habr.com/ru/post/152931/


All Articles