因为在工作中,批量的上架服务器,一次几十上百台,如果手动一台一台的在SecureCRT中添加过于麻烦,于是自己编写了一个Perl脚本批量创建,好 用又快速:
1、编辑一个列表文件,包括IP和主机名的,但是IP要以lan:开头,主机名要以hostname:开头,这个绝对不会难倒你吧。
[test@mantis create_session]$ vim list
lan:192.168.21.30 hostname:test1
2、运行脚本生指定ssh端口为22
ssh用户为nagios
ssh的password为6个0,(这里输入的是SecureCRT自己加密后的字符)
指定列表文件名为list
[test@mantis create_session]$ perl cre_sess.pl --port 22 --user nagios --password 8d860cf50f78af1a2833433076d288f3 --list list
运行该脚本需要先确认当前目录下有session_dir这个目录
已经修正,如果不存在session_dir目录,脚本将会自行创建。
3、运行后可以看到已经有Session文件了
[test@mantis create_session]$ ls session_dir/
(test0001) 192.168.21.30.ini
这里列表里只有一个主机,所以只生成一台,如果有多台,就会都生成在这个目录里的。接下来可以把这些文件拷到SecureCRT的Session目录里使用了。
############################################################
#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;
}
本文转自fine102 51CTO博客,原文链接:http://blog.51cto.com/gzmaster/293904,如需转载请自行联系原作者