[Date Prev] [Date Index] [Date Next] [Thread Prev] [Thread Index] [Thread Next]

Re: sshconsole

Iain Rae iainr@inf.ed.ac.uk
Thu, 3 Apr 2003 01:39:06 -0800 (PST)


Richard Zinar wrote:
> I've seen references to an sshconsole script - e.g.
>  
> http://www.dice.informatics.ed.ac.uk/doc/comp/dice-conserver-client/ 
> sshconsole.1.html
>  
> But I can't locate a copy of the script itself.   Does someone
> have a copy of the script, or can someone point me to a site
> where I can obtain it?   Thanks ....
>  
> Richard
>  
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> users mailing list
> users@conserver.com
> https://www.conserver.com/mailman/listinfo/users


It's site specific but you can probably hack it around to work. 
Basically it needs a list of conserver servers to search, we use a 
locally designed configuration system called lcfg. you could use a file 
or I have an older version that had that info deployed via nis maps.






-- 
Iain Rae			Tel:01316505202
Computing Officer		JCMB:2418
School of Informatics
The University of Edinburgh
#!/usr/bin/perl   -w
#
# $Header: /home/iainr/work/CVS/sshconsole/sshconsole,v 1.17 2002/05/06 09:47:55 iainr Exp $
# $Author: iainr $
# $Date: 2002/05/06 09:47:55 $ 
# $State: Exp $
#
# Revision Log
# $Log: sshconsole,v $
# Revision 1.17  2002/05/06 09:47:55  iainr
# added check for uid=0
#
# Revision 1.16  2002/04/22 16:37:25  iainr
# scanhosts gets to be usr/sbin
#
# Revision 1.15  2002/04/22 15:58:36  iainr
# rewrote the sshconsole script to connect to one server and scan all of them
# added scanhosts script for deployment on server
#
# Revision 1.14  2002/02/08 17:35:57  iainr
# removed reliance on shinynewlcfg, fixed problem with qxprof output change
#
# Revision 1.13  2001/10/18 09:30:47  iainr
# tidied up the -u type options a bit
#
# Revision 1.12  2001/10/12 13:53:32  iainr
# fix ssh for suns
#
# Revision 1.11  2001/10/12 09:46:24  iainr
# fiddled with a couple of things to do with shinynewlcfg
#
# Revision 1.10  2001/10/10 15:00:25  iainr
# removed all reliance on yp apart from one ypmatch which is needed
# for backwards comaptibility on non ngclient hosts
#
# Revision 1.9  2001/10/10 14:58:47  iainr
# fixed a few bugs, now drop
#
# Revision 1.8  2001/04/23 13:26:53  iainr
# now we check for ssh binary
#
# Revision 1.7  2001/04/06 09:23:38  iainr
# tidied up the error handling and program output a bit.
#
# Revision 1.6  2001/03/14 17:09:18  iainr
# added check for existance of conserver.inf file
#
# Revision 1.5  2001/03/14 16:36:46  iainr
# changed tag for serial ports to conerver.serial_
# to match change to make lcfg a bit more sensible
#
# Revision 1.4  2001/03/14 10:08:04  iainr
# Finished the arguemtn handling and it all seems to work, will call this one
# a first release
#
# Revision 1.3  2001/03/13 19:01:30  iainr
# can now work out which server the host we want to connect to is on
# and do some sensible parsing of the arguments
#
# Revision 1.2  2001/03/12 18:59:54  iainr
# testing updates and have actually written a bit
#
# Revision 1.1.1.1  2001/03/12 18:22:25  iainr
# initial import of sshconsole into CVS
#

use diagnostics;
use strict;
use vars qw($opt_a $opt_A $opt_b $opt_d $opt_D $opt_e $opt_f $opt_F $opt_M $opt_r $opt_s $opt_S $opt_u $opt_v $opt_V $opt_w $opt_x); 
use Getopt::Std;
my $args="";
my $host="";
my $console_server="unset";
my $signo;
my $conserverconf="/etc/conserver.inf";
my $allservers="allservers";
my @serverlist;
my @reversed;
my $error;
my $filler;
my $foo;
my $ssh;
my $sunssh="/usr/local/bin/ssh";
my $linuxssh="/usr/bin/ssh";
my $qxprof="/usr/bin/qxprof";
#
# Process the arguments and build up an argument list for the 
# console command which will be run o the server
#
sub proc_args {
my $args="";
my $interactive=1;
#
# there MUST be a nicer way of doing this
#
	if ($opt_b){
		$args="$args -b \"$opt_b\"";	
		$interactive=0;
	}
	if ($opt_D && !($opt_d)) {
		$args="$args -D ";	
		$interactive=0;
	}

	if ($opt_d) {
		$args="$args -d ";	
		$interactive=0;
	}
		if ($opt_u){
		$args="$args -u ";	
		$interactive=0;
	}
	if ($opt_V){
		$args="$args -V ";
		$interactive=0;
	}
	if ($opt_w){
		$args="$args -w ";
		$interactive=0;
	}
	if ($opt_x){
		$args="$args -x ";
		$interactive=0;
	}
	if ($interactive) {

		if ((!$opt_a) && (!$opt_f) && (!$opt_s)) {
			$args="$args -A ";
		}
		if ($opt_a){
			$args="$args -a ";
		}
		if ($opt_f && !($opt_F)) {
			 $args="$args -f ";
		}

		if ($opt_F) {
			$args="$args -F ";
		}
		if ($opt_s && !($opt_S)) {
			 $args="$args -s ";
		}

		if ($opt_S) {
			$args="$args -S ";
		}
		if ($opt_r) {
			$args="$args -r";
		}
		if ($opt_e) {
			$args="$args -e \"$opt_e\"";
		}

	}

	return $args





}

