批量生成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;
}




     本文转自fine102 51CTO博客,原文链接:http://blog.51cto.com/gzmaster/293904,如需转载请自行联系原作者



相关文章
|
3天前
|
数据安全/隐私保护
XShell新建会话指南
XShell新建会话指南
|
2月前
|
存储 网络安全 数据安全/隐私保护
scp上传文件到远程服务器,如何避免每次都要输入远程服务器的密码
scp上传文件到远程服务器,如何避免每次都要输入远程服务器的密码
49 0
|
5月前
|
网络安全 Go Cloud Native
【小技巧】SSH连接服务器后如何默认执行多条命令?
大家平时有没有遇到自己连接云服务器,ssh 连接上去之后,发现自己的一些小工具用不了 例如go build无法使用 ,由于我们安装配置golang 环境的时候
【小技巧】SSH连接服务器后如何默认执行多条命令?
|
7月前
|
存储 关系型数据库 MySQL
常见连接工具保存密码获取
之前有发过关于xshell&finalshell密码破解的文章,本文将继续对一些其他常见的连接工具进行讨论,如有错误,欢迎留言指出!
202 0
常见连接工具保存密码获取
xshell 所选的用户密钥未在远程主机上注册
xshell 所选的用户密钥未在远程主机上注册
622 0
xshell 所选的用户密钥未在远程主机上注册
SecureCRT工具会话保持的办法
版权声明:转载请注明出处:http://blog.csdn.net/dajitui2024 https://blog.csdn.net/dajitui2024/article/details/79396706 SecureCRT创建会话以后,总是自动掉线或者无响应了。
1232 0
如何在Webstorm/Phpstorm中设置连接FTP,并快速进行文件比较,上传下载,同步等操作
Phpstorm除了能直接打开localhost文件之外,还可以连接FTP,除了完成正常的数据传递任务之外,还可以进行本地文件与服务端文件的异同比较,同一文件自动匹配目录上传,下载,这些功能是平常IDE,FTP软件中少见的,而且是很耗工作时间的一个操作。
2081 0