vieyahn2017 / iBlog

44 stars 0 forks source link

4.11 linux内嵌expect使用 #355

Closed vieyahn2017 closed 3 months ago

vieyahn2017 commented 4 years ago

linux内嵌expect使用

vieyahn2017 commented 4 years ago

一体化的su


touch su.exp && chmod 777 su.exp && echo "Usage: ./su.exp" && 
(cat <<EOF
#!/usr/bin/expect -f

spawn su
expect {
    "*assword*"            { send "pwd\r" }
    eof                    { exit 0 }
}
send "whoami\r"
expect "root"
interact

EOF
) > su.exp
vieyahn2017 commented 3 years ago

ssh


touch ssh.exp && chmod 777 ssh.exp && echo "Usage: ./ssh.exp" && 
(cat <<EOF
#!/usr/bin/expect -f
spawn ssh  sshuser@172.17.35.2
expect {
    "*assword*"            { send "pwd\r" }
    "*(yes/no)*"           { send "yes\r"; exp_continue } 
    eof                    { exit 0 }
}
interact
EOF
) > ssh.exp
vieyahn2017 commented 3 years ago

10.7 scp的脚本:

#!/usr/bin/expect
set timeout -1
set host [lindex $argv 0]
set file [lindex $argv 1]

spawn scp $file  user@$host:/home/userpath
expect {
    "*assword*"            { send "pwd@RD\r" }
    "*(yes/no)*"           { send "yes\r"; exp_continue } 
    eof                    { exit 0 }
}

interact

//第一次ssh连接会提示yes/no,继续 //不是第一次连接,一般 /root/.ssh/known_hosts这里会记录一条目标ip

vieyahn2017 commented 3 years ago

Shell脚本学习之expect命令

https://www.cnblogs.com/lixigang/articles/4849527.html

这个很全,基本上能满足现阶段遇到的使用