zoukankan      html  css  js  c++  java
  • expect 交互 模拟ssh 登陆

    模拟ssh登录

    #!/bin/bash
    Ip='192.168.1.6'            # 循环就行
    RemoteUser='user'           # 普通用户
    RemotePasswd='userpasswd'   # 普通用户的密码
    RemoteRootPasswd='rootpasswd'
    /usr/bin/expect -c "
    set timeout -1
    spawn ssh -t -p $Port -o StrictHostKeyChecking=no $RemoteUser@$Ip
    expect {
        password {
            send_user RemotePasswd
            send ${RemotePasswd}
    ;
            expect {
                "does not exist" {
    send_user "root user does not exist
    "
    exit 10
                }
                password {
    send_user "user passwd error
    "
    exit 5
                }
                Last {
    send "su - batch
    "
    expect {
        Password {
            send_user RemoteRootPasswd
            send ${RemoteRootPasswd}
    ;
            expect {
                "]#" {
    send "sh /tmp/update.sh update
     "
    expect {
        "]#" {
            send_user ${Ip}_Update_Done
    
        }
        eof
    }
                }
            }
        }
    }
                }
            }
        }
        "No route to host" {
            send_user "host not found
    "
            exit 4
        }
        "Invalid argument" {
            send_user "incorrect parameter
    "
            exit 8
        }
        "Connection refused" {
            send_user "invalid port parameters
    "
            exit 9
        }
        "does not exist" {
            send_user "root user does not exist"
            exit 10
        }
        timeout {
            send_user "connection timeout 
    "
            exit 3
        }
        eof
    }
    "
    state=`echo $?`
  • 相关阅读:
    面向对象初识
    day 20 异常
    day 16 正则表达式
    day 13 生成器,推导式
    CSS
    html页面编写
    DAY 17常用模块
    DAY16 模块和包的导入
    DAY15 模块
    DAY14 函数(三)
  • 原文地址:https://www.cnblogs.com/sharesdk/p/8710095.html
Copyright © 2011-2022 走看看