#!/usr/bin/perl -w
#Batch Create SecureCRT Session
#write by luox
#2010-04-11
#MAIL:fine102#163.com
#OICQ:79810841
use strict;
use Getopt::Long;
use Cwd;
use vars qw/$port $user $password/;
my $PROGRAM_NAME =
"cre_sess.pl";
my $VERSION =
"0.1";
my $type =
"lan";
my $port =
"9922";
my $user =
"root";
my $password =
"ea4324f911e4210ab76cf50276d54725";
my $list_file =
"list";
my $help;
my $debug = 0;
my $status = GetOptions (
"type=s" => \$type,
"port=i" => \$port,
"user=s" => \$user,
"password=s" => \$password,
"list_file=s" => \$list_file,
"help" => \$help,
"debug=i" => \$debug,
);
#conver decval to hexval with $port
$port = &convert_dec($port);
my $local = cwd;
chdir $local;
print
"DEBUG MODE\n" if $debug;
print
"\$type is: " . $type .
"\n" if $debug;
print
"\$port is: " . $port .
"\n" if $debug;
print
"\$user is: " . $user .
"\n" if $debug;
print
"\$password is: " . $password .
"\n" if $debug;
print
"\$list_file is: " . $list_file .
"\n" if $debug;
if ($status == 0) {
&print_help;
exit 0;
}
if ($help) {
&print_help;
exit 0;
}
my $session_path =
"$local/session_dir";
my $contents = `cat $local/source.ini`;
my $list = `cat $list_file`;
$list =~ s/^#.*\n
//g; my @list = split(/\n/,$list);
print $contents .
"\n" if $debug;
if ( ! -d $session_path ) {
mkdir $session_path;
print
"$session_path not found,now mkdir \n";
}
foreach my $line (@list) {
print $line .
"\n" if $debug;
my $file_name = "";
my $hostname = "";
my $lan_ip = "";
my $wan_ip = "";
if ( $line =~ m{hostname:(\w+\d+)}i ) {
$hostname = $1;
print $hostname .
"\n" if $debug;
last
if $debug;
}
if ( $line =~ m{lan:(\d+\.\d+\.\d+\.\d+)}i ) {
$lan_ip = $1;
}
if ( $line =~ m{wan:(\d+\.\d+\.\d+\.\d+)}i ) {
$wan_ip = $1;
}
#如果主机名长度,进行补0(便于SecureCrt的排序)
if ( $hostname =~ m{(\w+)(\d{4})}i ) {
} elsif ( $hostname =~ m{(\w+)(\d{3})}i ) {
$hostname =
"$1" .
"0" .
"$2";
} elsif ( $hostname =~ m{(\w+)(\d{2})}i ) {
$hostname =
"$1" .
"00" .
"$2";
} elsif ( $hostname =~ m{(\w+)(\d{1})}i ) {
$hostname =
"$1" .
"000" .
"$2";
}
if ($type =~ m{^lan$}i) {
$file_name =
"($hostname) " . $lan_ip .
".ini";
} elsif ($type =~ m{^wan$}i) {
$file_name =
"($hostname) " . $wan_ip .
".ini";
}
else {
die
"type unkown please input right type: $!\n" }
print $file_name .
"\n" if $debug;
if ($type =~ m{^lan$}i) {
$contents =~ s/\"Hostname\"=\d+\.\d+\.\d+\.\d+/\"Hostname\"=$lan_ip/;
} elsif ($type =~ m{^wan$}i) {
$contents =~ s/\"Hostname\"=\d+\.\d+\.\d+\.\d+/\"Hostname\"=$wan_ip/;
}
else {
die
"type unkown please input right type: $!\n" }
$contents =~ s/\[SSH2\]\s+Port\"=[\dA-Fa-f]+/\[SSH2\] Port\"=$port/;
$contents =~ s/\"Username\"=\w+/\"Username\"=$user/;
$contents =~ s/\"Password\"=\w+/\"Password\"=$password/;
chdir $session_path or die
"can't change directory: $!";
open FH,
"> $file_name" or die
"Cant open $file_name file: $!";
printf FH ($contents);
close FH;
last
if $debug;
}
sub print_help {
printf
"\t (%s) auto create SecureCRT session files,must allocate OPTIONS list/user/port/password \n",$PROGRAM_NAME;
printf
"\t VERSION is %s \n",$VERSION;
printf
"\t--type setting session use lan or wan,default is lan \n";
printf
"\t--port setting session use port,default is 9922 \n";
printf
"\t--user setting session use user,default is root \n";
printf
"\t--password setting ssession use passowrd,default is 111111 \n";
printf
"\t passowrd is use secure encrypt \n";
printf
"\t--list attach to list path \n";
printf
"\t default list file in local directory list \n";
printf
"\t--debug setting debug mode\n";
printf
"\n";
printf
"\t how to use Example as follows \n";
printf
"\t perl cre_sess.pl --port 22 --user nagios --password 8d860cf50f78af1a2833433076d288f3 --list list \n" }
sub convert_dec {
my $port = shift;
my $port_total_len = 8;
$port = sprintf(
"%x",$port);
my $port_len = length($port);
my $bad = $port_total_len - $port_len;
my $hex_port .= 0 x $bad . $port;
return $hex_port;
}