Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Task_SNR_2970G implements Task {
- protected $name;
- protected $host;
- protected $storage;
- protected $login;
- protected $password;
- protected $enablePassword;
- public function __construct($name, $host, $storage, $login, $password, $enablePassword) {
- $this->name = $name;
- $this->host = $host;
- $this->storage = $storage;
- $this->login = $login;
- $this->password = $password;
- $this->enablePassword = $enablePassword;
- }
- public function getLogin() {
- return $this->login;
- }
- public function setLogin($login) {
- $this->login = $login;
- }
- public function getPassword() {
- return $this->password;
- }
- public function setPassword($password) {
- $this->password = $password;
- }
- public function getEnablePassword() {
- return $this->enablePassword;
- }
- public function setEnablePassword($enablePassword) {
- $this->enablePassword = $enablePassword;
- }
- public function getStorage() {
- return $this->storage;
- }
- public function setStorage($storage) {
- $this->storage = $storage;
- }
- public function getName() {
- return $this->name;
- }
- public function setName($name) {
- $this->name = $name;
- }
- public function getHost() {
- return $this->host;
- }
- public function setHost($host) {
- $this->host = $host;
- }
- public function exec() {
- $journal = new Journal();
- $journal->add(new JournalElement(JournalElement::TYPE_INFO, "Начало выполнения задания"));
- try {
- $confFileName = $this->getName().".conf";
- $telnet = new Telnet($this->getHost());
- if (!$telnet->connect())
- throw new Exception("Ошибка подключения к ".$this->getHost());
- if (!$telnet->readRegExp('Username:'))
- throw new Exception("Сервер не запросил ввод имени пользователя.");
- if (!$telnet->exec($this->getLogin()))
- throw new Exception("Ошибка отправки имени пользователя.");
- if (!$telnet->readRegExp('Password:'))
- throw new Exception("Сервер не запросил ввод пароля.");
- if (!$telnet->exec($this->getPassword()))
- throw new Exception("Ошибка отправки пароля.");
- if (!$telnet->readRegExp('>'))
- throw new Exception("Ошибка авторизации.");
- if (!$telnet->exec('enable'))
- throw new Exception("Ошибка отправки enable.");
- if (!$telnet->readRegExp('password:'))
- throw new Exception("Сервер на запросил пароль на enable.");
- if (!$telnet->exec($this->getEnablePassword()))
- throw new Exception("Ошибка отправки пароля на enable.");
- if (!$telnet->readRegExp('#'))
- throw new Exception("Ошибка авторизации enable.");
- if (!$telnet->exec('copy startup-config tftp: '.Config::tftpdServerIp))
- throw new Exception("Ошибка отправки комманды на копирование конфигурации.");
- if (!$telnet->readRegExp('Destination file name'))
- throw new Exception("Сервер не запросил имя файла.");
- if (!$telnet->exec($confFileName))
- throw new Exception("Ошибка отправки имени файла.");
- if (!$telnet->readRegExp('TFTP:successfully'))
- throw new Exception("Ошибка передачи файла.");
- $telnet->close();
- if (@!file_exists(Config::tftpdServerDir."/".$confFileName))
- throw new Exception("Файл с конфигурацией не найден.");
- if (!@file_exists($this->getStorage())){
- if (@mkdir($this->getStorage(), 0, 1)){
- $journal->add(new JournalElement(JournalElement::TYPE_INFO, "Директория ".$this->getStorage()." успешно создана."));
- } else {
- throw new Exception("Ошибка создания директории ".$this->getStorage());
- }
- }
- if (!@rename(Config::tftpdServerDir."/".$confFileName, $this->getStorage()."/".$confFileName))
- throw new Exception("Ошибка сохранения файла ".$this->getStorage()."/".$confFileName);
- } catch (Exception $e){
- $journal->add(new JournalElement(JournalElement::TYPE_ERROR, $e->getMessage()));
- return new TaskResult(false, $journal);
- }
- $journal->add(new JournalElement(JournalElement::TYPE_INFO, "Задание успешно завершено."));
- return new TaskResult(true, $journal);
- }
- public function __toString(){
- return
- "name: ".$this->getName()."\n".
- "host: ".$this->getHost()."\n".
- "login: ".$this->getLogin()."\n".
- "password: ".$this->getPassword()."\n".
- "enable: ".$this->getEnablePassword()."\n".
- "storage: ".$this->getStorage();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement