Chinaunix首页 | 论坛 | 博客
  • 博客访问: 240055
  • 博文数量: 41
  • 博客积分: 1523
  • 博客等级: 上尉
  • 技术积分: 579
  • 用 户 组: 普通用户
  • 注册时间: 2007-02-05 21:23
文章分类

全部博文(41)

文章存档

2014年(1)

2013年(2)

2012年(1)

2011年(2)

2010年(3)

2009年(1)

2008年(20)

2007年(11)

分类: LINUX

2012-06-28 09:42:08

    作为系统管理员经常碰到的问题就是 给这 30 台机器装个软件,给哪50台机器装个软件的。我使用的系统是 linux 现在管理的服务器大概有几百台了。都是用户名和密码,没加 key 认证,网上一般都说加 ssh-key 然后 怎么怎么实现之类的。这里放出我的方法。

方法1:用 expect 脚本实现。 (需要安装 expect 包)

点击(此处)折叠或打开

  1. #!/usr/bin/expect -f
  2. exp_version -exit 5.0
  3. set Conf "./exp_auto_run.conf"
  4. set Default_User defusername
  5. set Default_PWD defpassword
  6. set timeout 60
  7. if {$argc!=0} {
  8.     send_user "usage: $argv0 host1 host2 host3 . . .\n";
  9.     send_user "usage: $argv0 $argc [lrange $argv 0 $argc]\n";
  10.     send_user "argv 0 is [lindex $argv 0]\n"
  11. }
  12. if {![file exists $Conf]} {
  13.     puts "no fonnd $Conf"
  14.     exit 1
  15. }
  16. set fileId [open $Conf r 0400]
  17. set contents [read $fileId]
  18. close $fileId
  19. foreach i [split $contents \n] {
  20.     if {[string match host=* $i]} {
  21.         foreach j $i {
  22.             switch -regexp -- $j {
  23.                 ^host=.+ {set host [lindex [split $j =] 1]}
  24.                 ^port=.+ {set port [lindex [split $j =] 1]}
  25.                 ^username=.+ {set username [lindex [split $j =] 1]}
  26.                 ^password=.+ {set password [lindex [split $j =] 1]}
  27.             }
  28.             if {![info exists port]} {
  29.                 set port [lindex [split $host .] 3]22
  30.             }
  31.             if {![info exists username]} {
  32.                 set username $Default_User
  33.             }
  34.             if {![info exists password]} {
  35.                 set password $Default_PWD
  36.             }
  37.         }
  38.         spawn ssh -l $username $host -p $port
  39.         expect {
  40.             "(yes/no)" {send "yes\r";exp_continue}
  41.             "assword:" {send "${password}\r";exp_continue}
  42.             -re ".\+]\\\$" {send "sudo -s\r";exp_continue}
  43.             -re ".\+]\\\#" {send "su -l\r";}
  44.         }
  45.         foreach cmd {
  46.                 "echo running command at server"
  47.         } {
  48.             expect -re ".\+]#" {send "${cmd}\r"}
  49.         }
  50.         sleep .1
  51.         expect -re "\\\[.\+]" {send ":\r"}
  52.         close -i $spawn_id
  53.         unset host port username password
  54.     }
  55. }

  56. exit

配置文件是:

点击(此处)折叠或打开

  1. # Expect Config File
  2. # [HOST]        [PORT]        [USERNAME]        [PASSWORD]
  3. host=192.168.1.2    port=22    username=u_admin        password=p_admin

方法2:
用 python 的 pexpect 实现,需要装 pexpect 模块

点击(此处)折叠或打开

  1. #!/usr/bin/python

  2. """connect ssh pexpect"""

  3. import os,sys,pexpect

  4. def connect_host(host_dic):
  5.     connect_host = pexpect.spawn('ssh -l %s %s -p %s' % (host_dic["username"],host_dic["host"],host_dic["port"]))
  6.     while(1):
  7.         index = connect_host.expect(['.assword:','.*(yes/no).','.*[#\$] ',pexpect.EOF,pexpect.TIMEOUT])
  8.         if index == 0:
  9.             connect_host.sendline(host_dic["password"])
  10.         elif index == 1:
  11.             connect_host.sendline("yes")
  12.         elif index == 2:
  13.             connect_host.sendline("")
  14.             break
  15.         elif index == 3:
  16.             break
  17.         elif index == 4:
  18.             break
  19.         else:
  20.             sys.exit(1)
  21.     connect_host.interact()
  22.     connect_host.close()

  23. def main():
  24.     host_list={"host":"192.16.1.3","username":"u_admin","password":"p_admin","port":"22"}
  25.     connect_host(host_list)
  26.     sys.exit(0)

  27. main()



方法3: 用 ssh + screen 实现。

点击(此处)折叠或打开

  1. 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'
  2.     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'
  3. }
  4. function all_run() { screen -S ${STY} -X at web# stuff "$@"$'\x0a'; }

  5. screen -S admin
  6. conn_server
  7. all_run "echo running at remove server"


多个脚本用途各有各的不同,妙用在那里需要自己体会使用了 .
阅读(1757) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~