Closed vieyahn2017 closed 3 months 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
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
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
linux内嵌expect使用