Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

run() command fails if environment variable value contains an ampersand (&) #2295

Open
dtenenba opened this issue Jan 26, 2024 · 2 comments
Open

Comments

@dtenenba
Copy link

dtenenba commented Jan 26, 2024

ETA: I've been able to pass an environment variable with a & in it in Invoke and paramiko, so it does seem like a Fabric issue.

Describe the bug

run() on a connection fails if you pass an environment variable (with the env= argument) whose value contains an ampersand (&).

To Reproduce
Steps to reproduce the behaviour (please attach a minimal example):

Run this. Change the hostname, username (if necessary) and key or password:

#!/usr/bin/env python3

import fabric

# TODO - edit this to match your system
conn = fabric.Connection('my.host.name', user="root", connect_kwargs={"key_filename": "ssh_key"})  

env = {"FOO": "has an ampersand & iamnotacommand blah"}

try:
    conn.run("hostname", env=env)
except Exception as e:
     print(e)

Prints out this:

bash: iamnotacommand: command not found
Encountered a bad command exit code!

Command: 'hostname'

Exit code: 127

Stdout: already printed

Stderr: already printed

Remove the & and it works fine.

Expected behaviour

It should print out the hostname.

Environment

  • What version of the Python interpreter are you using? Are you using an
    alternative interpreter such as PyPy?
Python 3.12.0 (main, Nov 21 2023, 17:38:35) [GCC 12.2.0] on linux
  • What operating system are you using both client & server-side?

Client:

PRETTY_NAME="Debian GNU/Linux 12 (bookworm)"
NAME="Debian GNU/Linux"
VERSION_ID="12"
VERSION="12 (bookworm)"
VERSION_CODENAME=bookworm
ID=debian

Server:

DISTRIB_DESCRIPTION="Ubuntu 18.04.6 LTS"
NAME="Ubuntu"
VERSION="18.04.6 LTS (Bionic Beaver)"
  • Are you using OpenSSH server or something else?

yes

  • Which version or versions of the software are you using?

3.2.2

  • Have you already tried the latest release?

yes

  • Have you, or can you, try some older releases to pin down where the bug
    appeared?

I have not

  • How can the developers recreate the bug on their end? If possible, include a
    copy of your code, the command you used to invoke it, and the full output of
    your run (if applicable.)

see above

  • A common tactic is to pare down your code until a simple (but still
    bug-causing) “base case” remains. Not only can this help you identify
    problems which aren’t real bugs, but it means the developer can get to fixing
    the bug faster.

see above

@auioe
Copy link

auioe commented Feb 4, 2024

Add parameter inline_ssh_env=False while initializing your connection. Or fabric only add a simple export command in front of hostname command.

@dtenenba
Copy link
Author

dtenenba commented Feb 4, 2024

When I set inline_ssh_env=False I don't get an error, but the variable is not set in the remote shell.
This works, but only when inline_ssh_env=False:

conn.run("export 'FOO=b&ar' && echo $FOO")

This still seems like a bug and your suggestion is a workaround.
Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants