使用Expect脚本从远程计算机执行SSH [英] Using expect script to do an ssh from a remote machine

查看:153
本文介绍了使用Expect脚本从远程计算机执行SSH的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是期望脚本的新手,有一个用例,我需要在已经使用期望脚本执行过ssh的计算机上执行ssh.这是我的代码段

I am new to expect scripts and have a use case in which I need to do an ssh from a machine in which I have already done an ssh using expect script. This is my code snippet

#!/usr/bin/expect -f
set timeout 60
spawn ssh username@machine1.domain.com

expect "Password: "
send "Password\r"

send "\r" # This is successful. I am able to login successfully to the first machine

set timeout 60
spawn ssh username@machine2.domain.com  #This fails

这需要花费一些时间,并且无法说 ssh:连接到host2.domain.com端口22:操作超时.我知道22是ssh运行的默认端口,我可以通过给ssh提供-p选项来手动覆盖它.

This takes a some amount of time and fails saying ssh: connect to host machine2.domain.com port 22: Operation timed out. I understand that 22 is the default port on which ssh runs and I can manually override it by giving a -p option to ssh.

如果我尝试在没有期望脚本的情况下独立进行ssh操作,则会出现提示要求我输入的提示(是/否).如果我不使用期望脚本直接执行ssh,将从何处获取正确的端口.如果我不需要在shell上输入端口号,那么如果我使用的是期望脚本,为什么还要输入端口号.

If I try to ssh independently without the expect script I get a prompt that asks me to enter (yes/no). From where is the correct port being picked up if I execute ssh directly without the expect script. If I do not need to enter the port number on shell why would it be needed to enter a port number if I am using an expect script.

推荐答案

那一点,您不会产生新的ssh:spawn在本地计算机上创建了一个新进程.您只需将命令发送到远程服务器

That that point, you don't spawn a new ssh: spawn creates a new process on your local machine. You just send a command to the remote server

#!/usr/bin/expect -f
set timeout 60
spawn ssh username@machine1.domain.com

expect "Password: "
send "Password\r"

send "\r" # This is successful. I am able to login successfully to the first machine

# at this point, carry on scripting the first ssh session:
send "ssh username@machine2.domain.com\r"
expect ...

这篇关于使用Expect脚本从远程计算机执行SSH的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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