Golang SSH加载LD_PRELOAD和LD_LIBRARY_PATH环境变量。

huangapple go评论154阅读模式
英文:

Golang SSH load LD_PRELOAD and LD_LIBRARY_PATH environment variables

问题

我正在尝试使用Golang SSH包连接到远程服务器,但是我的工作站和远程服务器之间有一个SOCKS代理。

我可以通过设置LD_PRELOAD和LD_LIBRARY_PATH并运行以下命令来连接到服务器:

$ export LD_PRELOAD="/path/to/lib"
$ export LD_LIBRARY_PATH="/path/to/lib"
$ ssh user@hostname

但是,当我在Go代码中设置这些变量时,它不起作用:

os.Setenv("LD_PRELOAD", "/path/to/file")
os.Setenv("LD_LIBRARY_PATH", "/path/to/file")

如果我在Go代码中设置这些变量并尝试以下操作,它可以工作:

ssh := exec.Command("ssh", "hostname")
output, _ := ssh.Output()
fmt.Println(string(output))

ssh的PermitUserEnvironment设置为yes。

有没有办法“强制”Golang SSH使用这些环境变量?

英文:

I'm trying to connect to a remote server using Golang SSH package, but there's a SOCKS between my workstation and this remote server.

I'm able to connect to the server by simply setting a LD_PRELOAD and LD_LIBRARY_PATH and then running:

$ export LD_PRELOAD="/path/to/lib"
$ export LD_LIBRARY_PATH="/path/to/lib"
$ ssh user@hostname

But when I set these variables within the Go code, it doesn't work:

os.Setenv("LD_PRELOAD", "/path/to/file")
os.Setenv("LD_LIBRARY_PATH", "/path/to/file")

If I set these variables within the Go code and try the following, it works:

ssh := exec.Command("ssh", "hostname")
output, _ := ssh.Output()
fmt.Println(string(output))

The ssh PermitUserEnvironment is set as yes

Is there any way to "force" the Golang SSH to use these environment variables?

答案1

得分: 1

LD_PRELOADLD_LIBRARY_PATH是在程序启动时由动态链接器处理的环境变量。如果你在程序内部设置这些环境变量,它们对程序本身没有影响,因为链接器没有看到它们。

另一方面,这些环境变量会影响你从程序内部运行的外部应用程序(例如ssh命令),因为链接器负责解析应用程序使用的共享库。

如果你在运行Go程序之前设置这些环境变量,我认为你会达到预期的效果。(这仅适用于编译的程序链接到共享的标准C库。有关更多详细信息,请参阅@JimB的评论。)

英文:

(Edit: This answer does not necessarily apply to Go 1.8 and above. See comments for discussion)

LD_PRELOAD and LD_LIBRARY_PATH are environment variables processed by the dynamic linker when a program is starting up. If you set those environment variables inside the program, they don't have an effect on the program itself since the linker didn't see them.

On the other hand, the environment variables will affect external applications that you run (the ssh command for example) from within the program, since the linker is given control to resolve the shared libraries that the application uses.

<s>If you instead set those environment variables before running the Go program, I think you'll have the desired effect.</s> (This is only applicable if the compiled program is linked to the shared standard C library. See @JimB's comments below for more details.)

huangapple
  • 本文由 发表于 2017年8月2日 01:42:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/45444513.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定