expect 小脚本一例
时间:2011-01-27
来源:互联网
- #!/usr/bin/expect
-
- if {$argc!=3} {
- send_user "Usage: $argv0 {Array IP} {Password} {CMD}\n\n"
- exit
- }
- #log_file ./out
- set IP [lindex $argv 0]
- set Password [lindex $argv 1]
- set CMD [lindex $argv 2]
-
- spawn ssh admin@$IP
- #spawn ssh root@$IP
- #expect {
- #"Are you" { send "yes\n"
- # }
- #}
- #set timeout 50
- expect {
- "Password:" {
- exec sleep 1
- send "${Password}\r"
- }
- "*continue connecting*" {
- exec sleep 1
- send "yes\r"
- expect "*Password:" {
- exec sleep 1
- send "${Password}\r"
- }
- }
- }
- #exec sleep 1
- #send "${Password}\r"
- expect {
- "*Password*" { send_user "\n1:Password error!\n"
- exit
- }
- "*already*" { send_user "\n2:Repeated login!\n"
- exit
- }
- "OceanStor: admin>" { send "${CMD}\r"
- }
- }
- #send_user "sleep"
- #expect "OceanStor: admin>"
- #send "${CMD}\r"
- #exec sleep 5
-
- expect "*>"
- #send "exit\r"
- send "exit\r"
- #send "exit\r"
- expect "*closed*"
- exit
- #exec sleep 10
作者: chinaboywg 发布时间: 2011-01-27
回复 chinaboywg
可用不?说下环境之类的,大家都在找这个呢
可用不?说下环境之类的,大家都在找这个呢
作者: expert1 发布时间: 2011-01-27
- et prefix "\033\[1;31m>>>\033\[0m"
-
- proc usage {} {
- regsub ".*/" $::argv0 "" name
- send_user "Usage:\n"
- send_user " $name \[user@]host password\n"
- send_user "\n"
- send_user "Report bugs to <\033\[1;[email protected]\033\[0m>\n"
- exit 1
- }
-
- proc check_id_files {} {
- if {! [file exists $::id_file]} {
- send_user "$::prefix id file not found, try creating ...\n"
- if {[catch { spawn ssh-keygen -t rsa } error]} {
- send_error "$::prefix $error\n"
- exit 1
- }
- expect -nocase -re "\(.*\):"
- send -- "\r"
- expect -nocase -re "passphrase.*:"
- send -- "\r"
- expect -nocase -re "passphrase.*again:"
- send -- "\r"
- expect eof
- send_user "$::prefix id file successfully created\n"
- }
- }
-
- proc remove_known_hosts_entry {host} {
- regsub ".*/" $::argv0 "" name
- set tmp_file "/tmp/$name.tmp"
- set known_hosts "$::env(HOME)/.ssh/known_hosts"
- send_user "$::prefix trying to remove '$host' from ~/.ssh/known_hosts ... "
- if {[catch {
- set fd_known_hosts [open $known_hosts r]
- set fdTmp [open $tmp_file w]
- while 1 {
- gets $fd_known_hosts line
- if [eof $fd_known_hosts] {
- break
- }
- if [regexp "(\[^, ]+,)*${host}(,\[^, ]+)* " $line] {
- continue
- }
- puts $fdTmp $line
- }
- close $fd_known_hosts
- close $fdTmp
- file rename -force $tmp_file $known_hosts
- send_user "OK\n"
- } error]} {
- send_user "failed\n"
- send_user "$::prefix $error\n"
- exit 1
- }
- }
作者: chinaboywg 发布时间: 2011-01-27
- ## get host and password from command line parameters
- #if {[llength $argv] != 3} {
- # usage
- #}
- set user@host [lindex $argv 0]
- set passwd [lindex $argv 1]
- set cmd ""
- set src ""
- set dst ""
- set address_pos ""
- if {[llength $argv] == 3} {
- set cmd [lindex $argv 2]
- }
- if {[llength $argv] == 5} {
- set src [lindex $argv 2]
- set dst [lindex $argv 3]
- set address_pos [lindex $argv 4]
- }
-
-
- ## create public key file if not found
- set id_file "$env(HOME)/.ssh/id_rsa.pub"
- #check_id_files
-
- ## ssh to host
- set yes_no 0
- set ok_string SUCCESS
- set timeout 66
- set done 0
- while {!$done} {
- spawn ssh ${user@host} echo $ok_string
- expect {
- -nocase -re "yes/no" {
- set yes_no 1
- send -- "yes\r"
- set done 1
- }
- -nocase -re "password: " {
- set done 1
- }
- $ok_string {
- send_user "$prefix ok\n"
- exit 0
- }
- "@@@@@@@@@@@@@@@@@@@@" {
- expect eof
- set indexOfAtSign [string first "@" ${user@host}]
- incr indexOfAtSign
- set hostname [string range ${user@host} $indexOfAtSign end]
- remove_known_hosts_entry $hostname
- }
- eof {
- send_error "$prefix failed\n"
- exit 1
- }
- timeout {
- send_error "$prefix timeout\n"
- #modified by z60006453 for problem AB6D00409 and AB6D00410 start
- exit 16
- #exit 1
- #modified by z60006453 for problem AB6D00409 and AB6D00410 end
- }
- }
- }
-
- if {$yes_no} {
- expect {
- $ok_string {
- send_user "$prefix ok\n"
- exit 0
- }
- -nocase -re "password: " {}
- }
- }
- send -- "$passwd\r"
- expect {
- -nocase "try again" {
- send_error "$prefix passwd error\n"
-
- exit 11
-
- }
-
- -nocase "password:" {
- send_error "$prefix passwd error\n"
-
- exit 11
-
-
-
- }
-
- $ok_string {}
- }
- expect eof
-
- ##solve the expect&ssh dead process(a little probability) problem.
- ##make the frequency of spawn ssh not very high
- sleep 1
-
- ## append public key file to remote host's ~/.ssh/authorized_keys
- #if {[catch {
- # set IDFILE [open $id_file RDONLY]
- # set pub_key [read $IDFILE]
- # close $IDFILE
- #} error]} {
- # send_error "$prefix $error\n"
- # exit 1
- #}
- #set pub_key [string trimright $pub_key "\r\n"]
-
- ###a little more than max install time
- set timeout 10806
-
- if {[llength $argv] == 3} {
- ##solve the expect&ssh dead process(a little probability) problem.
- spawn ssh ${user@host} "$cmd"
- }
- if {[llength $argv] == 5} {
- if { $address_pos == "RIGHT"} {
- ##solve the expect&ssh dead process(a little probability) problem.
- spawn /opt/dms/bin/scp_echo -r "$src" "${user@host}:$dst"
- }
- if { $address_pos == "LEFT" } {
- ##solve the expect&ssh dead process(a little probability) problem.
- spawn /opt/dms/bin/scp_echo -r "${user@host}:$src" "$dst"
- }
- }
- expect -nocase -re "password:"
- send -- "$passwd\r"
-
- ##solve the expect&ssh dead process(a little probability) problem.
- ##replace expect eof to expect "--exit code=.*=";
- ##when expect eof, there are no eof come forever with a little probability.
- expect {
- -nocase -re "--exit code=.*=" {
- exit 0
- }
- timeout {
- exit 16
- }
- }
-
- #send_user "$prefix gook luck\n"
- ## THE END
作者: chinaboywg 发布时间: 2011-01-27
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28