批量生成SecureCRT的Session文件

因爲在工作中,批量的上架服務器,一次幾十上百臺,如果手動一臺一臺的在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目錄裏使用了。


############################################################

#!/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;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章