Created
October 10, 2014 03:30
-
-
Save shalk/202d0862d7d3e0d884d8 to your computer and use it in GitHub Desktop.
expect example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 $?` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment