openvpn 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #!/usr/bin/perl
  2. # Made by Pierre Mavro/Deimosfr <deimos@deimos.fr>
  3. # Licensed under the terms of the GNU GPL v3, or any later version.
  4. # Version: 0.2
  5. # Usage:
  6. # 1. The configuration name of OpenVPN should be familiar for you (home,work...)
  7. # 2. The device name in your configuration file should be fully named (tun0,tap1...not only tun or tap)
  8. # 3. When you launch one or multiple OpenVPN connexion, be sure the PID file is written in the correct folder (ex: --writepid /run/openvpn/home.pid)
  9. use strict;
  10. use warnings;
  11. use utf8;
  12. use Getopt::Long;
  13. my $openvpn_enabled='/dev/shm/openvpn_i3blocks_enabled';
  14. my $openvpn_disabled='/dev/shm/openvpn_i3blocks_disabled';
  15. # Print output
  16. sub print_output {
  17. my $ref_pid_files = shift;
  18. my @pid_files = @$ref_pid_files;
  19. my $change=0;
  20. # Total pid files
  21. my $total_pid = @pid_files;
  22. if ($total_pid == 0) {
  23. print "VPN: down\n"x2;
  24. # Delete OpenVPN i3blocks temp files
  25. if (-f $openvpn_enabled) {
  26. unlink $openvpn_enabled or die "Can't delete $openvpn_enabled\n";
  27. # Colorize if VPN has just went down
  28. print '#FF0000\n';
  29. }
  30. unless (-f $openvpn_disabled) {
  31. open(my $shm, '>', $openvpn_disabled) or die "Can't write $openvpn_disabled\n";
  32. }
  33. exit(0);
  34. }
  35. # Check if interface device is present
  36. my $vpn_found=0;
  37. my $pid;
  38. my $cmd_line;
  39. my @config_name;
  40. my @config_path;
  41. my $interface;
  42. my $current_config_path;
  43. my $current_config_name;
  44. foreach (@pid_files) {
  45. # Get current PID
  46. $pid=0;
  47. open(PID, '<', $_);
  48. while(<PID>) {
  49. chomp $_;
  50. $pid = $_;
  51. }
  52. close(PID);
  53. # Check if PID has been found
  54. if ($pid ==0) {
  55. print "Can't get PID $_: $!\n";
  56. }
  57. # Check if PID is still alive
  58. $cmd_line='/proc/'.$pid.'/cmdline';
  59. if (-f $cmd_line) {
  60. # Get config name
  61. open(CMD_LINE, '<', $cmd_line);
  62. while(<CMD_LINE>) {
  63. chomp $_;
  64. if ($_ =~ /--config\s*(.*\.conf)/) {
  65. # Get interface from config file
  66. $current_config_path = $1;
  67. # Remove unwanted escape chars
  68. $current_config_path =~ s/\x{00}//g;
  69. $interface = 'null';
  70. # Get configuration name
  71. if ($current_config_path =~ /(\w+).conf/) {
  72. $current_config_name=$1;
  73. } else {
  74. $current_config_name='unknow';
  75. }
  76. # Get OpenVPN interface device name
  77. open(CONFIG, '<', $current_config_path) or die "Can't read config file '$current_config_path': $!\n";
  78. while(<CONFIG>) {
  79. chomp $_;
  80. if ($_ =~ /dev\s+(\w+)/) {
  81. $interface=$1;
  82. last;
  83. }
  84. }
  85. close(CONFIG);
  86. # check if interface exist
  87. unless ($interface eq 'null') {
  88. if (-d "/sys/class/net/$interface") {
  89. push @config_name, $current_config_name;
  90. $vpn_found=1;
  91. # Write enabled file
  92. unless (-f $openvpn_enabled) {
  93. open(my $shm, '>', $openvpn_enabled) or die "Can't write $openvpn_enabled\n";
  94. $change=1;
  95. }
  96. }
  97. }
  98. }
  99. }
  100. close(CMD_LINE);
  101. }
  102. }
  103. # Check if PID found
  104. my $names;
  105. my $short_status;
  106. if ($vpn_found == 1) {
  107. $names = join('/', @config_name);
  108. $short_status='up';
  109. } else {
  110. $short_status='down';
  111. $names = $short_status;
  112. }
  113. print "VPN: $names\n";
  114. print "VPN: $short_status\n";
  115. # Print color if there were changes
  116. print "#00FF00\n" if ($change == 1);
  117. exit(0);
  118. }
  119. sub check_opts {
  120. # Vars
  121. my @pid_file=glob '/run/openvpn/*.pid';
  122. # Set options
  123. GetOptions( "help|h" => \&help,
  124. "p=s" => \@pid_file);
  125. print_output(\@pid_file);
  126. }
  127. sub help {
  128. print "Usage: openvpn [-d pid folder files]\n";
  129. print "-d : pid folder files (default /run/openvpn/*.pid)\n";
  130. print "Note: devices in configuration file should be named with their number (ex: tun0, tap1)\n";
  131. exit(1);
  132. }
  133. &check_opts;