作为系统管理员经常碰到的问题就是 给这 30 台机器装个软件,给哪50台机器装个软件的。我使用的系统是 linux 现在管理的服务器大概有几百台了。都是用户名和密码,没加 key 认证,网上一般都说加 ssh-key 然后 怎么怎么实现之类的。这里放出我的方法。
方法1:用 expect 脚本实现。 (需要安装 expect 包)
- #!/usr/bin/expect -f
- exp_version -exit 5.0
- set Conf "./exp_auto_run.conf"
- set Default_User defusername
- set Default_PWD defpassword
- set timeout 60
- if {$argc!=0} {
- send_user "usage: $argv0 host1 host2 host3 . . .\n";
- send_user "usage: $argv0 $argc [lrange $argv 0 $argc]\n";
- send_user "argv 0 is [lindex $argv 0]\n"
- }
- if {![file exists $Conf]} {
- puts "no fonnd $Conf"
- exit 1
- }
- set fileId [open $Conf r 0400]
- set contents [read $fileId]
- close $fileId
- foreach i [split $contents \n] {
- if {[string match host=* $i]} {
- foreach j $i {
- switch -regexp -- $j {
- ^host=.+ {set host [lindex [split $j =] 1]}
- ^port=.+ {set port [lindex [split $j =] 1]}
- ^username=.+ {set username [lindex [split $j =] 1]}
- ^password=.+ {set password [lindex [split $j =] 1]}
- }
- if {![info exists port]} {
- set port [lindex [split $host .] 3]22
- }
- if {![info exists username]} {
- set username $Default_User
- }
- if {![info exists password]} {
- set password $Default_PWD
- }
- }
- spawn ssh -l $username $host -p $port
- expect {
- "(yes/no)" {send "yes\r";exp_continue}
- "assword:" {send "${password}\r";exp_continue}
- -re ".\+]\\\$" {send "sudo -s\r";exp_continue}
- -re ".\+]\\\#" {send "su -l\r";}
- }
- foreach cmd {
- "echo running command at server"
- } {
- expect -re ".\+]#" {send "${cmd}\r"}
- }
- sleep .1
- expect -re "\\\[.\+]" {send ":\r"}
- close -i $spawn_id
- unset host port username password
- }
- }
- exit
配置文件是:
- # Expect Config File
- # [HOST] [PORT] [USERNAME] [PASSWORD]
- host=192.168.1.2 port=22 username=u_admin password=p_admin
方法2:
用 python 的 pexpect 实现,需要装 pexpect 模块
- #!/usr/bin/python
- """connect ssh pexpect"""
- import os,sys,pexpect
- def connect_host(host_dic):
- connect_host = pexpect.spawn('ssh -l %s %s -p %s' % (host_dic["username"],host_dic["host"],host_dic["port"]))
- while(1):
- index = connect_host.expect(['.assword:','.*(yes/no).','.*[#\$] ',pexpect.EOF,pexpect.TIMEOUT])
- if index == 0:
- connect_host.sendline(host_dic["password"])
- elif index == 1:
- connect_host.sendline("yes")
- elif index == 2:
- connect_host.sendline("")
- break
- elif index == 3:
- break
- elif index == 4:
- break
- else:
- sys.exit(1)
- connect_host.interact()
- connect_host.close()
- def main():
- host_list={"host":"192.16.1.3","username":"u_admin","password":"p_admin","port":"22"}
- connect_host(host_list)
- sys.exit(0)
- main()
方法3: 用 ssh + screen 实现。
- function conn_server() {
local _U="username" _P
>/${USER}/.ssh/known_hosts
for ip in 2 3 4 5 6 7 8 9;do
screen -S ${STY} -X screen -t web ssh -l ${_U} 192.168.1.${ip} -p 22
done
screen -S ${STY} -X select 0
stty -echo
read -t 60 -p "Password: " _P
stty echo
sleep 2
screen -S ${STY} -X at web# stuff $'yes\x0a'
sleep 2
screen -S ${STY} -X at web# stuff "${_P:-defpassword}"$'\x0a' - unset _P
sleep .5
screen -S ${STY} -X at web# stuff $'sudo -s\x0a'
sleep .5
screen -S ${STY} -X at web# stuff $'su -l\x0a'
- }
- function all_run() { screen -S ${STY} -X at web# stuff "$@"$'\x0a'; }
- screen -S admin
- conn_server
- all_run "echo running at remove server"
多个脚本用途各有各的不同,妙用在那里需要自己体会使用了 .
阅读(1812) | 评论(0) | 转发(0) |