serial-mikrotik-fix.pl 1.86 KB
Newer Older
1 2 3 4 5 6 7 8 9
#!/usr/bin/perl -CL

use common::sense;
use Data::Dumper;
use IO::Socket::UNIX;

my $client = IO::Socket::UNIX->new( Type => SOCK_STREAM(), Peer => "/var/lib/virt/run/$ARGV[0].serial");
my $data;

Jan Moskyto Matejka's avatar
Jan Moskyto Matejka committed
10 11 12 13
my $debug = 0;
sub D { say @_ if $debug; }
sub S { say "\rmikrotik init ", @_; }

14 15 16 17
$/ = "";

my $state = "login";

Jan Moskyto Matejka's avatar
Jan Moskyto Matejka committed
18 19 20
say "";
S "started";
D "init client";
21 22
syswrite $client, "\r";
while (1) {
Jan Moskyto Matejka's avatar
Jan Moskyto Matejka committed
23
  D "";
24 25 26
  sleep 1;
  sysread $client, $data, 65536;
  $data =~ s/[^ -~]/?/g;
Jan Moskyto Matejka's avatar
Jan Moskyto Matejka committed
27
  D Dumper \$data;
28
  if ($data =~ /MikroTik Login: $/) {
Jan Moskyto Matejka's avatar
Jan Moskyto Matejka committed
29
    S "sending login";
30 31 32 33 34 35
    syswrite $client, "root\r";
    $state = "login";
    next;
  }

  if ($data =~ /assword: $/) {
Jan Moskyto Matejka's avatar
Jan Moskyto Matejka committed
36
    S "sending password";
37 38 39 40 41
    syswrite $client, "root\r";
    next;
  }

  if ($data =~ /tinue!/) {
Jan Moskyto Matejka's avatar
Jan Moskyto Matejka committed
42
    S "continue boilerplate";
43 44 45 46 47 48 49
    syswrite $client, "\r";
#    $state = "export";
    $state = "macreset";
    next;
  }

  if ($data =~ /\[root\@MikroTik\] > $/ && $state eq "export") {
Jan Moskyto Matejka's avatar
Jan Moskyto Matejka committed
50
    D "export";
51 52 53 54 55 56
    syswrite $client, "export\r";
    $state = "macreset";
    next;
  }

  if ($data =~ /\[root\@MikroTik\] > $/ && $state eq "macreset") {
Jan Moskyto Matejka's avatar
Jan Moskyto Matejka committed
57 58
    D "interface ethernet reset-mac-address numbers=0";
    S "reset mac address";
59 60 61 62 63 64
    syswrite $client, "interface ethernet reset-mac-address numbers=0\r";
    $state = "dhcp";
    next;
  }

  if ($data =~ /\[root\@MikroTik\] > $/ && $state eq "dhcp") {
Jan Moskyto Matejka's avatar
Jan Moskyto Matejka committed
65 66
    D "ip dhcp-client renew numbers=0";
    S "flush dhcp";
67 68 69 70 71 72
    syswrite $client, "ip dhcp-client renew numbers=0\r";
    $state = "quit";
    next;
  }

  if ($data =~ /\[root\@MikroTik\] > $/ && $state eq "quit") {
Jan Moskyto Matejka's avatar
Jan Moskyto Matejka committed
73
    D "quit";
74 75 76 77
    syswrite $client, "quit\r";
    sleep 1;
    sysread $client, $data, 65536;
    $data =~ s/[^ -~]/?/g;
Jan Moskyto Matejka's avatar
Jan Moskyto Matejka committed
78 79
    D Dumper \$data;
    S "done", " "x50;
80 81 82 83
    exit 0;
  }

  if ($data =~ /\[root\@MikroTik\] > $/) {
Jan Moskyto Matejka's avatar
Jan Moskyto Matejka committed
84
    D "Logged in, noop now";
85 86 87 88 89
    syswrite $client, "\r";
    $state = "macreset";
    next;
  }

Jan Moskyto Matejka's avatar
Jan Moskyto Matejka committed
90
  D "NOP, strange input";
91 92
}