简体   繁体   English

在ssh会话中继承环境变量?

[英]Inherit environment variable in ssh session?

I need to deal with a lot of remote machines, and each machine shares a global environment variable ( like CONTROLLER_IP). 我需要处理很多远程机器,每台机器共享一个全局环境变量(如CONTROLLER_IP)。 When I try to ssh to the remote machine, I would like to set the CONTROLLER_IP according to current localhost setting. 当我尝试ssh到远程机器时,我想根据当前的localhost设置设置CONTROLLER_IP。 is there any way to make it happen? 有什么办法可以实现吗?

Example: 例:

In localhost host, I set ofc1=192.168.0.1, and ofc2=192.168.1.1 and I need to ssh to ofs1, ofs2. 在localhost主机中,我设置了c1 = 192.168.0.1和ofc2 = 192.168.1.1,我需要ssh到ofs1,ofs2。 I would like to do something like: 我想做的事情如下:

CONTROLLER_IP=$ofc1 ssh root@ofs1; CONTROLLER_IP=$ofc2 ssh root@ofs2

then I will get the CONTROLLER_IP setting in each ssh session. 那么我将在每个ssh会话中获得CONTROLLER_IP设置。 (the code shown above does not work...) (上面显示的代码不起作用......)

In /etc/sshd_config on the server you can define the list of accepted environment variables using the AcceptEnv setting, and then you can send environment variables like this: 在服务器上的/etc/sshd_config ,您可以使用AcceptEnv设置定义接受的环境变量列表,然后您可以发送如下环境变量:

CONTROLLER_IP=$ofc1 ssh -o SendEnv=CONTROLLER_IP root@ofs1

But this seems a bit overkill for your purposes. 但这似乎有点矫枉过正。

The alternative is to pass the variables in the remote command, like this: 另一种方法是在远程命令中传递变量,如下所示:

ssh root@ofs1 "CONTROLLER_IP=$ofc1 somecmd"

Or if you run multiple remote commands then like this: 或者如果你运行多个远程命令,那么像这样:

ssh root@ofs1 "export CONTROLLER_IP=$ofc1; cmd1; cmd2; cmd3; ..."

If you need to quote the value of the variable, you can do like this: 如果需要引用变量的值,可以这样做:

ssh root@ofs1 "CONTROLLER_IP='$ofc1' somecmd"

Try 尝试

 ssh root@ofs1 "env CONTROLLER_IP=$ofc1 somescript"

(assuming $ofc1 is evaluated to some IP address like 12.234.56.178 without spaces or naughty characters) (假设$ofc1被评估为某个IP地址,如12.234.56.178没有空格或顽皮字符)

or perhaps 也许

 ssh root@ofs1 "env CONTROLLER_IP='$ofc1' somescript"

if $ofc1 could contain spaces or naughty characters 如果$ofc1可以包含空格或顽皮字符

where somescript is a script on the remote machine ofs1 ; somescript是远程机器ofs1上的脚本; if you want an interactive shell try 如果你想要一个交互式shell试试

 ssh root@ofs1 "env CONTROLLER_IP='$ofc1' /bin/bash"

At last, ssh is usually setting some environment variables (on the remote machine), notably the SSH_CONNECTION one. 最后, ssh通常设置一些环境变量(在远程机器上),特别是SSH_CONNECTION You could use it on the remote machine. 您可以在远程计算机上使用它。 Its third field is the IP address of the origin host (the one on which you do the ssh ...). 它的第三个字段是原始主机的IP地址(您执行ssh那个...)。 So perhaps the .bashrc on the remote host might contain 所以也许远程主机上的.bashrc可能包含

 if [ -n "$SSH_CONNECTION" ]; then
   export CONTROLLER_IP=$(echo $SSH_CONNECTION|cut -f3 -d' ')
 fi

better yet, replace the CONTROLLER_IP occurrences in your remote scripts with something using SSH_CONNECTION 更好的是,使用SSH_CONNECTION替换远程脚本中的CONTROLLER_IP事件

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

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