- 论坛徽章:
- 27
|
本帖最后由 yifangyou 于 2012-05-22 13:52 编辑
话题一:生产环境中各种脚本(备份,部署等)应用
答:
远程执行命令脚本
scp_shell.sh的源代码- #!/usr/bin/expect
- #author: yifangyou
- #create time:2011-05-17
- set scphost "[lindex $argv 0]"
- set port "[lindex $argv 1]"
- set scpuser "[lindex $argv 2]"
- set scppw "[lindex $argv 3]"
- #要执行的shell命令
- set cmd "[lindex $argv 4]"
- spawn ssh -p $port $scpuser@$scphost "$cmd"
- set timeout 30
- expect {
- #respose: "root@1.2.3.4's password:"
- "*password*" {
- set timeout 30
- send "$scppw\r"
- }
- #the first connect will respose "Are you sure you want to continue connecting (yes/no)? yes"
- "*yes*" {
- set timeout 30
- send "yes\r"
- set timeout 30
- expect "*password*"
- set timeout 30
- send "$scppw\r"
- }
- busy {send_user "\n<error:busy>";exit 1;}
- failed {send_user "\n<error:failed>";exit 2;}
- timeout {send_user "\n<error:timeout>";exit 3;}
- }
- #Permission denied not try again
- expect {
- "*denied*" {
- send_user "\n<error:Permission denied>"
- exit 4
- }
- busy {send_user "\n<error:busy>";exit 5;}
- failed {send_user "\n<error:failed>";exit 6;}
- timeout {send_user "\n<error:timeout>";exit 7;}
- }
- exit 0
复制代码 批量远程执行命令脚本
multi_scp_shell.sh的源代码- #!/bin/bash
- #author: yifangyou
- #create time:2011-05-17
- #用来通过scp在目标机器批量执行命令
- #配置文件格式:
- #ssh_hosts=("1.1.1.1" "2.2.2.2")
- #ssh_ports=("22" "22") 这个可以缺省,缺省值为22,或者个数比ssh_hosts少时,使用缺省值
- #ssh_users=("root" "root") 这个可以缺省,缺省值为root,,或者个数比ssh_hosts少时,使用缺省值
- #ssh_passwords=("323" "222") 这个可以缺省,缺省的话需要从命令行输入,或者个数比ssh_hosts少时,使用命令行输入
- #执行:sh multi_scp_shell.sh conf_file_path 'cmd'
- if [ -z "$2" ]
- then
- echo "sh multi_scp_shell.sh conf_file_path 'cmd'";
- exit;
- fi
- default_ssh_user="root"
- default_ssh_port="22";
- #upload shell script file path
- scp_upload=scp_upload.sh
- #configure file path
- conf_file=$1
- #shell command
- scp_cmd=$2
- #判断conf_file配置文件是存在
- if [ ! -e "$conf_file" ]
- then
- echo "$conf_file is not exists";
- exit;
- fi
- #read configure file
- source $conf_file
- #若是没有在配置文件里提供密码,则在命令行输入
- if [ "${#ssh_passwords[@]}" = "0" ] || [ "${#ssh_passwords[@]}" -lt "${#ssh_hosts[@]}" ]
- then
- read -p "please input password:" -s default_ssh_password
- fi
- success_hosts="";
- fail_hosts="";
- for((i=0;i<${#ssh_hosts[@]};i++))
- do
- #remote ssh host
- ssh_host=${ssh_hosts[$i]};
- if [ "$ssh_host" != "" ]
- then
- #remote ssh port
- ssh_port=${ssh_ports[$i]};
- if [ "$ssh_port" = "" ]
- then
- ssh_port=$default_ssh_port; #use default value
- fi
- #remote ssh user
- ssh_user=${ssh_users[$i]};
- if [ "$ssh_user" = "" ]
- then
- ssh_user=$default_ssh_user; #use default value
- fi
- #remote ssh password
- ssh_password=${ssh_passwords[$i]};
- if [ "$ssh_password" = "" ]
- then
- ssh_password=$default_ssh_password; #use default value
- fi
- echo "["`date +"%F %T"`"] (scp -r $ssh_user@$ssh_host:$ssh_port exe '$scp_cmd') start"
- #scp file or dir
- echo "######################################$ssh_host output start############################################################"
- /usr/bin/expect scp_shell.sh "$ssh_host" "$ssh_port" "$ssh_user" "$ssh_password" "$scp_cmd"
- if [ "$?" -eq "0" ]
- then
- success_hosts="$success_hosts,$ssh_host"
- else
- fail_hosts="$fail_hosts,$ssh_host"
- fi
- echo "######################################$ssh_host output end############################################################"
- echo "["`date +"%F %T"`"] (scp -r $ssh_user@$ssh_host:$ssh_port exe '$scp_cmd') end"
- echo ""
- else
- echo "ssh_host[$i]=null"
- fi
- done
- echo "success_hosts=[$success_hosts]"
- echo "fail_hosts=[$fail_hosts]"
复制代码 配置文件格式- #ssh_hosts=("1.1.1.1" "2.2.2.2")
- #ssh_ports=("22" "22") #wheen port_num < host_num use default=22,or ssh_ports is undefined use 22 as default value
- #ssh_users=("root" "root") #wheen user_num < host_num use default=root,or ssh_users is undefined use root as default value
- #ssh_passwords=("323" "222") #wheen password_num < host_num use default=input password,or ssh_users is undefined use input password
复制代码 话题二:各种开源软件在自动化运维中的管理应用
答:
1.expect:能够自动默认人机交互,自动输入yes,或者密码等操作,可以让所有的人机交互变为自动输入,但是这方面的资料太少
2.xmanager,Cluster SSH,SecureCRT这些可以同时打开多个ssh客户端,运维人员可以输入一条命令在多个服务器上执行。
3.sshpass:类似于expect,但是比较弱只支持ssh或者scp可以用参数方式输入密码,没有返回值
话题三:公司需要运维平台的功能
答:1.能够同时控制多台机器执行shell命令
2.能够获取各台机器执行shell脚本后的返回结果,最好能够图形话表现出那些机器执行成功和执行进度
3.最好能够同时修改2000个机器的配置文件
话题四:实施自动化运维的难处
答:1.首先要统一各个服务器的环境,若是有某一台机器环境不一致(少装了一些软件),会导致同样的命令在这台上执行不成功
2.超过20台机器的部署比较困难,需要使用批量自动化工具
3.执行的命令最好不要或者尽量减少人机交换,最好所有命令都写到一个shell脚本上,只要执行一条命令执行即可
4.非常难写出一个能够在各个服务器上都能够成功运行起来的shell |
|