#!/usr/bin/expect -f # SSH连接NAS自动化脚本 # 配置参数 set host "192.168.2.154" set user "admin" set password "sanguo123" set command "ls -la" # 启动SSH连接 spawn ssh $user@$host $command # 处理密码提示 expect { "password:" { send "$password\r" } "yes/no" { send "yes\r" expect "password:" send "$password\r" } timeout { puts "连接超时" exit 1 } eof { puts "连接失败" exit 1 } } # 等待命令执行完成 expect eof # 检查退出码 if { $expect_out(buffer) contains "total" } { puts "✅ SSH连接成功" puts "✅ 命令执行完成" exit 0 } else { puts "❌ SSH连接失败" puts "❌ 命令执行失败" puts "输出: $expect_out(buffer)" exit 1 }