Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl -w
- #
- # $Id: om2browse.pl,v 1.2 2009/11/27 10:05:04 raven Exp $
- #
- use strict;
- use NetAddr::IP;
- use Data::Dumper;
- use IO::Socket;
- use Class::Date;
- use Text::Iconv;
- use IPC::Open2;
- my $network = '192.168.10.0/24';
- my $dhcp_server = '192.168.10.2';
- my $dhcp_server_timeout = '1';
- my $dhcp_key = 'keyname keyself';
- my $browser_file = '/var/db/samba/browse.dat';
- my $browser_domain = 'DOMAIN';
- my $browser_head = qq~"$browser_domain" c0001000 "PDC" "$browser_domain"
- "PDC" 408d9b2b "Самый Главный Компьютер" "$browser_domain"
- ~;
- our $dhcp_states = {
- '00:00:00:01' => 'free',
- '00:00:00:02' => 'active',
- '00:00:00:03' => 'expired',
- '00:00:00:04' => 'released',
- '00:00:00:05' => 'abandoned',
- '00:00:00:06' => 'reset',
- '00:00:00:07' => 'backup',
- '00:00:00:08' => 'backup-reserved',
- };
- sub trim_all_chars($$) {
- my($string, $chars) = (shift(), shift());
- return('') unless($string);
- $string =~ s/^[\Q$chars\E]+//;
- $string =~ s/[\Q$chars\E]+$//;
- return($string);
- }
- sub get_dhcp_lease {
- my $host = shift();
- my $timeout = shift();
- my $port = 7911;
- $host = 'localhost' unless(defined($host));
- ($host, $port) = split(/\:/, $host) if($host =~ /\:/);
- my $user_key = shift();
- my $sr_crit = shift();
- my $launcher = qq~server $host
- port $port
- key $user_key
- connect
- new lease
- $sr_crit
- open
- ~;
- my $result = { };
- eval {
- local $SIG{ALRM} = sub {
- die('omshell repsonse time out');
- };
- alarm($timeout);
- my $pid;
- $pid = IPC::Open2::open2(\*ROM, \*WOM, '/usr/local/bin/omshell 2>&1') or die('cannot launch omshell!');
- print(WOM $launcher);
- close(WOM);
- while(my $line = <ROM>) {
- next() if($line =~ /^\>/);
- chomp($line);
- my($key, $value) = (undef, undef);
- if($line =~ /(state) = (.+)/i) {
- if(defined($dhcp_states->{"$2"})) {
- ($key, $value) = ($1, $dhcp_states->{"$2"});
- } else {
- ($key, $value) = ($1, $2);
- };
- } elsif($line =~ /(ip-address) = (.+)/i) {
- my @numbers = split(/\:/, $2);
- foreach(@numbers) {
- $_ = unpack('C', pack('H2', $_));
- };
- my $j = join('.', @numbers);
- ($key, $value) = ($1, $j);
- } elsif(
- ($line =~ /(ends) = (.+)/i) or
- ($line =~ /(starts) = (.+)/i) or
- ($line =~ /(tstp) = (.+)/i) or
- ($line =~ /(tsfp) = (.+)/i) or
- ($line =~ /(cltt) = (.+)/i)
- ) {
- my($s1, $s2) = ($1, $2);
- # возможно, понадобится на 32х битных системах:
- # $s2 = join(':', reverse(split(/\:/, $s2)));
- $s2 =~ s/\://g;
- $s2 = hex($s2);
- $s2 = Class::Date::date($s2);
- ($key, $value) = ($s1, $s2);
- } elsif($line =~ /(.+?) = (.+)/) {
- $key = $1;
- $value = $2;
- };
- $key = trim_all_chars($key, q~'"~);
- $value = trim_all_chars($value, q~'"~);
- $result->{$key} = $value;
- };
- close(ROM);
- waitpid($pid, 0);
- alarm(0);
- };
- $@ && die($@);
- return($result);
- };
- my $subnet = NetAddr::IP->new($network);
- my $counter = $subnet->num();
- my $leases = {};
- while(--$counter >= 0) {
- my $addr = $subnet->nth($counter)->addr();
- my $sock = IO::Socket::INET->new(
- 'PeerAddr' => $addr,
- 'PeerPort' => 139,
- 'Proto' => 'tcp',
- 'Timeout' => 0.01
- ) && do {
- my $lease = get_dhcp_lease(
- $dhcp_server,
- $dhcp_server_timeout,
- $dhcp_key,
- 'set ip-address = ' . $addr
- );
- my $lease_hostname = $lease->{'client-hostname'};
- my $lease_ip_address = $lease->{'ip-address'};
- if(defined($lease_hostname) and defined($lease_ip_address)) {
- if($lease_hostname =~ /\w\w\:\w\w\:\w\w\:/) {
- my $decoded_lease_hostname = '';
- foreach my $letter(split(/\:/, $lease_hostname)) {
- $decoded_lease_hostname .= pack('C', hex($letter));
- };
- my $conv = Text::Iconv->new('cp866', 'utf-8');
- $lease_hostname = $conv->convert($decoded_lease_hostname);
- };
- my $lease_time = 0;
- $lease_time = $lease->{'starts'}->epoch();
- $lease_time = $lease->{'ends'}->epoch();
- my $stored_lease_time = 0;
- if(defined($leases->{"$lease_ip_address"}->{'time'})) {
- my $stored_lease_time = $leases->{"$lease_ip_address"}->{'time'}->epoch();
- }
- if(
- (not defined($stored_lease_time)) or
- (defined($stored_lease_time) and ($lease_time > $stored_lease_time))
- ) {
- $leases->{"$lease_ip_address"} = {
- 'hostname' => $lease_hostname,
- 'time' => $lease_time,
- };
- };
- };
- };
- }
- my $temp_browser_file = "${browser_file}.temp";
- open(R, ">$temp_browser_file") or die("cannot open [$temp_browser_file]\n");
- print(R $browser_head);
- foreach my $ip(keys(%$leases)) {
- my $hostname = $leases->{"$ip"}->{'hostname'};
- print(R qq~"$hostname" 00011003 "" "$browser_domain"\n~);
- };
- close(R);
- rename($temp_browser_file, $browser_file) or die("cannot rename [$temp_browser_file] to [$browser_file]\n");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement