StartVNCServer.pl

Page last edited hace 2081 días 9 horas
De Solaris-ES
Saltar a: navegación, buscar
#!/usr/bin/perl -w

# *********************************************************************** #
#                                                                         #
#  SCRIPT: srvc (SunRay VNC Control)                                      #
#  AUTHOR: Mariano J. Obarrio Miles <mariano.obarrio@gmail.com>           #
#    DATE: 25/08/10                                                       #
#     REV: 0.1                                                            #
#     LIC: Uso y reproduccion TOTALMENTE LIBRE. Si tienen un seg,         # 
#          envien un mail para saber en que lo estan utilizando.          #
#          Solo por curiosidad ;)                                         #
#                                                                         #
#  PLATFORM: Solaris 10                                                   #
#                                                                         #
#  PROPOSITO: Control de sesion remota de SR via VNC                      #
#                                                                         #
#  ESTE ESCRIPT ES LLAMADO DESDE:                                         #
#                                                                         #
#     Linea de comando                                                    #
#                                                                         #
#                                                                         #
#  EXIT CODES:                                                            #
#                                                                         #
#  NOTE: You must have root privileges to execute this script.            #
#                                                                         #
# Historico:                                                              #
#    31/05/2010 Rev. 0.1                                                  #
#    Mariano ->                                                           #
#                                                                         #
# *********************************************************************** #

#use strict;
#use diagnostics;
#use warnings;
use Data::Dumper;
use Getopt::Std;
use POSIX qw(strftime);

sub listVNCServers
{
   my(%DISPLAY) = @_;
   my $bool = 0;
   $cmd = "/usr/bin/ps -ef |/usr/bin/egrep -i \"x11vnc.*display*\"|/usr/bin/grep -v grep";
   @listado = qx($cmd);
   foreach $l (@listado)
   {
      print " DISP        PID      SO_USR     SR_USR             TERMINAL              IP                    MODELO   VNC\n" if($boo
l == 0);
      $bool = 1;
      chomp($l);
      $l =~ s/^\s+//g;
      $l =~ s/\s+/ /g;
      $l =~ s/(\w+)\s(\w+).*display.:(\w+.\w+)(.*)/$3 $2 $1/g;
      ($disp,$pid,$user) = split(/ /,$l);
      printf("%5s %10s %11s %10s %20s %15s %25s %5s\n",
             $disp,
             $pid,
             $user,
             $DISPLAY{$disp}{user},
             $DISPLAY{$disp}{terminal},
             $DISPLAY{$disp}{ip},
             $DISPLAY{$disp}{model},
             ($DISPLAY{$disp}{status} == 0?"OFF":"ON")
            );
   }
}

sub killSesion
{
   $disp = shift;

   $cmd = "/usr/bin/ps -ef |/usr/bin/egrep -i \"x11vnc.*display :$disp\"|/usr/bin/grep -v grep";
   $pid = qx($cmd);
   chomp($pid);
   $pid =~ s/^\s+//g;
   $pid =~ s/\s+/ /g;
   $pid =~ s/(\w+)\s(\w+)\s(\w+).*/$2/g;
   if(!$pid) { print "Sesion inexistente!\n"; }
   else{ system("/usr/bin/kill -15 $pid 1>/dev/null 2>&1"); print "Sesion $pid Cancelada\n"; }
}

sub showCFGFile
{
  my $cfgFile = "/var/adm/sun/etc/srvc.cfg";
  print "File       = $cfgFile\n";
  open (CFG, $cfgFile) || die "could not open $cfgFile - $!" ;
  while (<CFG>) { print $_; }
  close(CFG);
}

sub redCFGFile
{
  my $cfgFile = "/var/adm/sun/etc/srvc.cfg";

  open (CFG, $cfgFile) || die "could not open $cfgFile - $!" ;
  while (<CFG>)
  {
    chomp;                  # no newline
    s/#.*//;                # no comments
    s/^\s+//;               # no leading white
    s/\s+$//;               # no trailing white
    next unless length;     # anything left?
    my ($var, $value) = split(/\s*=\s*/, $_, 2);
    $ConfigFile{$var} = $value;
  }
  close(CFG);
}

sub genCMD
{
   my($x11vnc, $disp, %DISPLAY) = @_;
   my $parms  = "";
   my($vncport, $rfbport, $httpport) = getVncFreePorts();

   $parms .= "-display :".$DISPLAY{$disp}{display}." ";
   $parms .= "-bg ";
   $parms .= "-quiet ";
   $parms .= "-shared ";
   $parms .= "-forever ";
   $parms .= "-onetile ";
   $parms .= "-scale 2/3 ";
   $parms .= "-rfbport $rfbport ";
   $parms .= "-httpdir /usr/local/share/x11vnc/classes ";
   $parms .= "-httpport $httpport ";
   $parms .= "-gone /opt/SUNWut/bin/utdetach ";
   $parms .= "-passwd ".$ConfigFile{passwd}." ";
   $parms .= "-viewpasswd ".$ConfigFile{viewpasswd}." ";
   $parms .= "-timeout 60 ";
   $parms .= "-auth ".$DISPLAY{$disp}{home}."/.Xauthority";
   return("$x11vnc $parms",$rfbport,$httpport);
}

