main.pm 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  1. package eyelib::main;
  2. #
  3. # Copyright (C) Roman Dmitiriev, rnd@rajven.ru
  4. #
  5. use utf8;
  6. use open ":encoding(utf8)";
  7. use strict;
  8. use English;
  9. use FindBin '$Bin';
  10. use lib "/opt/Eye/scripts";
  11. use base 'Exporter';
  12. use vars qw(@EXPORT @ISA);
  13. use eyelib::config;
  14. use Socket;
  15. use IO::Select;
  16. use IO::Handle;
  17. use Crypt::CBC;
  18. use MIME::Base64;
  19. our @ISA = qw(Exporter);
  20. our @EXPORT = qw(
  21. eye_version
  22. log_file
  23. write_to_file
  24. wrlog
  25. log_session
  26. log_warning
  27. log_info
  28. log_debug
  29. log_error
  30. log_verbose
  31. log_die
  32. in_array
  33. timestamp
  34. do_exec
  35. do_exec_ref
  36. do_exit
  37. hash_to_kv_csv
  38. sendEmail
  39. IsNotRun
  40. IsMyPID
  41. Add_PID
  42. Remove_PID
  43. IsNotLocked
  44. IsMyLock
  45. Add_Lock
  46. Remove_Lock
  47. DefHash
  48. read_file
  49. uniq
  50. strim
  51. trim
  52. is_integer
  53. is_float
  54. run_in_parallel
  55. translit
  56. crypt_string
  57. decrypt_string
  58. netdev_set_auth
  59. );
  60. BEGIN
  61. {
  62. our $eye_version = "2.4.14";
  63. #---------------------------------------------------------------------------------------------------------
  64. sub log_file {
  65. return if (!$_[0]);
  66. return if (!$_[1]);
  67. return if (!$_[2]);
  68. open (LG,">>$_[0]") || die("Error open log file $_[0]!!! die...");
  69. my ($sec,$min,$hour,$mday,$mon,$year) = (localtime())[0,1,2,3,4,5];
  70. $mon += 1; $year += 1900;
  71. my @msg = split("\n",$_[2]);
  72. foreach my $row (@msg) {
  73. next if (!$row);
  74. printf LG "%04d%02d%02d-%02d%02d%02d %s [%d] %s\n",$year,$mon,$mday,$hour,$min,$sec,$_[1],$$,$row;
  75. }
  76. close (LG);
  77. if ($< ==0) {
  78. my $uid = getpwnam $log_owner_user;
  79. my $gid = getgrnam $log_owner_user;
  80. if (!$gid) { $gid=getgrnam "root"; }
  81. if (!$uid) { $uid=getpwnam "root"; }
  82. chown $uid, $gid, $_[0];
  83. chmod oct("0660"), $_[0];
  84. }
  85. }
  86. #---------------------------------------------------------------------------------------------------------
  87. sub write_to_file {
  88. return if (!$_[0]);
  89. return if (!$_[1]);
  90. my $f_name = shift;
  91. my $cmd = shift;
  92. my $append = shift;
  93. if ($append) {
  94. open (LG,">>$f_name") || die("Error open file $f_name!!! die...");
  95. } else {
  96. open (LG,">$f_name") || die("Error open file $f_name!!! die...");
  97. }
  98. binmode(LG,':utf8');
  99. if (ref($cmd) eq 'ARRAY') {
  100. foreach my $row (@$cmd) {
  101. next if (!$row);
  102. print LG $row."\n";
  103. }
  104. } else {
  105. my @msg = split("\n",$cmd);
  106. foreach my $row (@msg) {
  107. next if (!$row);
  108. print LG $row."\n";
  109. }
  110. }
  111. close (LG);
  112. }
  113. #---------------------------------------------------------------------------------------------------------
  114. sub wrlog {
  115. my $level = shift;
  116. my $string = shift;
  117. my $PRN_LEVEL = 'INFO:';
  118. if ($level == $W_INFO) { log_info($string); }
  119. if ($level == $W_ERROR) { $PRN_LEVEL = 'ERROR:'; log_error($string); }
  120. if ($level == $W_DEBUG) { $PRN_LEVEL = 'DEBUG'; log_debug($string); }
  121. my @msg = split("\n",$string);
  122. foreach my $row (@msg) {
  123. next if (!$row);
  124. print $PRN_LEVEL.' '.$row."\n";
  125. }
  126. }
  127. #---------------------------------------------------------------------------------------------------------
  128. sub log_session { log_file($LOG_COMMON,"SESSION:",$_[0]) if ($log_enable); }
  129. #---------------------------------------------------------------------------------------------------------
  130. sub log_info { log_file($LOG_COMMON,"INFO:",$_[0]) if ($log_enable); }
  131. #---------------------------------------------------------------------------------------------------------
  132. sub log_verbose { log_file($LOG_COMMON,"VERBOSE:",$_[0]) if ($log_enable); }
  133. #---------------------------------------------------------------------------------------------------------
  134. sub log_warning { log_file($LOG_COMMON,"WARN:",$_[0]) if ($log_enable); }
  135. #---------------------------------------------------------------------------------------------------------
  136. sub log_debug { log_file($LOG_DEBUG,"DEBUG:",$_[0]) if $debug; }
  137. #---------------------------------------------------------------------------------------------------------
  138. sub log_error { log_file($LOG_ERR,"ERROR:",$_[0]) if ($log_enable); }
  139. #---------------------------------------------------------------------------------------------------------
  140. sub log_die {
  141. wrlog($W_ERROR,$_[0]);
  142. my $worktime = time()-$BASETIME;
  143. log_info("Script work $worktime sec.");
  144. sendEmail("$HOSTNAME - $MY_NAME die! ","Process: $MY_NAME aborted with error:\n$_[0]");
  145. die ($_[0]);
  146. }
  147. #---------------------------------------------------------------------------------------------------------
  148. sub timestamp {
  149. my $worktime = time()-$BASETIME;
  150. #print "TimeStamp: $worktime sec.\n";
  151. log_info("TimeStamp: $worktime sec.");
  152. }
  153. #---------------------------------------------------------------------------------------------------------
  154. sub in_array {
  155. my $arr = shift;
  156. my @tmp = ();
  157. if (ref($arr)=~'ARRAY') { @tmp = @{$arr}; } else { push(@tmp,$arr); }
  158. my $value = shift;
  159. my %num = map { $_, 1 } @tmp;
  160. return $num{$value} || 0;
  161. }
  162. #---------------------------------------------------------------------------------------------------------.
  163. sub hash_to_kv_csv {
  164. my ($hash_ref, $delimiter) = @_;
  165. $delimiter ||= ',';
  166. return '' unless $hash_ref && %$hash_ref;
  167. # Экранируем специальные символы
  168. my $escape = sub {
  169. my $value = shift;
  170. return '' unless defined $value;
  171. # Если значение содержит кавычки или запятые - заключаем в кавычки
  172. if ($value =~ /["$delimiter]/) {
  173. $value =~ s/"/""/g;
  174. return '"' . $value . '"';
  175. }
  176. return $value;
  177. };
  178. # Формируем пары ключ=>значение
  179. my @pairs;
  180. while (my ($key, $value) = each %$hash_ref) {
  181. push @pairs, $escape->($key) . '=>' . $escape->($value);
  182. }
  183. return join($delimiter, @pairs);
  184. }
  185. #---------------------------------------------------------------------------------------------------------.
  186. sub do_exec_ref {
  187. my $ret = `$_[0] 2>&1`;
  188. my $res = $?;
  189. my %result;
  190. chomp($ret);
  191. $result{output}=$ret;
  192. $result{status}=$res;
  193. log_debug("Run: $_[0] Output:\n$ret\nResult code: $res");
  194. if ($res eq "0") { log_info("Run: $_[0] - $ret"); } else { log_error("Run: $_[0] - $ret"); }
  195. return %result;
  196. }
  197. #---------------------------------------------------------------------------------------------------------
  198. sub do_exec {
  199. my $ret = `$_[0]`;
  200. my $res = $?;
  201. log_debug("Run: $_[0] Output:\n$ret\nResult code: $res");
  202. if ($res eq "0") {
  203. log_info("Run: $_[0] - $ret");
  204. } else {
  205. $ret = "Error";
  206. log_error("Run: $_[0] - $ret");
  207. }
  208. return $ret;
  209. }
  210. #---------------------------------------------------------------------------------------------------------
  211. sub do_exit {
  212. my $worktime = time()-$BASETIME;
  213. my $code;
  214. if ($_[0]) { $code = $_[0]; } else { $code = 0; }
  215. log_info("Script work $worktime sec. Exit code: $code");
  216. exit $code;
  217. }
  218. #---------------------------------------------------------------------------------------------------------
  219. sub sendEmail {
  220. my ($subject, $message, $use_br) = @_;
  221. return unless $send_email;
  222. # Validate email addresses
  223. unless ($sender_email =~ /\A[^@\s]+@[^@\s]+\z/) {
  224. log_error("Invalid sender email address: $sender_email");
  225. return;
  226. }
  227. unless ($admin_email =~ /\A[^@\s]+@[^@\s]+\z/) {
  228. log_error("Invalid admin email address: $admin_email");
  229. return;
  230. }
  231. # Sanitize input
  232. $subject =~ s/[^\p{L}\p{N}\s\-\.\,\!\?]//g;
  233. $message =~ s/\r//g; # Remove carriage returns
  234. my $sendmail = '/usr/sbin/sendmail';
  235. unless (-x $sendmail) {
  236. log_error("Sendmail not found or not executable at $sendmail");
  237. return;
  238. }
  239. # Build email headers
  240. my $headers = <<"END_HEADERS";
  241. From: $sender_email
  242. To: $admin_email
  243. Subject: $subject
  244. MIME-Version: 1.0
  245. Content-Type: text/html; charset=utf-8
  246. Content-Transfer-Encoding: 8bit
  247. X-Mailer: Perl sendEmail
  248. END_HEADERS
  249. # Build HTML email body
  250. my $html_message = <<"END_HTML";
  251. <!DOCTYPE html>
  252. <html xmlns="http://www.w3.org/1999/xhtml">
  253. <head>
  254. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  255. <title>$subject</title>
  256. </head>
  257. <body>
  258. END_HTML
  259. # Process message lines
  260. my @lines = split("\n", $message);
  261. foreach my $line (@lines) {
  262. $line = htmlspecialchars($line); # HTML escape
  263. $html_message .= $use_br ? "$line<br>\n" : "$line\n";
  264. }
  265. $html_message .= "</body></html>\n";
  266. # Send email
  267. unless (open(MAIL, "|$sendmail -oi -t")) {
  268. log_error("Failed to open sendmail: $!");
  269. return;
  270. }
  271. print MAIL $headers;
  272. print MAIL $html_message;
  273. unless (close(MAIL)) {
  274. log_error("Failed to send email: $!");
  275. return;
  276. }
  277. log_info("Sent email from $sender_email to $admin_email with subject: $subject");
  278. log_debug("Email body:\n$message");
  279. }
  280. #---------------------------------------------------------------------------------------------------------
  281. # Helper function for HTML escaping
  282. sub htmlspecialchars {
  283. my ($text) = @_;
  284. $text =~ s/&/&amp;/g;
  285. $text =~ s/</&lt;/g;
  286. $text =~ s/>/&gt;/g;
  287. $text =~ s/"/&quot;/g;
  288. $text =~ s/'/&#039;/g;
  289. return $text;
  290. }
  291. #---------------------------------------------------------------------------------------------------------
  292. ### Check few run script
  293. sub IsNotRun {
  294. my $pname = shift;
  295. my $lockfile = $pname.".pid";
  296. # if pid file not exists - OK
  297. log_debug("Check what pid file $lockfile exists.");
  298. if (! -e $lockfile) { log_debug("pid file not found. Continue."); return 1; }
  299. open (FF,"<$lockfile") or log_die("can't open file $lockfile: $!");
  300. my $lockid = <FF>;
  301. close(FF);
  302. chomp($lockid);
  303. # If the process ID belongs to the current program - OK
  304. if ($lockid eq $$) { log_debug("pid file found, but owner is this process. Continue. "); return 1; }
  305. # if owner of this process ID not exists - OK
  306. my $process_count = `ps -p $lockid | grep \'$lockid\' | wc -l`;
  307. chomp($process_count);
  308. log_debug("Process count with id $lockid is $process_count");
  309. if ($process_count==0) { log_debug("pid file found, but owner process not found. Remove lock file and continue. "); unlink $lockfile; return 1; }
  310. log_debug("Another proceess with name $MY_NAME pid: $lockid already worked. ");
  311. return 0;
  312. }
  313. #---------------------------------------------------------------------------------------------------------
  314. sub IsMyPID {
  315. my $pname = shift;
  316. my $lockfile = $pname.".pid";
  317. log_debug("Check what pid file $lockfile exists.");
  318. if (! -e $lockfile) { log_debug("pid file not found. Continue."); return 1; }
  319. open (FF,"<$lockfile") or log_die "can't open file $lockfile: $!";
  320. my $lockid = <FF>;
  321. close(FF);
  322. chomp($lockid);
  323. if ($lockid eq $$) { log_debug("pid file is my. continue."); return 1; }
  324. log_debug("Another proceess with name $MY_NAME pid: $lockid already worked. ");
  325. return 0;
  326. }
  327. #---------------------------------------------------------------------------------------------------------
  328. sub Add_PID {
  329. my $pname = shift;
  330. my $lockfile = $pname.".pid";
  331. log_debug("Try create lock file $lockfile");
  332. open (FF,">$lockfile") or log_die "can't open file $lockfile: $!";
  333. flock(FF,2) or log_die "can't flock $lockfile: $!";
  334. print FF $$;
  335. close(FF);
  336. log_debug("Ok.");
  337. return 1;
  338. }
  339. #---------------------------------------------------------------------------------------------------------
  340. sub Remove_PID {
  341. my $pname = shift;
  342. my $lockfile = $pname.".pid";
  343. log_debug("Check what pid file $lockfile exists.");
  344. if (! -e $lockfile) { log_debug("pid file not exists. Continue."); return 1; }
  345. unlink $lockfile or return 0;
  346. log_debug("pid file $lockfile removed.");
  347. return 1;
  348. }
  349. #---------------------------------------------------------------------------------------------------------
  350. sub IsNotLocked {
  351. my $lockfile = $_[0] . ".lock";
  352. log_debug("Check what lock file $lockfile exists.");
  353. if (! -e $lockfile) { log_debug("lock file not found. Continue."); return 1; }
  354. open (FF,"<$lockfile") or log_die "can't open file $lockfile: $!";
  355. my $lockid = <FF>;
  356. close(FF);
  357. chomp($lockid);
  358. if ($lockid eq $$) { log_debug("lock file found, but it is owner is this process. Continue. "); return 1; }
  359. my $process_count = `ps -p $lockid | grep \'$lockid\' | wc -l`;
  360. if ($process_count lt 1) { log_debug("lock file found, but owner process not found. Remove lock file and continue. "); unlink $lockfile; return 1; }
  361. log_debug("Another proceess with pid: $lockid already use $_[0]");
  362. return 0;
  363. }
  364. #---------------------------------------------------------------------------------------------------------
  365. sub IsMyLock {
  366. my $lockfile = $_[0] . ".lock";
  367. log_debug("Check what lock file $lockfile exists.");
  368. if (! -e $lockfile) { log_debug("lock file not found. Continue."); return 0; }
  369. open (FF,"<$lockfile") or log_die "can't open file $lockfile: $!";
  370. my $lockid = <FF>;
  371. close(FF);
  372. chomp($lockid);
  373. if ($lockid eq $$) { log_debug("lock file found, but it is owner is this process. Continue. "); return 1; }
  374. log_debug("file $_[0] used by process with pid: $lockid");
  375. return 0;
  376. }
  377. #---------------------------------------------------------------------------------------------------------
  378. sub Add_Lock {
  379. if (!IsNotLocked($_[0])) { return 0; }
  380. my $lockfile = $_[0] . ".lock";
  381. open (FF,">$lockfile") or log_die "can't open file $lockfile: $!";
  382. flock(FF,2) or log_die "can't flock $lockfile: $!";
  383. print FF $$;
  384. close(FF);
  385. log_debug("Create lock file for $_[0]");
  386. return 1;
  387. }
  388. #---------------------------------------------------------------------------------------------------------
  389. sub Remove_Lock {
  390. if (!IsNotLocked($_[0])) { return 0; }
  391. my $lockfile = $_[0] . ".lock";
  392. if (! -e $lockfile) { return 1; }
  393. unlink $lockfile or return 0;
  394. log_debug("Lock file for $_[0] removed");
  395. return 1;
  396. }
  397. #---------------------------------------------------------------------------------------------------------
  398. sub DefHash {
  399. my $hash=$_[0];
  400. my $num_list = $_[1];
  401. my %num_keys;
  402. if ($num_list) {
  403. my @ret_num = split(' ',$num_list);
  404. %num_keys = map { $_, 1 } @ret_num;
  405. }
  406. foreach my $key (keys %$hash) {
  407. my $null_value = "";
  408. $null_value = 0 if (defined $num_keys{$key});
  409. $hash->{$key}=$null_value if (!defined($hash->{$key}));
  410. }
  411. return $hash;
  412. }
  413. #---------------------------------------------------------------------------------------------------------
  414. sub read_file {
  415. my $filename = shift;
  416. return if (!$filename);
  417. return if (!-e $filename);
  418. open (FF,"<$filename") or die "unable to open file $filename!" ;
  419. my @tmp=<FF>;
  420. close(FF);
  421. chomp(@tmp);
  422. return @tmp;
  423. }
  424. #---------------------------------------------------------------------------------------------------------
  425. sub uniq (\@) {
  426. my @tmp = @{(shift)};
  427. if (scalar(@tmp) eq 0) { return @tmp; }
  428. chomp(@tmp);
  429. my %newlist = map { $_, 1 } @tmp;
  430. return keys %newlist;
  431. }
  432. #---------------------------------------------------------------------------------------------------------
  433. sub strim {
  434. my $str=shift;
  435. return if (!$str);
  436. #$str =~ s/.*[^[:print:]]+//g;
  437. #$str =~ s/[^[:print:]]+//g;
  438. #$str =~ s/[^(a-z|A-Z|0-9|\:|\-|\s|\.)]//g;
  439. #$str =~ s/[:^print:]//g;
  440. $str =~ s/[^[:ascii:]]//g;
  441. $str =~ s/^\s+//g;
  442. $str =~ s/\s+$//g;
  443. return $str;
  444. }
  445. #---------------------------------------------------------------------------------------------------------
  446. sub trim {
  447. my $str=shift;
  448. return if (!$str);
  449. $str =~ s/\n/ /g;
  450. $str =~ s/^\s+//g;
  451. $str =~ s/\s+$//g;
  452. return $str;
  453. }
  454. #---------------------------------------------------------------------------------------------------------
  455. sub is_integer {
  456. defined $_[0] && $_[0] =~ /^[+-]?\d+$/;
  457. }
  458. #---------------------------------------------------------------------------------------------------------
  459. sub is_float {
  460. defined $_[0] && $_[0] =~ /^[+-]?\d+(\.\d+)?$/;
  461. }
  462. #---------------------------------------------------------------------------------------------------------
  463. sub run_in_parallel(\@) {
  464. my @commands = @{(shift)};
  465. my @result = ();
  466. return @result if (!@commands or !scalar(@commands));
  467. my $count = scalar(@commands);
  468. my $start = 0;
  469. while ($start<=$count-1) {
  470. my @run_list=();
  471. my $select = IO::Select->new();
  472. my $stop = $start + $parallel_process_count;
  473. $stop=$count-1 if ($stop >=$count);
  474. for (my $index = $start; $index <=$stop; $index++) {
  475. next if (!$commands[$index]);
  476. my $cmd=$commands[$index];
  477. log_info("Starting ".$cmd);
  478. my ($hchild, $hparent, $childid);
  479. socketpair($hchild, $hparent, AF_UNIX, SOCK_STREAM, PF_UNSPEC) or die "socketpair: $!";
  480. $childid = fork;
  481. die "cannot fork" if($childid == -1);
  482. # redirect child Input|Output
  483. unless($childid) {
  484. open STDIN, "<&", $hparent;
  485. open STDOUT, ">&", $hparent;
  486. open STDERR, ">&", $hparent;
  487. close $hparent;
  488. close $hchild;
  489. $select->remove($_) and close $_ for($select->handles);
  490. exec "/bin/nice -n 15 ".$cmd;
  491. }
  492. close $hparent;
  493. $select->add($hchild);
  494. }
  495. while (my @ready = $select->can_read) {
  496. next if (!@ready or !scalar(@ready));
  497. for my $read(@ready) {
  498. if($read->eof || $read->error) {
  499. # child exit
  500. $select->remove($read);
  501. close $read;
  502. next;
  503. }
  504. if(defined(my $str = <$read>)) {
  505. log_info("Read:".$str);
  506. push(@result,$str);
  507. }
  508. }
  509. }
  510. $start = $stop+1;
  511. }
  512. return (@result);
  513. }
  514. #---------------------------------------------------------------------------------
  515. sub translit {
  516. my $textline=shift;
  517. return if (!$textline);
  518. $textline =~ s/А/A/g; $textline =~ s/а/a/g;
  519. $textline =~ s/Б/B/g; $textline =~ s/б/b/g;
  520. $textline =~ s/В/V/g; $textline =~ s/в/v/g;
  521. $textline =~ s/Г/G/g; $textline =~ s/г/g/g;
  522. $textline =~ s/Д/D/g; $textline =~ s/д/d/g;
  523. $textline =~ s/Е/E/g; $textline =~ s/е/e/g;
  524. $textline =~ s/Ё/E/g; $textline =~ s/ё/e/g;
  525. $textline =~ s/Ж/Zh/g; $textline =~ s/ж/zh/g;
  526. $textline =~ s/З/Z/g; $textline =~ s/з/z/g;
  527. $textline =~ s/И/I/g; $textline =~ s/и/i/g;
  528. $textline =~ s/Й/I/g; $textline =~ s/й/i/g;
  529. $textline =~ s/К/K/g; $textline =~ s/к/k/g;
  530. $textline =~ s/Л/L/g; $textline =~ s/л/l/g;
  531. $textline =~ s/М/M/g; $textline =~ s/м/m/g;
  532. $textline =~ s/Н/N/g; $textline =~ s/н/n/g;
  533. $textline =~ s/О/O/g; $textline =~ s/о/o/g;
  534. $textline =~ s/П/P/g; $textline =~ s/п/p/g;
  535. $textline =~ s/Р/R/g; $textline =~ s/р/r/g;
  536. $textline =~ s/ТС/T-S/g; $textline =~ s/Тс/T-s/g; $textline =~ s/тс/t-s/g;
  537. $textline =~ s/С/S/g; $textline =~ s/с/s/g;
  538. $textline =~ s/Т/T/g; $textline =~ s/т/t/g;
  539. $textline =~ s/У/U/g; $textline =~ s/у/u/g;
  540. $textline =~ s/Ф/F/g; $textline =~ s/ф/f/g;
  541. $textline =~ s/Х/Kh/g; $textline =~ s/х/kh/g;
  542. $textline =~ s/Ц/Ts/g; $textline =~ s/ц/ts/g;
  543. $textline =~ s/Ч/Ch/g; $textline =~ s/ч/ch/g;
  544. $textline =~ s/Ш/Sh/g; $textline =~ s/ш/sh/g;
  545. $textline =~ s/Щ/Shch/g; $textline =~ s/щ/shch/g;
  546. #$textline =~ s/Ь/'/g; $textline =~ s/ь/'/g;
  547. #$textline =~ s/Ъ/''/g; $textline =~ s/ъ/''/g;
  548. $textline =~ s/Ь//g; $textline =~ s/ь//g;
  549. $textline =~ s/Ъ//g; $textline =~ s/ъ//g;
  550. $textline =~ s/Ы/Y/g; $textline =~ s/ы/y/g;
  551. $textline =~ s/Э/E/g; $textline =~ s/э/e/g;
  552. $textline =~ s/Ю/Yu/g; $textline =~ s/ю/yu/g;
  553. $textline =~ s/Я/Ya/g; $textline =~ s/я/ya/g;
  554. return $textline;
  555. }
  556. #---------------------------------------------------------------------------------
  557. sub netdev_set_auth {
  558. my $device = shift;
  559. $device->{login}=$config_ref{router_login} if (!$device->{login});
  560. $device->{password}=$config_ref{router_password} if (!$device->{password});
  561. $device->{password}=decrypt_string($device->{password});
  562. $device->{enable_password}='';
  563. #$device->{enable_password}=$device->{passowrd};
  564. $device->{proto} = 'ssh';
  565. $device->{proto} = 'telnet' if ($device->{protocol} eq '1');
  566. #patch for ssh
  567. if ($device->{proto} eq 'ssh' and exists $switch_auth{$device->{vendor_id}}{proto}) {
  568. #set specified ssh type
  569. if ($switch_auth{$device->{vendor_id}}{proto} =~/ssh/i) {
  570. $device->{proto} = $switch_auth{$device->{vendor_id}}{proto};
  571. }
  572. }
  573. $device->{port} = $device->{control_port} if ($device->{control_port});
  574. $device->{prompt} = qr/[\$#>]\s?$/;
  575. if (exists $switch_auth{$device->{vendor_id}}) {
  576. $device->{prompt} = $switch_auth{$device->{vendor_id}}{prompt} if ($switch_auth{$device->{vendor_id}}{prompt});
  577. }
  578. return $device;
  579. }
  580. #---------------------------------------------------------------------------------
  581. sub decrypt_string {
  582. my $crypted_string = shift;
  583. return if (!$crypted_string);
  584. my $cipher_handle = Crypt::CBC->new(
  585. {
  586. 'key' => $config_ref{encryption_key},
  587. 'cipher' => 'Cipher::AES',
  588. 'iv' => $config_ref{encryption_iv},
  589. 'literal_key' => 1,
  590. 'header' => 'none',
  591. keysize => 128 / 8
  592. }
  593. );
  594. my $result = $cipher_handle->decrypt(decode_base64($crypted_string));
  595. return $result;
  596. }
  597. #---------------------------------------------------------------------------------
  598. sub crypt_string {
  599. my $simple_string = shift;
  600. return if (!$simple_string);
  601. my $cipher_handle = Crypt::CBC->new(
  602. {
  603. 'key' => $config_ref{encryption_key},
  604. 'cipher' => 'Cipher::AES',
  605. 'iv' => $config_ref{encryption_iv},
  606. 'literal_key' => 1,
  607. 'header' => 'none',
  608. keysize => 128 / 8
  609. }
  610. );
  611. my $result = encode_base64($cipher_handle->encrypt($simple_string));
  612. return $result;
  613. }
  614. #---------------------------------------------------------------------------------
  615. #log_file($LOG_COMMON,"INFO:","----------------------------------------------------------------------------------------");
  616. #log_file($LOG_COMMON,"INFO:","Run script $0. Pid: $$ Pid file: $SPID.pid");
  617. #log_file($LOG_COMMON,"INFO:","User uid: $< Effective uid: $>");
  618. #log_file($LOG_COMMON,"INFO:","Status:");
  619. #log_file($LOG_COMMON,"INFO:","Logging enabled: $log_enable");
  620. #log_file($LOG_COMMON,"INFO:","Logging debug: $debug");
  621. 1;
  622. }