简体   繁体   English

在 ssh 会话中,如何设置 env 变量并运行 shell 命令?

[英]On a ssh session how can I set env variables and run shell commands?

I need to :我需要 :

  1. ssh to a remote server (currently I have used paramiko.SSHClient) ssh 到远程服务器(目前我使用了 paramiko.SSHClient)
  2. For the session set env variables that's required to run a set of subsequent commands/scripts对于运行一组后续命令/脚本所需的会话设置环境变量
  3. Run the commands/scripts.运行命令/脚本。

My env settings are temporary for that session alone, so I guess modifying ~/.ssh/environment is not an option.我的 env 设置仅针对该会话是临时的,所以我想修改~/.ssh/environment不是一个选项。 <<< Please correct if I am wrong. <<<如果我错了请指正。

Also the server is password protected, so I guess subprocess.Popen(ssh) is also out of the question?服务器也受密码保护,所以我猜 subprocess.Popen(ssh) 也是不可能的? Again, I am still trying things, please correct me.再说一遍,我还在尝试一些东西,请纠正我。

I have tried with no success:我试过没有成功:

a. ssh.exec_command(env_export_commands)
b. ssh.exec_command(command=dummy,environment=env_export_commands_dictionary).

But after either of the above, I still see the old values in 'env'.但是在上述任何一项之后,我仍然看到“env”中的旧值。 Any pointers to what I am missing is highly appreciated.任何指向我所缺少的内容的指针都非常感谢。

Learnt that each exec_command is executed in a different shell,so setting and checking env ( or any other command that uses the newly set env variables) need to get executed in single exec_command了解到每个 exec_command 在不同的 shell 中执行,因此设置和检查 env(或任何其他使用新设置的 env 变量的命令)需要在单个 exec_command 中执行

ie : IE :

 command_set = "export env1=value1; env;" + script_using_env 

 stdin, stdout, stderr = ssh.exec_command(command_set)

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

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