使用期望在bash脚本提供密码SSH命令 [英] Use expect in bash script to provide password to SSH command

查看:161
本文介绍了使用期望在bash脚本提供密码SSH命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于那些谁想要回复,我应该使用SSH密钥请弃权

我试图使用期望在bash脚本提供SSH密码。提供密码的作品,但我应该,它可以追溯到两岸来砸我没有在SSH会话结束。

I'm trying to use expect in an bash script to provide the SSH password. Providing the password works but I don't end up in the SSH session as I should, it goes back strait to bash.

我的脚本:

#!/bin/bash

read -s PWD

/usr/bin/expect <<EOD
spawn ssh -oStrictHostKeyChecking=no -oCheckHostIP=no usr@$myhost.example.com'
expect "password"
send "$PWD\n" 
EOD
echo "you're out"

我的脚本的输出:

The output of my script:

spawn ssh -oStrictHostKeyChecking=no -oCheckHostIP=no usr@$myhost.example.com
usr@$myhost.example.com's password: you're out

我想有我的SSH会话,只有当我离开它去回到我的bash脚本。
为什么我使用bash之前预期的原因是因为我有使用菜单,我可以选择连接到哪个单位。

I would like to have my SSH session and only when I exit it to go back to my bash script. The reason why I am using bash before expect is because I have use a menu I can choose which unit to connect to.

感谢

推荐答案

混合bash和期望是不是达到预期的效果的好方法。我想尝试只用Expect:

Mixing bash and expect is not a good way to achieve the desired effect. I'd try to use only Expect:

#!/usr/bin/expect
eval spawn ssh -oStrictHostKeyChecking=no -oCheckHostIP=no usr@$myhost.example.com
#use correct prompt
set prompt ":|#|\\\$"
interact -o -nobuffer -re $prompt return
send "my_password\r"
interact -o -nobuffer -re $prompt return
send "my_command1\r"
interact -o -nobuffer -re $prompt return
send "my_command2\r"
interact

对于bash样品溶液可能是:

Sample solution for bash could be:

#!/bin/bash
/usr/bin/expect -c 'expect "\n" { eval spawn ssh -oStrictHostKeyChecking=no -oCheckHostIP=no usr@$myhost.example.com; interact }'

这将等待进入,比回报(一会儿)交互式会话。

This will wait for enter and than return (for a moment) interactive session.

这篇关于使用期望在bash脚本提供密码SSH命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