#
# 
sub which_server {
my $hostname="";
my $count=0;
my $interactive=0;
	if ( $#ARGV == 0){
		$hostname=$ARGV[0];
	}else {
		$hostname=$allservers;
	}
	return $hostname;
	
}

sub get_console {
my $lcfg="";
my $serials;
my $grepline;
my $string="conserver.serial_";
my @line;
my $key;
my $pid;
my $count=0;
my $conserver="unconsolable";
my $hostname=shift (@_);
my $index;
#for each server listed in conserver.servers
#	LINE: foreach $key (@serverlist) {
#		print "Checking $key\n";
#		$pid=open(README, "$ssh -x -t $key /usr/bin/console -M localhost -u |");
#		while (<README>) {
#			chomp;
#			@line=split /\s+/, $_;
#			if ($line[1] eq $hostname){
#				if ($conserver eq "unconsolable") {
#					$conserver=$key;
#				}
#				else {
#					print "multiple instances of $hostname found on $conserver and $key, aborting\n";
#					exit 1;
#				}
#			}
#		}		
#		close (README);
#		
#		next LINE;
#	};


	$index=0; 
	while ( $conserver eq "unconsolable" && $index <= $#serverlist ) {

		
		$pid=open(README, "$ssh -x -t $serverlist[$index] /usr/sbin/scanhosts $hostname  2> /dev/null | ");

		while (<README>){
			#chomp;
			@line=split /:/, $_;
			if ( $line[0] eq "found"){
				$conserver=$line[1];
				chomp($conserver);
			}
		}	
	close README;
		$index++;	
	};
	# so we do an ugly hack and see if conserver is still the default
	if ($conserver eq "unconsolable"){
		print "didn't find anything in maps, falling back to config file\n";
		open (README, "< $conserverconf") or die "$conserverconf does not exist\n";
		close (README);
		open (README, "/bin/grep $hostname $conserverconf |") or die "Can't find grep .... damn it was here a minute ago\n";
		$count=0;
		while(<README>){
			$grepline=$_;
			$count+=1;
		}
		if ($count < 0) {
			die "I found a -ve number of entries for $hostname in   $conserverconf something really bad has happened\n";
		}	
		if ($count > 1) { 
			die "found more than one entry for $hostname in $conserverconf, please check config file\n"; 
		}
		if ( $count == 0 ){
		die "cound not find entry for $hostname in $conserverconf\n"; 
		}
		@line=split /:/,$grepline;
		$conserver=$line[0];
		close README;
	}	
	return $conserver;
}


#
#Main part of script
#
#


getopts('aAb:dDe:fFM:qQrsSuvVwx');
$args=proc_args();
$host=which_server;
if ($< eq 0){
 	print "cannot run as root\n";
	exit 1;
}
#
# ssh is either in /bin/ssh (linux) or /usr/local/bin/ssh (sun)
# we could go looking for it ourselves but we don't know what we'll
# find so let's just check those places
#

if (-f $sunssh) { 
	$ssh=$sunssh;
}
elsif ( -f $linuxssh) {
	$ssh=$linuxssh;
}
else {
	print "cannot find ssh binary\n";
	exit;
}
if ( -e $qxprof ) {
	@serverlist=split /\s+/,`/usr/bin/qxprof -a conserver.servers`;
	
#temp ugli hack for first batch of ngclient pcs
	if ( $serverlist[0] eq "conserver.servers:"){
		@reversed=reverse @serverlist;
		$#reversed--;
		@serverlist=@reversed
	}
	else {
		$serverlist[0] =~ s/servers=//;
	}
	print "Scanning servers for console, please wait\n";
	if ($serverlist[0] eq "<undef>"){
		print "could not find any servers to check\n";
		exit 1;
	}
}else {
	print "Can't find $qxprof Using lcfg NIS map\n";
	@serverlist=split /\s+/,`/usr/bin/ypmatch  *.conserver.servers lcfg`;
}

if ($host eq $allservers ){
#get list of all servers	
		
	print "servers are:@serverlist\n";	
	foreach $console_server (@serverlist) {
		chomp($console_server);
		print "Hosts connected to $console_server.\n";
		if (($signo = system($ssh,"-t","$console_server","/usr/bin/console -M localhost $args"))&= 127) {
			die "program killed by signal $signo\n";
		}
	}
}
else {
	#get host to connect to
	eval {$console_server=get_console($host, @serverlist) };
	if ($@ && $@ =~ /get_console/) {
		print"Cannot determine console server because:\n";
		($filler, $error, $filler) = split /^/m, $@, 3;
		print $error;
		print "Terminating\n";
		exit;
	}
print "connecting to $host on $console_server\n";

	if (($signo = system($ssh,"-t","$console_server","/usr/bin/console -M localhost $args $host")) &= 127) {
		die "program killed by signal $signo\n";
	}
}