#!/bin/bash content='Line' expect <<EOF set file "hello.txt" set fd [open \$file r] set n 0 while {[gets \$fd line] != -1} { incr n puts "$content\$n: \$line" } close \$fd EOF
#!/usr/bin/expect -f set fd [open /etc/passwd r] set data [read $fd] puts $data ### 这样就可以输出每一行 close $fd
把本地文件中的命令,执行在远程机上
,执行结束后,自动退出远程端
#!/usr/bin/expect -f set ip 192.168.6.**** set user ******* set password ******* set timeout 1 spawn ssh $user@$ip expect { "*yes/no" { send "yes\r"; exp_continue} "*password:" { send "$password\r" } } expect "pc0003" send "ls -lh\r" set fd [open ./com r] while {[gets $fd line] != -1} { expect "pc0003" send "$line \r" } close $fd expect eof #interact