[关闭]
@Great-Chinese 2016-09-08T05:13:57.000000Z 字数 690 阅读 848

Linux编程实战

linux


expect 自动登录脚本

安装expect

  1. yum install -y expect

自动登录到另外一台机器

新建文件1.expect

  1. #! /usr/bin/env expect
  2. set host "192.168.11.102"
  3. set passwd "123456"
  4. spawn ssh root@$host
  5. expect {
  6. "yes/no" { send "yes\r"; exp_continue}
  7. "assword:" { send "$passwd\r" }
  8. }
  9. interact

自动同步脚本

  1. #!/usr/bin/env expect
  2. set passwd "123456"
  3. spawn rsync -av root@192.168.11.18:/tmp/12.txt /tmp/
  4. expect {
  5. "yes/no" { send "yes\r"}
  6. "password:" { send "$passwd\r" }
  7. }
  8. expect eof

传递参数

执行语法 ./2.expect 192.168.1.123 123456 "ls -alF /tmp"

  1. #!/usr/bin/env expect
  2. set password [lindex $argv 1]
  3. set host [lindex $argv 0]
  4. set cm [lindex $argv 2]
  5. spawn ssh root@$host
  6. expect {
  7. "yes/no" { send "yes\r" }
  8. "assword:" { send "$password\r" }
  9. }
  10. expect "]*"
  11. send "$cm \r"
  12. expect "]*"
  13. send "exit\r"
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注