简体   繁体   English

在Bash脚本中将Telnet命令传递给SSH

[英]Pass Telnet Command to SSH in Bash Script

I'm trying to write a bash to first ssh into a server and then telnet from that server to a ElastiCache Redis endpoint. 我正在尝试先将bsh写入bash,然后再从该服务器telnet到ElastiCache Redis端点。

My Code is the following: telnet.sh 我的代码如下:telnet.sh

#!/bin/bash
...
ssh -t -i $key_home ec2-user@$private_ip << EOF 
        telnet $endpoint $_port
    EOF

I would like to call my bash script and have the user interactively be connected to the Redis Cluster so that the user can enter redis cli commands after invoking telnet.sh. 我想调用我的bash脚本,并使用户以交互方式连接到Redis群集,以便用户可以在调用telnet.sh后输入redis cli命令。

The above code opens and connects to the redis cluster but immediately closes the ssh session. 上面的代码打开并连接到Redis集群,但立即关闭ssh会话。 Is there a way to stay connected to the Redis Cluster and direct input back to the user? 有没有办法保持与Redis群集的连接并将输入直接返回给用户?

Thanks! 谢谢!

Don't override stdin with a heredoc -- you need it for user interaction. 不要用Heredoc覆盖stdin-您需要它来进行用户交互。 Instead, pass your script to run remotely as a command-line argument to ssh. 而是将您的脚本作为ssh的命令行参数传递给远程运行。

#!/bin/bash
#      ^^^^-- bash, not /bin/sh, to ensure that printf %q is available
#             ...ksh can also be used if modified to not need printf -v.

# generate a script to run remotely
printf -v cmd 'telnet %q %q' "$endpoint" "$_port"

# run that script
ssh -t -i "$key_home" "ec2-user@$private_ip" "$cmd"

Using printf %q to generate an eval -safe version of our variables ensures that content is passed through to the telnet command exactly as-given -- ensuring that even if endpoint='$(rm -rf $HOME)' , that value is interpreted by the remote shell as a constant string. 使用printf %q生成变量的eval -safe版本可确保将内容按原样传递给telnet命令-确保即使endpoint='$(rm -rf $HOME)' ,该值也是远程外壳将其解释为常量字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM