0

I spent some time reading Fabric's documentation and I found that, by default (?) the channel object won't have all the environment variables as a logged-in shell (see http://www.fabfile.org/faq.html#faq-bash )

I have the following code:

from fabric2 import Connection

self.ssh = Connection(
    host=kwargs["host"],
    user="admin",
    connect_kwargs=conn,
)

self.ssh.run("env", hide=False)

which indeed confirms that the ssh session doesn't have all environment variables that I need.

How can I make Fabric run my commands inside a shell with my user's environment variables? (actual code example)

Note: I don't want to modify anything on the server side. I just want to be able to run commands from Fabric in the same environment I'd get if I'd use a normal terminal (iTerm2, Yakuake, PuTTY, whatever).

alexandernst
  • 14,352
  • 22
  • 97
  • 197
  • @MartinPrikryl Once again, from that link: point 1: how can I do that with Fabric? point 2, 3 and 4 require modifications at the server side, which is NOT what I'm asking. point 5: Adding get_pty to Fabric doesn't seem to be working. – alexandernst Mar 19 '20 at 15:22
  • Points 3 and 4 are purely client-side. Point 3 is what you ask for. Though it's still a workaround only. The right solution is to fix your server. – Martin Prikryl Mar 19 '20 at 15:49
  • @MartinPrikryl Great! How can I apply point 3 to Fabric? (actual code) – alexandernst Mar 19 '20 at 16:50
  • `self.ssh.run("bash --login -c \"env\"", hide=False)` – Martin Prikryl Mar 19 '20 at 17:09
  • @MartinPrikryl Okey, so what happens if the server I'm connecting to doesn't have `bash`? How do regular terminals open a shell with the "correct" interpreter? – alexandernst Mar 19 '20 at 18:08
  • Human-facing terminal client applications use terminal emulation in "shell" SSH channel, which takes care of running the default shell. Both the "shell" channel and terminal emulation are intended for an interactive human use. Not for automating commands execution. If you use terminal emulation, you will get unpredictable side effects. You code might break anytime in the future, once the server administrator decides to configure yet another human-friendly fancy terminal feature. See for example https://stackoverflow.com/q/33291631/850848. – Martin Prikryl Mar 20 '20 at 07:26
  • @MartinPrikryl Ok. Let me rephrase my entire question. I need to run a script while having all the env vars (like I'd do in a regular terminal). And I can't make assumptions about the installed shell. What is the right way of doing that? – alexandernst Mar 20 '20 at 10:35
  • Sorry, but you are still asking the same question. I have already wrote you couple of times, that the right way is to fix your server to give your the same environment for both interactive and non interactive sessions. All others ways are workarounds. If you do not want to go the right way, you will have to pick the workaround that suits your needs the best and ask a more specific question for that. – Martin Prikryl Mar 20 '20 at 10:50
  • @MartinPrikryl I really can't believe it's *that* hard to just run a command via ssh in a logged-in shell (without modifying the server and without making assumptions about the installed shell). – alexandernst Mar 20 '20 at 13:48

0 Answers0