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

[cobalt-developers] Re: connecting to cced remotely



Once upon a time, Derek Belrose <derek@xxxxxxxxxxxxx> said:
> Does anyone know if the cce daemon can accept remote connections via tcp/ip? 
> 
> I'm working on a utility to manipulate some custom data and was hoping that I 
> wouldn't have to write a daemon to sit on a port to control cce.

You can use SSH to open a secure connection and execute the cceclient.
Here's a sample perl script (requires the Net::SSH::Perl module, which
requires a bunch of other perl modules) that connects to the CCE via
SSH.
-- 
Chris Adams <cmadams@xxxxxxxxxx>
Systems and Network Administrator - HiWAAY Internet Services
I don't speak for anybody but myself - that's enough trouble.


#!/usr/bin/perl -w
#
# Copyright (c) 2003
#   Chris Adams <cmadams@xxxxxxxxxx>
#
########################################################################
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
########################################################################
#
# Sample program to connect to the Cobalt Configuration Engine on a remote
# RaQ via SSH
#

use Net::SSH::Perl;
use POSIX;
use strict;

# Host to connect to
my $raq = shift @ARGV || die "no RaQ specified\n";

# Get the admin password
my $pass = get_pass ();

# Connect and authenticate
my $ssh = Net::SSH::Perl->new ($raq, ( protocol => 2 ) )
    or die "ssh($raq): $!\n";
$ssh->login ("admin", $pass) or die "login: $!\n";
my @cce = $ssh->open2 ("/usr/sausalito/bin/cceclient");

# Open CCE client and authenticate to it
my @r = cceresp (@cce);
die "Bad banner: ", join ("\n", @r), "\n" if ($r[$#r] !~ /^2/);
@r = ccecmd (@cce, "auth " . "admin" . " " . $pass);
die "no auth: ", join ("\n", @r), "\n" if ($r[$#r] !~ /^2/);

# Find the admin user and print the data
@r = ccecmd (@cce, "find User name = admin");
my ($admin_oid) = map { /^104 OBJECT (\d+)/ } @r;
die "Can't find admin: ", join ("\n", @r), "\n" if (! $admin_oid);
@r = ccecmd (@cce, "get " . $admin_oid);
print join ("\n", grep { /^1/ } @r), "\n";


# Done
@r = ccecmd (@cce, "endkey");
@r = ccecmd (@cce, "bye");


# Send a CCE command
sub ccecmd
{
	my ($read, $write, $cmd) = @_;

	print $write $cmd, "\n";
	cceresp ($read);
}


# Get a CCE response
sub cceresp
{
	my ($read) = @_;

	my @resp = ();
	my $buf = "";
	my $off = 0;
	# Read input as long as we get non-(success|failure) messages
	do {
		sysread ($read, $buf, $off, 8192) or die "sysread: $!\n";
		push @resp, split (/\n/, $buf);
		# The last line may not have been a full line
		if ($buf !~ /\n$/) {
			$buf = pop @resp;
			$off = length ($buf);
		} else {
			$buf = "";
			$off = 0;
		}
	} while ($resp[$#resp] =~ /^[139]/);
	return @resp;
}



sub get_pass
{
	my $msg = shift || "RaQ admin password: ";

	open (TTY, "+< /dev/tty");
	my $fd = select (TTY);
	$| = 1;
	select ($fd);
	my $tty = fileno (TTY);
	print TTY $msg;
	my $t = POSIX::Termios->new;
	$t->getattr ($tty);
	my $l = $t->getlflag;
	my $le = $l & ~(&POSIX::ECHO | &POSIX::ICANON);
	$t->setlflag ($le);
	$t->setattr ($tty, &POSIX::TCSANOW);
	my $pass = <TTY>;
	chomp $pass;
	$t->setlflag ($l);
	$t->setattr ($tty, &POSIX::TCSANOW);
	print TTY "\n";
	die "No password\n" if (! $pass);
	close (TTY);

	return $pass;
}