免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 8007 | 回复: 14
打印 上一主题 下一主题

如何用expect写一个自动登录脚本 ? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-01-16 13:56 |只看该作者 |倒序浏览
公司服务器比较多,又不让用ssh公钥

如何用expect 写一个自动登录脚本。

论坛徽章:
0
2 [报告]
发表于 2009-01-16 14:00 |只看该作者
http://bbs.chinaunix.net/viewthread.php?tid=1070390
请教用expect实现ssh自动登录的问题。
http://bbs.chinaunix.net/viewthread.php?tid=1235827
通过expect自动登录到30台linux机器运行相同命令

论坛徽章:
0
3 [报告]
发表于 2009-01-16 16:38 |只看该作者
  1. #!/usr/bin/expect -f
  2. set f [open $PWD/ip.txt r]
  3. for {} {gets $f ipaddr } {} {
  4.     set ip [lindex ipaddr 1]
  5.     set pwd [lindex ipaddr 2]
  6.     ssh -p22 root@$ip
  7.      expect {
  8.                 "ssh: Could not resolve hostname : Name or service not known" {send_user "[exec echo \" ssh: Could not resolve hostname\($s
  9. ip\) : Name or service not known\" >>config_log]";exit}

  10.                 "not know" {send_user "[exec echo \"not know:$localip\" >>config_log]";exit}
  11.                  "(yes/no)?"    {send "yes\r";exp_continue}
  12.                 "password:"     {send "$pwd\r";exp_continue}
  13.                 "Permission denied, please try again." {
  14.                         send_user "[exec echo \"Error:Password is wrong:$localip\">>config_log]"
  15.                         exit
  16.                 }
  17.                
  18.             }
  19.     expect " ~]#"  {send "you command\r"}
  20.     expect "]$ "   {send "you command\r"}
  21.     close
  22. }
  23. close $f
复制代码


脚本从ip.txt文件中读取ip地址和登录密码,格式如下
192.168.1.1 123456
192.168.1.1 234567

如果登录时候的部分错误会记录在config_log里,由于没有环境,暂时没有测试。。大概的过程就是这个样子。

论坛徽章:
0
4 [报告]
发表于 2009-01-16 17:05 |只看该作者
我用



#!/usr/bin/expect -f

set ipaddress [lindex $argv 0]
set passwd [lindex $argv 1]
set timeout 30
spawn ssh root@$ipaddress

expect "password:"
send "$passwd\r"
expect "]*"
send "mkdir -p /tmp/haha/haha2\r"
send "exit\r"

测试正常  就是在判断一次登录输入YES的时候不行。那个步骤要怎么做

论坛徽章:
0
5 [报告]
发表于 2009-01-16 17:10 |只看该作者
我已经告诉你答案了 。
还有上个帖子http://bbs.chinaunix.net/thread-1070390-1-1.html里的waker 也都告诉你答案了。。你没看到马?

论坛徽章:
0
6 [报告]
发表于 2009-01-16 17:22 |只看该作者

回复 #1 gjw_apparitor 的帖子

不用ssh公钥的理由是?

论坛徽章:
0
7 [报告]
发表于 2009-01-16 17:23 |只看该作者
#!/usr/bin/expect -f

set ipaddress [lindex $argv 0]
set passwd [lindex $argv 1]
set timeout 30
spawn ssh root@$ipaddress
expect {
"yes/no" { send "yes\r"; exp_continue}
"password:" { send "$passwd\r" }
}



是这么加吗 ?  
我加了这条运行不成功。

论坛徽章:
0
8 [报告]
发表于 2009-01-16 17:31 |只看该作者
原帖由 gjw_apparitor 于 2009-1-16 17:23 发表
#!/usr/bin/expect -f

set ipaddress [lindex $argv 0]
set passwd [lindex $argv 1]
set timeout 30
spawn ssh root@$ipaddress
expect {
"yes/no" { send "yes\r"; exp_continue}
"password:" { sen ...




expect -d 把调试信息贴出来看下

论坛徽章:
0
9 [报告]
发表于 2009-01-16 17:35 |只看该作者
在最后加上


expect "]*"

就可以了


expect "]*"   是什么意思?

论坛徽章:
0
10 [报告]
发表于 2009-01-16 17:37 |只看该作者
原帖由 gjw_apparitor 于 2009-1-16 17:35 发表
在最后加上


expect "]*"

就可以了


expect "]*"   是什么意思?



期望看到 ]*字符串
如果看到了这个字符串就...
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP