Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2019
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.47 KB | None | 0 0
  1. <?php
  2.  
  3. class Task_SNR_2970G implements Task {
  4.     protected $name;
  5.     protected $host;
  6.     protected $storage;
  7.     protected $login;
  8.     protected $password;
  9.     protected $enablePassword;
  10.  
  11.     public function __construct($name, $host, $storage, $login, $password, $enablePassword) {
  12.         $this->name = $name;
  13.         $this->host = $host;
  14.         $this->storage = $storage;
  15.         $this->login = $login;
  16.         $this->password = $password;
  17.         $this->enablePassword = $enablePassword;
  18.     }
  19.  
  20.     public function getLogin() {
  21.         return $this->login;
  22.     }
  23.  
  24.     public function setLogin($login) {
  25.         $this->login = $login;
  26.     }
  27.  
  28.     public function getPassword() {
  29.         return $this->password;
  30.     }
  31.  
  32.     public function setPassword($password) {
  33.         $this->password = $password;
  34.     }
  35.  
  36.     public function getEnablePassword() {
  37.         return $this->enablePassword;
  38.     }
  39.  
  40.     public function setEnablePassword($enablePassword) {
  41.         $this->enablePassword = $enablePassword;
  42.     }
  43.  
  44.     public function getStorage() {
  45.         return $this->storage;
  46.     }
  47.  
  48.     public function setStorage($storage) {
  49.         $this->storage = $storage;
  50.     }
  51.  
  52.     public function getName() {
  53.         return $this->name;
  54.     }
  55.  
  56.     public function setName($name) {
  57.         $this->name = $name;
  58.     }
  59.  
  60.     public function getHost() {
  61.         return $this->host;
  62.     }
  63.  
  64.     public function setHost($host) {
  65.         $this->host = $host;
  66.     }
  67.  
  68.     public function exec() {
  69.         $journal = new Journal();
  70.         $journal->add(new JournalElement(JournalElement::TYPE_INFO, "Начало выполнения задания"));
  71.  
  72.         try {
  73.             $confFileName = $this->getName().".conf";
  74.  
  75.             $telnet = new Telnet($this->getHost());
  76.             if (!$telnet->connect())
  77.                 throw new Exception("Ошибка подключения к ".$this->getHost());
  78.  
  79.             if (!$telnet->readRegExp('Username:'))
  80.                 throw new Exception("Сервер не запросил ввод имени пользователя.");
  81.  
  82.             if (!$telnet->exec($this->getLogin()))
  83.                 throw new Exception("Ошибка отправки имени пользователя.");
  84.  
  85.             if (!$telnet->readRegExp('Password:'))
  86.                 throw new Exception("Сервер не запросил ввод пароля.");
  87.  
  88.             if (!$telnet->exec($this->getPassword()))
  89.                 throw new Exception("Ошибка отправки пароля.");
  90.  
  91.             if (!$telnet->readRegExp('>'))
  92.                 throw new Exception("Ошибка авторизации.");
  93.  
  94.             if (!$telnet->exec('enable'))
  95.                 throw new Exception("Ошибка отправки enable.");
  96.  
  97.             if (!$telnet->readRegExp('password:'))
  98.                 throw new Exception("Сервер на запросил пароль на enable.");
  99.  
  100.             if (!$telnet->exec($this->getEnablePassword()))
  101.                 throw new Exception("Ошибка отправки пароля на enable.");
  102.  
  103.             if (!$telnet->readRegExp('#'))
  104.                 throw new Exception("Ошибка авторизации enable.");
  105.  
  106.             if (!$telnet->exec('copy startup-config tftp: '.Config::tftpdServerIp))
  107.                 throw new Exception("Ошибка отправки комманды на копирование конфигурации.");
  108.  
  109.             if (!$telnet->readRegExp('Destination file name'))
  110.                 throw new Exception("Сервер не запросил имя файла.");
  111.  
  112.             if (!$telnet->exec($confFileName))
  113.                 throw new Exception("Ошибка отправки имени файла.");
  114.  
  115.             if (!$telnet->readRegExp('TFTP:successfully'))
  116.                 throw new Exception("Ошибка передачи файла.");
  117.  
  118.             $telnet->close();
  119.  
  120.             if (@!file_exists(Config::tftpdServerDir."/".$confFileName))
  121.                 throw new Exception("Файл с конфигурацией не найден.");
  122.  
  123.             if (!@file_exists($this->getStorage())){
  124.                 if (@mkdir($this->getStorage(), 0, 1)){
  125.                     $journal->add(new JournalElement(JournalElement::TYPE_INFO, "Директория ".$this->getStorage()." успешно создана."));
  126.                 } else {
  127.                     throw new Exception("Ошибка создания директории ".$this->getStorage());
  128.                 }
  129.             }
  130.  
  131.             if (!@rename(Config::tftpdServerDir."/".$confFileName, $this->getStorage()."/".$confFileName))
  132.                 throw new Exception("Ошибка сохранения файла ".$this->getStorage()."/".$confFileName);
  133.  
  134.         } catch (Exception $e){
  135.             $journal->add(new JournalElement(JournalElement::TYPE_ERROR, $e->getMessage()));
  136.             return new TaskResult(false, $journal);
  137.         }
  138.  
  139.         $journal->add(new JournalElement(JournalElement::TYPE_INFO, "Задание успешно завершено."));
  140.         return new TaskResult(true, $journal);
  141.     }
  142.  
  143.     public function __toString(){
  144.         return
  145.             "name: ".$this->getName()."\n".
  146.             "host: ".$this->getHost()."\n".
  147.             "login: ".$this->getLogin()."\n".
  148.             "password: ".$this->getPassword()."\n".
  149.             "enable: ".$this->getEnablePassword()."\n".
  150.             "storage: ".$this->getStorage();
  151.     }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement