当我输入时,我可以看到已连接的客户端,但我希望在新的客户端连接或断开连接时将arp -a
相同的信息输出。kdialog
使用 shell 脚本可以实现吗?
答案1
#!/usr/bin/env perl
use File::Temp 'tempfile';
my $command = 'arp -a';
my (undef, $tmp) = tempfile;
while (1) {
system "$command > $tmp";
sleep 1;
my @diff = split "\n", `bash -c "diff -d -U0 $tmp <($command)"`;
shift @diff; shift @diff; shift @diff; # remove diff header
my (@join, @left);
while (my $line = shift @diff) {
if ($line =~ s/^-//) {
push @left, $line;
} elsif ($line =~ s/^\+//) {
push @join, $line;
} else {
warn "found junk line in diff: $line\n";
}
}
if (@join or @left) {
system "kdialog --passivepopup 'joined: @join\nleft: @left'"
}
}