#!/bin/sh -- # A comment mentioning perl. Indented to force sh eval 'exec perl -S $0 ${1+"$@"}' if 0; $Usage = <<'END'; Usage: findvncports findvncports findvncports - findvncports - END if ($ARGV[0] =~ '^-h') { print $Usage; exit; } $max = shift; $min = 10; if ( $max =~ /-/ ) { ($min, $max) = split(/-/, $max, 2); } $max = 10000 unless $max; $uname = `uname`; if ($uname =~ /Linux/) { open(NETSTAT, "netstat -a -n -t |") || die "netstat: $!"; } else { open(NETSTAT, "netstat -a -n -f inet -P tcp|") || die "netstat: $!"; } while () { if ($uname =~ /Linux/) { ($t, $r, $q, $l, $f) = split(' ', $_); if ( $l =~ /^(\d[\d\.]+):(\d+)/ ) { $ports{$2} = 1; } } else { # Looking for: # 129.148.9.83.56385 .... # *.6020 .... if ( /^(\d[\d\.]+|\s*\*)\.(\d+)\s/ ) { $ports{$2} = 1; } } } close(NETSTAT); for ($n = $min; $n <= $max; $n++) { $disp = $n; $x11port = 6000 + $n; $rfbport = 5900 + $n; $httpport = 5800 + $n; if ( ! $ports{$x11port} && ! $ports{$rfbport} && ! $ports{$httpport} ) { $unix = "/tmp/.X11-unix/X$n"; if ( ! $hestia && -e $unix ) { # print STDERR "skipping $n due do $unix\n"; next; } chomp($host = `hostname`); $host =~ s/\..*$//; print "VNCDISPLAY=$host:$disp\n"; print "X11PORT=$x11port\n"; print "RFBPORT=$rfbport\n"; print "HTTPPORT=$httpport\n"; print "URL=http://$host:$httpport/\n\n"; print "Possible commands to start vncserver:\n\n"; print "vncserver :$disp -nolisten local -httpd $pkgdir/classes\n"; exit 0; } } print STDERR "error: Sorry, could not find free triple of X11, RFB, and HTTP ports.\n"; exit 1;