Chinaunix首页 | 论坛 | 博客
  • 博客访问: 373701
  • 博文数量: 75
  • 博客积分: 1486
  • 博客等级: 上尉
  • 技术积分: 675
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-22 18:38
个人简介

...

文章分类
文章存档

2023年(1)

2021年(3)

2020年(2)

2018年(1)

2017年(1)

2016年(10)

2015年(34)

2011年(14)

2010年(9)

分类: LINUX

2016-06-13 21:07:23

1)使用expect命令在shell脚本中实现的简单登陆

点击(此处)折叠或打开

  1. #!/bin/bash
  2. #!/usr/bin/expect -f

  3. if [ $# != 3 ];then
  4.     echo "Usage: cmd [host] [user] [passwd]"
  5. fi

  6. host=$1
  7. user=$2
  8. password=$3

  9. echo
  10. echo $user@$host
  11. echo

  12. #            eof {exit 0}
  13. auto_ssh_copy_id ()
  14. {
  15.     expect -c "set timeout -1;
  16.         spawn ssh $2;
  17.         expect
  18.         {
  19.             "*yes/no" {send "yes\r"; exp_continue}
  20.             "*password:" {send "$1\r"}
  21.         };
  22.         interact
  23.     "
  24. }

  25. #auto_ssh_copy_id $password $user@$host


  26. set ip [lindex $argv 0 ]
  27. set password [lindex $argv 1 ]
  28. set timeout 10
  29. spawn ssh root@$ip
  30.     expect {
  31.         "*yes/no" { send "yes\r"; exp_continue}
  32.         "*password:" { send "$password\r" }
  33.     }
  34. interact



  35. exit 0


2)expect脚本简单使用(*.exp 脚本)

点击(此处)折叠或打开

  1. #!/usr/bin/expect -f

  2. set ip 192.168.2.101
  3. set password "321"


  4. spawn ssh root@$ip

  5. expect {
  6.      "*yes/no" {send "yes\r"; exp_continue}
  7.      "*password:" {send "$password\r" }
  8. }

  9. expect "#*"
  10. send "passwd\r"


  11. expect {
  12.     "Enter*" { send "123\r"; exp_continue}
  13.     "Retype*" { send "123\r"; exp_continue}
  14. }

  15. send "exit\r"
  16. expect eof

  17. exit

3)自动登陆shell代码段

点击(此处)折叠或打开

  
  1. ……

  2. auto_ssh_login ()
  3. {
  4.     expect -c "
  5.         set timeout 5

  6.         spawn sshpass -p $2 ssh -o StrictHostKeyChecking=no -p 39000 root@$1

  7.         send "passwd\\r"
  8.         
  9.         expect {
  10.             Enter* {send -- $3\r; exp_continue}
  11.             Retype* {send -- $3\r; exp_continue}
  12.         }

  13.         send "exit\\r"
  14.         expect eof
  15.     "
  16. }

  17. ……




暂时更新到这里 继续研究



阅读(2199) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~