10

I am installing certificates on a remote server and want to check whether they exist before I overwrite them. The server only allows non-root access via ssh public key. I can sudo -s to root once in a shell. Root is required because /etc/ssl is not readable by anyone else. This is being developed in python fabric, so any command that can be run in a shell command via sudo would work. I don't mind typing in passwords at prompts in this case.

TL;DR: I need an sh command that can tell my python program whether a remote file (or directory) exists when run as if fabric.sudo(sh_command) == True: (or something similar).

Thank you!

mh00h
  • 1,824
  • 3
  • 25
  • 45

3 Answers3

36
from fabric.contrib.files import exists

def foo():
    if exists('/path/to/remote/file', use_sudo=True):
        #command
Yuichiro
  • 1,220
  • 9
  • 8
  • Link to doc: http://docs.fabfile.org/en/latest/api/contrib/files.html?highlight=exists#fabric.contrib.files.exists – ecstaticpeon Dec 09 '13 at 17:04
  • This function was moved to the patchwork library as of fabric v2.0 https://github.com/fabric/patchwork/blob/master/patchwork/files.py – Ben Mar 03 '22 at 11:34
1

Maybe not the simplest way, but out of my head, I would suggest

ssh user@server 'bash -c "if [ -e /path/to/remote/file ] ; then true ; fi"'
Eric Fournie
  • 1,362
  • 8
  • 10
0

Run a command like test in Linux to figure out whether a directory exists or not. The output of fabric.sudo is a multline-string, that can be parsed for the return status.

Prahalad Deshpande
  • 4,709
  • 1
  • 20
  • 22