Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import std.stdio;
- import std.algorithm;
- import std.datetime;
- import std.path;
- import std.process;
- import core.thread;
- import std.conv;
- string currentDateTime() {
- return findSplit(Clock.currTime().toString, ".")[0];
- }
- enum MAX_ATTEMPTS = 3;
- enum SLEEP_TIME = 10;
- enum MAX_WAIT_REBOOT_TIME = 300; //5 минут
- int main(string[] argv) {
- // argv ~= "192.168.1.43";
- void ShowUsage() {
- auto exeName = baseName(argv[0]);
- writeln("Usage: "~exeName~" <IP-address or DNS-name>\n\rExample: "~exeName~" 192.168.1.23");
- }
- if (argv.length != 2) {
- ShowUsage();
- return 1;
- }
- auto ip = argv[1];
- writeln("Ping: "~ip~" | Started: "~currentDateTime);
- auto attempts = MAX_ATTEMPTS;
- auto attemptsMultiplier = 1;
- auto sleepMultiplier = 1;
- while (true) {
- while (attempts > 0){
- core.thread.Thread.sleep(dur!("seconds")(sleepMultiplier * SLEEP_TIME));
- //посылаем 3 пинга, хотя бы один не прошёл - не доступен
- if ((system("ping -n 1 -w 50 "~ip~" > nul") && system("ping -n 1 -w 50 "~ip~" > nul") && system("ping -n 1 -w 50 "~ip~" > nul")) == 0) {
- writeln(currentDateTime~": ping succeed");
- attempts = MAX_ATTEMPTS;
- attemptsMultiplier = 1;
- sleepMultiplier = 1;
- }
- else {
- attempts -= sleepMultiplier;
- if (attempts < (MAX_ATTEMPTS / attemptsMultiplier)) {
- if (sleepMultiplier == 1) {
- writeln(currentDateTime~": ping failed! Will try "~to!string(attempts)~" more times before eject CD.");
- }
- else {
- writeln(currentDateTime~": timeout expired. Re-ejecting CD and wait for boot longer.");
- attemptsMultiplier++;
- break;
- }
- }
- else {
- writeln(currentDateTime~": waiting for boot. Will re-try in "~to!string(attempts * SLEEP_TIME)~" seconds.");
- sleepMultiplier = 5;
- }
- }
- }
- writeln("Ejecting CD-ROM...");
- system(`powershell $cds = (New-Object -ComObject "WMPlayer.OCX").cdromCollection; for($i=0;$i -lt $cds.Count;$i++) { $cds.Item($i).Eject() }`);
- attempts = attemptsMultiplier * (MAX_WAIT_REBOOT_TIME / SLEEP_TIME); //достаточное количество попыток для запуска компа
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement