#!/usr/bin/expect
spawn ssh -p 22 root@192.168.2.3
expect {
"*(yes/no)?" { send "yes\r";exp_continue}
"*password:" { send "123456\r";}
"authenticity" {exp_continue}
"ECDSA" {exp_continue}
default {exp_continue}
}
expect {
"Last*" {exp_continue}
"#" { send "/app/shell.sh\r"}
default {exp_continue}
}
expect "#"
send "exit\r"
#退出进程
expect eof
#等待退出,否则会产生僵尸进程
wait
搜索了一大堆都是有毛病的,首先一个expect和send是对应关系,如果顺序不同呢?就需要引入expect{}组的概念,然后是里面的exp_continue是表示继续循环这个组,直到满足某一个条件!!!
至于后续的就看实际需求了,注意这个文件的权限需要可执行权限,然后是文件里面的格式,需要LF换行模式,windows的是LC LF模式,否则会报找不到文件的莫名错误。
如果是打包成docker则需要如下代码
FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
WORKDIR /app
EXPOSE 80
RUN apt-get update
RUN apt-get install -y openssh-client
RUN apt-get install -y expect
COPY . .
FROM base AS final
WORKDIR /app
RUN chmod +x xxx.sh#给这个文件权限
ENTRYPOINT ["dotnet", "xxx.dll"]