0
import sys
from fabric.api import *

env.hosts = ['my.host.com']
env.user = 'myuser'
env.password = 'mypassword'
# env.shell = '/bin/bash -l -c'

def deploy():
    x = run("cd /srv/proj/")
    print x.__dict__

I'm trying to login into remote shell and execute this simple cd command. although it shows that there is no error

[my.host.com] run: cd /srv/proj/
{'succeeded': True, 'return_code': 0, 'failed': False, 'command': 'cd /srv/proj/', 'stderr': '', 'real_command': '/bin/bash -l -c "cd /srv/proj/"'}

but when I execute run('ls') after cd command, it prints nothing but definitely there are files. so what is happening here. apart from that I'm having issues in executing manually set command (i mean alias in .bashrc file). fabric uses /bin/bash -l -c .... how can I overcome this hurdles.

I'm using ubuntu 14.04

ps: it's not same as os.chdir

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Wendy
  • 1,523
  • 3
  • 12
  • 16
  • 1
    Replace `run("cd /srv/proj/")` with `os.chdir("/srv/proj/")` or merge 2 commands as `run("cd /srv/proj/; ls")` – anishsane Jul 14 '16 at 08:57

1 Answers1

1

You could maybe try with cd :

def deploy():
    with cd("/srv/proj/"):
        x = run("ls")
    print(x)
pwnsauce
  • 416
  • 4
  • 14
  • great, it worked, can you please explain the things what is happening here? why we need `with`, tag? – Wendy Jul 18 '16 at 11:43
  • For all the `with cd` block, you are in the `/srv/proj/` directory, so every commande you'll run will be executed there. When you leave the block, you'll be back in the previous directory you came from. (I can't tell you if this appends `cd /srv/proj/` before the commande, because I don't know how it does it.) – pwnsauce Jul 18 '16 at 12:18
  • also, cd can not return anything (exepted a execution code) so `x = run(cd ..)` has not a lot of sense beyond checking if it's 0 or 1 – pwnsauce Jul 18 '16 at 15:19