#!/usr/bin/perl

$login = 'nas-login';
$passwd = 'nas-password';

$host=shift || '';
$type = shift || 'snmp';
$nastype = shift || 'cisco';
$username=shift || '';
$sessionid = shift || '';

$port = 0;
$comm = '';

if ($type eq 'snmp'){
$comm = shift || 'public';
}
if ($type eq 'telnet'){
$port = shift || 0;
}


die "No \$host argument given\n" if ($host eq '');
die "No \$username argument given\n" if ($username eq '');

if ($nastype eq 'cisco' && $type eq 'telnet'){
	die "Usage: clearsession \$host telnet cisco \$username \$sessionid \$port\n" if ($port  == 0);

	if (eval require Net::Telnet::Cisco){
		Net::Telnet::Cisco->import();

		my $session = Net::Telnet::Cisco->new(Host => $host);
		$session->login($login, $passwd);

		if ($port >= 20000){
			my @output = $session->cmd("sh caller user $username");
			foreach $line (@output){
				if ($line =~ /User: $username, line (Vi\d+),/){
					$session->cmd("clear interface $1");
				}
			}
		}
		else{
			$session->cmd("clear line $port\n");
		}

		$session->close;
	}
}
if ($nastype eq 'cisco' && $type eq 'snmp'){

	$SNMPGET="/usr/local/bin/snmpget";
	$SNMPSET="/usr/local/bin/snmpset";

	die "Could not find snmpwalk binary. Please make sure that the \$SNMPGET variable points to the right location\n" if (! -x $SNMPGET);
	die "Could not find snmpset binary. Please make sure that the \$SNMPSET variable points to the right location\n" if (! -x $SNMPSET);
	die "Usage: clearsession \$host snmp \$username cisco \$sessionid \$community\n" if ($sessionid eq '' || $comm eq '');

	if ($sessionid ne '' && $username ne ''){
		print "$SNMPGET -v2c -c $comm $host .iso.org.dod.internet.private.enterprises.9.9.150.1.1.3.1.2.$sessionid\n";
		$walk =`$SNMPGET -v2c -c $comm $host .iso.org.dod.internet.private.enterprises.9.9.150.1.1.3.1.2.$sessionid`;
		unless ($walk =~ /^$/){
			if ($walk =~ /$username/){
				print "FOUND: $username\n";
		`$SNMPSET -v2c -c $comm $host .iso.org.dod.internet.private.enterprises.9.9.150.1.1.3.1.5.$sessionid i 1`;
			}
		}
	}
}
