- 安装expect
sudo apt install expect
- 脚本示例
推送文件到服务器
#!/usr/bin/expect -f
cd netcore-jenkins
spawn scp -r ./release/ root@127.0.0.1:/opt/
expect "*password:"
send "123456\n"
expect eof
ssh连接到服务器并且执行命令
#!/usr/bin/expect
set timeout 30
spawn ssh root@127.0.0.1
#判断输出的信息
expect{
"yes/no" #是否需要保存主机指纹 exp_continue用于继续匹配 expect中的规则
{
send "yes\r"
exp_continue
}
"password:" #是否需要输入密码
{
send "password\r"
}
}
expect "root" #出现root认为登录成功
puts "登录成功"
send "mkdir demo\r" #创建文件夹,也可为其他指令
expect "root" #打印root认为成功
#interact #interact可以保留当前会话,进入命令交互
send "exit\r"
expect "close" #退出成功
expect eof
3.相关文章
expect 参数 https://www.cnblogs.com/vmsysjack/articles/17289417.html
使用expect脚本自动化SSH登录与文件传输,
文章介绍了如何利用expect工具进行自动化操作,包括通过sudoaptinstallexpect安装expect,然后展示了两个脚本示例,一个是用expect配合scp将文件推送到服务器,另一个是使用expect进行ssh连接并执行命令,如创建目录。文章还提到了expect在处理主机指纹和密码输入时的关键匹配与交互功能。
2129

被折叠的 条评论
为什么被折叠?



