Created
October 11, 2014 02:32
-
-
Save Buttonwood/8f6f702d7d90c7e9c55f to your computer and use it in GitHub Desktop.
expect 自动登录
This file contains hidden or 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
| # http://dongwm.blog.51cto.com/2621371/781099 | |
| #!/usr/bin/expect #设置解析器 | |
| set timeout 10 #设置超时时间 | |
| set password1089 ***** | |
| set passwordvps ***** | |
| set passwordkvm26 ***** | |
| set password4150 ***** #以上密码我隐藏了 | |
| set file1 [lindex $argv 0] #程序第一个参数 | |
| set port [lindex $argv 1] #程序第二个参数 | |
| if {$port==""} { | |
| set port 22 | |
| } #设置登录的默认ssh端口是22 ,假如程序只有一个参数(也就是设计的服务器的代码)表示默认的是22 | |
| if {$argc>2} { | |
| puts stderr "参数错误,请使用以下格式: $argv0 {想去的服务器简称,比如89 端口(默认是22)}" | |
| exit 1 #设置当参数出现错误的提示信息,这里没有太强的逻辑性,当参数多于2个就会弹这个错误 | |
| } | |
| if {$file1=="89"} { #当程序的第一个参数是89 执行这个代码块,以下的类似,当然可以写成switch形式的 | |
| spawn luit -encoding gbk ssh -p $port root@192.168.10.89 | |
| #必须加spawn luit是我自己的设计,它是一个命令,但是需要手动安装,因为当我登录远程服务器可能因为编码出现一些错误,但是不能随意改变远程服务器的编码locale,只是我登录的这个终端使用我设计的编码,我这里是gbk编码。去掉这个额外的设置是:spawn ssh -p $port root@192.168.10.89 | |
| expect "password:" #这里表示等待出现 "password:" | |
| send "$password1089\n" #然后发送数据 记住需要回车 | |
| interact | |
| #表示执行完登录到远程服务器后可以自由使用,要不然登录上去什么也作不了,在这里就可以继续执行其他命令操作 | |
| } elseif {$file1=="58"} { | |
| #tcl语言控制结构的语法是 if... elseif ... else | |
| spawn luit -encoding gbk ssh -p 9922 root@192.168.8.58 | |
| expect "password:" | |
| send "****\n" #这里我没有设置变量 而是直接将相应字符串写进来 | |
| interact | |
| } elseif {$file1=="26"} { | |
| spawn luit -encoding gbk ssh -p 61300 root@10.14.22.26 -X -C | |
| #expect "password:" | |
| #send "$passwordkvm26\n" | |
| interact | |
| } elseif {$file1=="150"} { | |
| spawn luit -encoding gbk ssh -p $port root@192.168.4.150 | |
| expect "password:" | |
| send "$password4150\n" | |
| interact | |
| } elseif {$file1=="62"} { #这里是使用rdesktop登录windows机器 ,其中密码我隐藏了 | |
| spawn rdesktop -u admin -p **** -f -r clipboard:PRIMARYCLIPBOARD -r disk:sunray=/home/dongwm 192.168.10.62:63005 | |
| interact | |
| } elseif {$file1=="63"} { | |
| spawn rdesktop -u admin -p **** -f -r clipboard:PRIMARYCLIPBOARD -r disk:sunray=/home/dongwm 192.168.10.63:63005 | |
| interact | |
| } else { #这里设置的是当第一个参数不是我设计的那些参数是默认的操作方式 比如我可以直接写服务器的ip 或者 /etc/hosts指定的hosts | |
| spawn luit -encoding gbk ssh -p $port root@$file1 | |
| interact | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment