shell自动输入ssh密码
时间: 2023-08-08 21:09:57 浏览: 189
为了自动输入 SSH 密码,可以使用 SSH 密钥对来避免输入密码。如果你不想使用 SSH 密钥对,可以使用 expect 工具来自动输入密码,具体步骤如下:
1. 安装 expect 工具,例如在 Ubuntu 上可以使用以下命令进行安装:
```
sudo apt-get install expect
```
2. 创建一个名为 ssh-login.exp 的 expect 脚本文件,文件内容如下:
```
#!/usr/bin/expect
set timeout 30
set host [lindex $argv 0]
set user [lindex $argv 1]
set password [lindex $argv 2]
spawn ssh $user@$host
expect {
"(yes/no)?" {
send "yes\r"
expect "*assword:" { send "$password\r" }
}
"*assword:" { send "$password\r" }
}
interact
```
3. 给 expect 脚本文件设置可执行权限:
```
chmod +x ssh-login.exp
```
4. 运行 expect 脚本文件,使用以下命令进行登录:
```
./ssh-login.exp [hostname] [username] [password]
```
其中,[hostname] 是 SSH 主机名或 IP 地址,[username] 是 SSH 用户名,[password] 是 SSH 密码。注意,这种方法会将密码明文存储在脚本文件中,因此不太安全,建议使用 SSH 密钥对进行身份验证。
阅读全文
相关推荐

















