File: dnet.pl

package info (click to toggle)
libnet-libdnet-perl 0.98-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 412 kB
  • sloc: perl: 1,007; ansic: 311; makefile: 9
file content (195 lines) | stat: -rw-r--r-- 4,252 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/perl
#
# $Id: dnet.pl 19 2008-12-06 17:06:25Z gomor $
#
use strict; use warnings;

my $opt = shift || die("Usage");
my $cmd = shift || die("Usage");
my $arg = "@ARGV";

use Net::Libdnet qw(:consts);

   if ($opt =~ /intf/i)  { intf_opt ($cmd, $arg) }
elsif ($opt =~ /route/i) { route_opt($cmd, $arg) }
elsif ($opt =~ /arp/i)   { arp_opt  ($cmd, $arg) }
elsif ($opt =~ /fw/i)    { fw_opt   ($cmd, $arg) }
elsif ($opt =~ /ip/i)    { ip_opt   ($cmd, $arg) }
elsif ($opt =~ /eth/i)   { eth_opt  ($cmd, $arg) }
#else { print_usage() }

#
# intf option handling
#
sub intf_opt {
   my ($cmd) = @_;
   use Net::Libdnet::Intf;
   my $h = Net::Libdnet::Intf->new;
   if ($cmd =~ /show/i) {
      $h->loop(\&intf_print);
   }
   elsif ($cmd =~ /get/i) {
      my $e = $h->get($arg);
      $e && intf_print($e);
   }
   elsif ($cmd =~ /set/i) {
      my $s = $h->set($arg);
      print $s ? "Success\n" : "Failed\n";
   }
   elsif ($cmd =~ /src/i) {
      my $e = $h->getSrc($arg);
      $e && intf_print($e);
   }
   elsif ($cmd =~ /dst/i) {
      my $e = $h->getDst($arg);
      $e && intf_print($e);
   }
   #else {
      #intf_usage();
   #}
}

sub intf_print {
   my ($e, $data) = @_;
   if (ref($e) eq 'HASH') {
      use Net::Libdnet::Entry::Intf;
      $e = Net::Libdnet::Entry::Intf->newFromHash($e);
   }
   print $e->print."\n";
}

#
# route handling
#

sub route_opt {
   my ($cmd) = @_;
   use Net::Libdnet::Route;
   my $h = Net::Libdnet::Route->new;
   if ($cmd =~ /show/i) {
      printf("%-30s %-30s\n", "Destination", "Gateway");
      $h->loop(\&route_print);
   }
   elsif ($cmd =~ /get/i) {
      my $e = $h->get($arg);
      print $e ? "Gateway: $e\n" : "Same subnet\n";
   }
   elsif ($cmd =~ /add/i) {
      my $s = $h->add(split(/ +/, $arg));
      print $s ? "Success\n" : "Failed\n";
   }
   elsif ($cmd =~ /delete/i) {
      my $s = $h->delete(split(/ +/, $arg));
      print $s ? "Success\n" : "Failed\n";
   }
   #else {
      #route_usage();
   #}
}

sub route_print {
   my ($e, $data) = @_;
   printf("%-30s %-30s\n", $e->{route_dst}, $e->{route_gw});
}

#
# arp handling
#

sub arp_opt {
   my ($cmd) = @_;
   use Net::Libdnet::Arp;
   my $h = Net::Libdnet::Arp->new;
   if ($cmd =~ /show/i) {
      $h->loop(\&arp_print);
   }
   elsif ($cmd =~ /get/i) {
      my $e = $h->get($arg);
      print $e ? "link: $e\n" : "No link found\n";
   }
   elsif ($cmd =~ /add/i) {
      my $s = $h->add(split(/ +/, $arg));
      print $s ? "Success\n" : "Failed\n";
   }
   elsif ($cmd =~ /delete/i) {
      my $s = $h->delete(split(/ +/, $arg));
      print $s ? "Success\n" : "Failed\n";
   }
   #else {
      #arp_usage();
   #}
}

sub arp_print {
   my ($e, $data) = @_;
   printf("%s at %s\n", $e->{arp_pa}, $e->{arp_ha});
}

#
# fw handling
#

sub fw_opt {
   my ($cmd) = @_;
   use Net::Libdnet::Fw;
   my $h = Net::Libdnet::Fw->new;
   if ($cmd =~ /show/i) {
      $h->loop(\&fw_print);
   }
   elsif ($cmd =~ /add/i) {
      my $s = $h->add($arg);
      print $s ? "Success\n" : "Failed\n";
   }
   elsif ($cmd =~ /delete/i) {
      my $s = $h->delete($arg);
      print $s ? "Success\n" : "Failed\n";
   }
   #else {
      #fw_usage();
   #}
}

sub fw_print {
   my ($e, $data) = @_;
   my $device = $e->{fw_device};
   my $op     = $e->{fw_op};
   my $dir    = $e->{fw_dir};
   my $proto  = $e->{fw_proto};
   my $src    = $e->{fw_src};
   my $dst    = $e->{fw_dst};
   my $sport  = $e->{fw_sport};
   my $dport  = $e->{fw_dport};
      if ($op == DNET_FW_OP_ALLOW) { $op = "allow" }
   elsif ($op == DNET_FW_OP_BLOCK) { $op = "block" }
      if ($dir == DNET_FW_DIR_IN)  { $dir = "in"  }
   elsif ($dir == DNET_FW_DIR_OUT) { $dir = "out" }
      if ($proto == 6)  { $proto = "tcp"  }
   elsif ($proto == 17) { $proto = "udp"  }
   elsif ($proto == 1)  { $proto = "icmp" }
   $src = $src.':'.join('-', @$sport);
   $dst = $dst.':'.join('-', @$dport);
   print "$op $dir $device $proto $src $dst\n";
}

#
# ip handling
#

sub ip_opt {
   my ($cmd) = @_;
   use Net::Libdnet::Ip;
   my $h = Net::Libdnet::Ip->new;
   $h->send($cmd);
}

#
# eth handling
#

sub eth_opt {
   my ($cmd, $arg) = @_;
   use Net::Libdnet::Eth;
   print "[$cmd] [$arg]\n";
   my $h = Net::Libdnet::Eth->new(device => $cmd);
   $h->send($arg);
}