zoukankan      html  css  js  c++  java
  • Shell登陆远程服务器

      现场服务器较多,密码3个月过期,在到期时需更改密码。

      使用expect编写,尝试登陆2次后退出(防止密码错误时账号锁定),超时重试一次。

      shell脚本调用并定时执行,登陆成功后执行一条命令,如:hostname、uname等,根据退出状态判断密码是否到期。

      0--正常

      1--传入参数错误

      2--timeout

      3--密码错误或到期

      脚本如下: 

    #!/usr/bin/expect
    
    ###############################################################
    # 连接远程主机
    proc do_login {passwd} {
        set timeout 30
        set done 1
        set timeout_case 0
        set ps1 {PS1="doraemon#";export PS1}
        while {$done<3} {
            expect {
                *assword* {
                    send $passwd
                    incr done
                }
                [$%>#] {
                    set done 5
                    send $ps1
    
                    break
                }
                timeout {
                    set done 1
                    switch -- $timeout_case {
                        1 { send_user "try again ...
    " }
                        2 { exit 2
                            expect eof
                        }
                    }
                    incr timeout_case
                }
            }
        }
        if {$done==3} {
            exit 3
            expect eof
        }
    }
    ###############################################################
    # 执行命令
    proc exec_cmd {cmd} {
        expect -re "doraemon#$"
        send_user "$cmd: $cmd
    "
        send $cmd
    }
    ###############################################################
    # 退出
    proc logout {} {
        expect -re "doraemon#$"
        send exit
        expect eof
    }
    ###############################################################
    if {$argc < 4} {
        send_user "Usage:$argv0 user pass ip command
    "
        exit 1
    }
    set user [lindex $argv 0]
    set pass [lindex $argv 1]
    set ip [lindex $argv 2]
    set num 3
    
    spawn ssh -o StrictHostKeyChecking=no -l $user $ip 
    do_login $pass
    while {$num<$argc} {
        set proc [lindex $argv $num]
        # 捕获输出用
        set cmd "echo ==$ip==$user==`$proc`=="
        exec_cmd $cmd
        incr num
    }           
    logout

       测试输出:

    [root@centos1 shcript]# ./logon.expect 
    Usage:./logon.expect user pass ip command
    [root@centos1 shcript]# ./logon.expect root root123 127.0.0.1 hostname
    spawn ssh -o StrictHostKeyChecking=no -l root 127.0.0.1
    root@127.0.0.1's password: 
    Last login: Fri Aug 14 02:54:56 2015 from 127.0.0.1
    [root@centos1 ~]# PS1="doraemon#";export PS1
    doraemon#
    doraemon#$cmd: echo ==127.0.0.1==root==`hostname`==
    echo ==127.0.0.1==root==`hostname`==
    ==127.0.0.1==root==centos1==
    doraemon#exit
    logout
    Connection to 127.0.0.1 closed.
    [root@centos1 shcript]# echo $?
    0
  • 相关阅读:
    influxdb服务器 relay
    browse-agent type and curl post
    使用 Ansible 管理 MySQL 复制
    ansible里的item和with_items
    Ansible 从MySQL数据库添加或删除用户
    ansibel---tag模块
    ll | wc -l的陷阱
    ansible 判断和循环
    Ansible详解(二)
    Ansible详解(一)
  • 原文地址:https://www.cnblogs.com/wbjxxzx/p/4545928.html
Copyright © 2011-2022 走看看