main.pm 20 KB

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