sub getVncFreePorts
{
  @vncfreeport = `$ConfigFile{path_rc}/findvncports|/usr/bin/egrep "RFBPORT|HTTPPORT"`;
  foreach $port (@vncfreeport)
  {
    chomp($port);
    if($port =~ /RFBPORT/)
    {
       $port =~ s/RFBPORT=//g;
       $rfbport = $port;
       $vncport = $port-5900;
    }
    elsif($port =~ /HTTPPORT/)
    {
       $port =~ s/HTTPPORT=//g;
       $httpport = $port;
    }
  }
  return($vncport,$rfbport,$httpport);
}


sub getUserData
{
   $username = shift;

   $passdata = `/usr/bin/getent passwd $username`;
   chomp($passdata);
   
   # utku0:x:150000:101:KioskSessionServiceUser:/var/opt/SUNWkio/home/utku0:/bin/sh
   my($user,$psw,$id,$grp,$desc,$home,$shell) = split(/:/,$passdata);
   return($home);
}

sub sleepTime
{
   my $st = `$ConfigFile{path_rc}/randwait`;
   return(chomp($st));
} 

sub cuenta
{
  my %hs = @_;
  return(scalar keys %hs);
}

sub cargaSesiones
{
   my %DISPLAY;
   my @ses = qx(/opt/SUNWut/bin/utwho -Hc|grep -v "DISP");
   foreach $session (@ses)
   {
      chomp($session);
      $session =~ s/ +/ /g;
      $session =~ s/^ +//g;
      ($disp, $term, $user, $ip, $model) = split(/ /,$session);
      $DISPLAY{$disp} = { 
                         terminal => $term,
                         display  => $disp,
                         user     => $user,
                         ip       => $ip,
                         model    => $model,
                         status   => 0,
                         home     => getUserData($user)
                      };
   }
   return(%DISPLAY);
}

sub chkVNCStarted
{
   my %DISPLAY = @_;
   my @VNCStarted = `/usr/bin/ps -efo 'args' |/usr/bin/egrep -i x11vnc|/usr/bin/grep -v grep`;
   foreach $display (@VNCStarted)
   {
      chomp($display);
      $display =~ s/^.*-display //;
      $display =~ s/(\s).*$/$1/;
      $display =~ s/[\s|\:]//g;
      $DISPLAY{$display}{status} = 1 if(exists($DISPLAY{$display}));
   }
   return(%DISPLAY);
}

sub listaSesiones
{
   my %DISPLAY = @_;
   my $bool = 0;
   foreach $disp (keys %DISPLAY)
   {
      printf("DISP       USER             TERMINAL              IP                     MODEL   VNC\n") if($bool == 0);
      printf("%4s %10s %20s %15s %25s %5s\n",
              $disp,
              $DISPLAY{$disp}{user},
              $DISPLAY{$disp}{terminal},
              $DISPLAY{$disp}{ip},
              $DISPLAY{$disp}{model},
              ($DISPLAY{$disp}{status} == 0?"OFF":"ON")
            );
      $bool = 1;
   }
}

sub startVNCServer
{
   my ($disp, %DISPLAY) = @_;
   if(exists($DISPLAY{$disp}{status}) && $DISPLAY{$disp}{status} == 0)
   {
     my $hostname = `hostname`;
     chomp($hostname);
     my $server = `/usr/bin/getent hosts $hostname|awk '{print \$1}'`;
     chomp($server);
     
     my ($cmd,$rfbport,$httpport) = &genCMD("$ConfigFile{path_vnc}/x11vnc",$disp,%DISPLAY);
     system("$cmd 1>/dev/null 2>&1");
     print "Terminal   : $DISPLAY{$disp}{terminal}\n";
     print "VNC Viewer : $server:".($rfbport-5900)."\n";
     print "WEB        : http://$server:$httpport\n";
   }
}

sub usage{
        print "Usage:\n";
        print "\n";
        print "usage: $0 [-l | -d X.X {-s|-x|-k}]\n";
        print "   -h: Help\n";
        print "   -d: Display\n";
        print "   -s: Start VNC\n";
        print "   -x: Status Sesion\n";
        print "   -l: Listar Sesiones\n";
        print "   -c: Ver config file\n";
        print "   -k: Kill sesion\n";
        exit;
}


getopts("1ckhsxld:", \%args);

if($args{h}){ usage(); exit; }
if(($args{s} || $args{x} || $args{k}) && (!$args{d}) ){ usage(); exit; }
if( $args{c} ){ showCFGFile(); exit; }

eval
{
   redCFGFile();
   my %DISPLAY = &chkVNCStarted(&cargaSesiones());

   if(!%DISPLAY)   { print "No hay sesiones activas\n"; exit; }
   if($args{l})    { &listaSesiones(%DISPLAY); }
   elsif($args{s}) 
   { 
      if($DISPLAY{$args{d}}{status} == 0) 
      { 
          &startVNCServer($args{d}, %DISPLAY); 
      }
      else
      { 
          print "El VNC Server actualmente esta en [".($DISPLAY{$disp}{status} == 0?"OFF":"ON")."] para el display: [$args{d}]\n";
          exit;
      }
   }
   elsif($args{x}) { print "El VNC Server esta en [".($DISPLAY{$disp}{status} == 0?"OFF":"ON")."] para el display: [$args{d}]\n"; }
   elsif($args{k}) { &killSesion($args{d}); }
   elsif($args{1}) { &listVNCServers(%DISPLAY); }
   else            { usage(); exit; }
}
;
  • /var/adm/sun/etc/srvc.cfg
viewpasswd = xxxxxxxx
passwd     = yyyyyyyy
path_vnc   = /usr/local/bin
path_rc    = /opt/SRSS-Addons/RemoteControl/bin