perl远程执行多台服务器shell命令
2012-09-12 15:49
387 查看
在生成环境中通常运维需要执行非常多的重复命令,一台可能还好 多台就杯具了。尤其有时候要批量去更新多台服务器的文件,或者是删除。
一个好运维都是懒惰的,所以自己写了个perl脚本可以远程去执行shell命令,很灵活。2个配置文件,一个管理服务器信息,一个放所需要执行的命令。
code:
#!/usr/bin/perl
use strict;
use Net::SSH::Expect;
my @ssh_list;
my $ssh_txt='ip_list.txt';
my $command_txt='command_txt.txt';
open FH,$ssh_txt;
while(<FH>){
@ssh_list=split;
print "正在登陆".$ssh_list[0]."...\n";
&ssh_conn("$ssh_list[0]","$ssh_list[1]","$ssh_list[2]","$ssh_list[3]");
}
close FH;
sub ssh_conn(){
my($host,$port,$user,$pass) = @_;
my $ssh = Net::SSH::Expect->new(
host => $host,
port => $port,
user => $user,
password =>$pass,
no_terminal =>0,
raw_pty =>1,
timeout => 3,
);
$ssh->debug(0);
$ssh->run_ssh() or die "SSH process coundn't start:$!";
$ssh->waitfor( '\(yes\/no\)\?$', 1 ); #交互式修改密码,给予2秒的时间
$ssh->send("yes\n");
$ssh->waitfor( 'password:\s*$/', 1);
$ssh->send("$ssh_list[3]");
$ssh->send("su - root");
$ssh->waitfor( 'password:\s*$/', 1);
$ssh->send("$ssh_list[4]");
#$ssh->waitfor("#\s*",2);
open F1,$command_txt;
while(<F1>){
my @command=split/\n/,$_;
print "$command[0]--> ";
$ssh->exec("$command[0]");
print "$ssh_list[0]命令执行完毕\n";
}
close F1;
$ssh->close();
下面是2个文件内容。
[root@nagios script]# cat ip_list.txt
192.168.2.101 22 mcshell psswd server1
192.168.2.102 22 mcshell psswd server2
192.168.2.103 22 mcshell psswd server3
[root@nagios script]# cat command_txt.txt
touch /home/mcshell/file1
touch /home/mcshell/file2
执行结果:
当然我这里为了测试方便,用了写的比较简单,大家也可以发挥想象,直接把复杂的shell或者perl密令直接放在command_txt.txt中。同样可以批量处理
本文出自 “mcshell学习博客” 博客,请务必保留此出处http://mcshell.blog.51cto.com/803455/988791
一个好运维都是懒惰的,所以自己写了个perl脚本可以远程去执行shell命令,很灵活。2个配置文件,一个管理服务器信息,一个放所需要执行的命令。
code:
#!/usr/bin/perl
use strict;
use Net::SSH::Expect;
my @ssh_list;
my $ssh_txt='ip_list.txt';
my $command_txt='command_txt.txt';
open FH,$ssh_txt;
while(<FH>){
@ssh_list=split;
print "正在登陆".$ssh_list[0]."...\n";
&ssh_conn("$ssh_list[0]","$ssh_list[1]","$ssh_list[2]","$ssh_list[3]");
}
close FH;
sub ssh_conn(){
my($host,$port,$user,$pass) = @_;
my $ssh = Net::SSH::Expect->new(
host => $host,
port => $port,
user => $user,
password =>$pass,
no_terminal =>0,
raw_pty =>1,
timeout => 3,
);
$ssh->debug(0);
$ssh->run_ssh() or die "SSH process coundn't start:$!";
$ssh->waitfor( '\(yes\/no\)\?$', 1 ); #交互式修改密码,给予2秒的时间
$ssh->send("yes\n");
$ssh->waitfor( 'password:\s*$/', 1);
$ssh->send("$ssh_list[3]");
$ssh->send("su - root");
$ssh->waitfor( 'password:\s*$/', 1);
$ssh->send("$ssh_list[4]");
#$ssh->waitfor("#\s*",2);
open F1,$command_txt;
while(<F1>){
my @command=split/\n/,$_;
print "$command[0]--> ";
$ssh->exec("$command[0]");
print "$ssh_list[0]命令执行完毕\n";
}
close F1;
$ssh->close();
下面是2个文件内容。
[root@nagios script]# cat ip_list.txt
192.168.2.101 22 mcshell psswd server1
192.168.2.102 22 mcshell psswd server2
192.168.2.103 22 mcshell psswd server3
[root@nagios script]# cat command_txt.txt
touch /home/mcshell/file1
touch /home/mcshell/file2
执行结果:
当然我这里为了测试方便,用了写的比较简单,大家也可以发挥想象,直接把复杂的shell或者perl密令直接放在command_txt.txt中。同样可以批量处理
本文出自 “mcshell学习博客” 博客,请务必保留此出处http://mcshell.blog.51cto.com/803455/988791
相关文章推荐
- shell: 普通用户如何以root权限执行远程服务器上的命令_20160706_七侠镇莫尛貝
- windows 下 java程序jsch连接远程linux服务器执行shell命令
- shell: 普通用户如何以root权限执行远程服务器上的命令_20160706_七侠镇莫尛貝
- shell小白 想玩自动化部署,无密码ssh 登录远程服务器,执行scp命令传送war包 并重启tomcat。
- shell小白 想玩自动化部署,无密码ssh 登录远程服务器,执行scp命令传送war包 并重启tomcat。
- Java实现远程联接服务器执行shell命令
- Windows远程linux服务器执行shell命令
- 本地shell执行远程命令
- Java 连接远程Linux 服务器执行 shell 脚本查看 CPU、内存、硬盘信息
- Jenkin配置执行远程shell命令
- 使用paramiko模块在远程服务器执行命令
- shell ssh远程执行命令
- JAVA实现远程执行SSH2主机的SHELL命令
- 在远程服务器上执行本地的shell脚本
- php 利用ssh执行远程或本地liunx服务器命令
- Jenkins执行远程Linux系统的shell命令
- JSch:纯JAVA实现远程执行SSH2主机的SHELL命令
- Linux远程拷贝&远程执行命令shell脚本
- shell脚本实现同时多台远程主机执行命令的代码分享
- java jsch远程执行shell脚本命令