码迷,mamicode.com
首页 > 其他好文 > 详细

expect 交互 模拟ssh 登陆

时间:2018-04-03 19:15:53      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:密码   用户   not found   upd   user   expect   eof   timeout   /usr   

模拟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}\r;
        expect {
            \"does not exist\" {
send_user \"root user does not exist\n\"
exit 10
            }
            password {
send_user \"user passwd error\n\"
exit 5
            }
            Last {
send \"su - batch\n\"
expect {
    Password {
        send_user RemoteRootPasswd
        send ${RemoteRootPasswd}\r;
        expect {
            \"]#\" {
send \"sh /tmp/update.sh update\n \"
expect {
    \"]#\" {
        send_user ${Ip}_Update_Done\n
    }
    eof
}
            }
        }
    }
}
            }
        }
    }
    \"No route to host\" {
        send_user \"host not found\n\"
        exit 4
    }
    \"Invalid argument\" {
        send_user \"incorrect parameter\n\"
        exit 8
    }
    \"Connection refused\" {
        send_user \"invalid port parameters\n\"
        exit 9
    }
    \"does not exist\" {
        send_user \"root user does not exist\"
        exit 10
    }
    timeout {
        send_user \"connection timeout \n\"
        exit 3
    }
    eof
}
"
state=`echo $?`

 

expect 交互 模拟ssh 登陆

标签:密码   用户   not found   upd   user   expect   eof   timeout   /usr   

原文地址:https://www.cnblogs.com/sharesdk/p/8710095.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!