通过 2 个跳转主机/密码提前自动登录

通过 2 个跳转主机/密码提前自动登录

我需要找到一种在远程计算机上自动登录的方法。我们可以通过多种方法来做到这一点,但这对我来说有点棘手。

自动登录到远程计算机并执行命令或脚本并将输出重定向到本地系统文件中。

ssh remote-host < ./script >> storageinfo_$date.txt

但困难的部分是我们不能直接连接到远程主机;我们需要首先连接到Jumphost1--> Jumphost2--> 然后 -->remote-host

Jumphostx 已启用 sshkeygen,但密码短语例如:userpass
远程主机未启用 sshkeygen,例如:remotepass

我们曾经.ssh/config通过以下方式对文件执行此操作。到目前为止,这在测试环境中是成功的。但我们不应该在实时环境中安装expect。

# cat .ssh/config

Host jump1-*
    User ldap-user
    IdentityFile ~/.ssh/id_rsa
    ForwardAgent yes
    ServerAliveInterval 60
    ServerAliveCountMax 12

Host jump1-centos01-temporary 
    Hostname 13.1.2.19
    Port 2222

Host jump1-centos01        
    Hostname 1.2.1.18
    Port 22
    ProxyCommand ssh -W %h:%p jump1-centos01-temporary

Host remote-host
    Hostname 12.1.1.8
    ProxyCommand ssh -W %h:%p jump1-centos01
    User root

具有期望和发送模式的 ssh 连接

# cat expect.sh 

#!/usr/bin/env expect
set timeout 7
set date [exec date "+%d-%B-%Y"]

spawn sh -c "ssh va1ap-vsns0001n < ./isi.py > storageinfo_$date.txt"
expect "Enter passphrase for key '/root/.ssh/id_rsa':"
send "\r"
expect "Enter passphrase for key '/root/.ssh/id_rsa':"
send "userpass\r"
expect "Enter passphrase for key '/root/.ssh/id_rsa':"
send "userpass\r"
expect "Password:"
send "remotepass\r"
interact

答案1

ssh -t $jump1 ssh -t $jump2 ssh -t $tgt

将实现您正在寻找的联系;确保分配了 pty,如果要求在目的地执行命令,则-t通常不会分配 pty 。ssh

在这种情况下,代理和代理转发似乎完全合适,因此它看起来更像是这样:

ssh -A -t $jump1 ssh -A -t $jump2 ssh -t $tgt

如果这对您不起作用,script可以用作expectin的替代方案一些情况。例如,echo $passwd | script -q -f -c "ssh -q -t $host sudo -p \"''\" \"$some_command\"这是我需要用于非交互目的的模式。

不过,实际上,我会推动配置某种类型的基于密钥或单点登录身份验证;您描述的工作流程听起来像是一场噩梦,特别是如果您的测试和生产环境需要如此不同。

相关内容