下面是我的脚本:
from fabric2 import Connection
c = Connection('127.0.0.1')
with c.cd('/home/bussiere/'):
c.run('ls -l')
但是我有这个错误:
paramiko.ssh_exception.AuthenticationException: Authentication failed.
那么如何在localhost上运行命令呢?
发布于 2018-07-29 00:36:52
在Fabric2中,Connection
对象有一个local()
方法。看一下这个对象的文档here。
发布于 2020-07-13 23:50:01
在2020年7月,使用fabric2时,如果您不将参数传递给任务修饰器,则默认情况下您是在本地计算机上。
例如,以下代码将在本地计算机(localhost)上运行:
示例1:仅在本地
#python3
#fabfile.py
from fabric import task, Connection
c = Connection('remote_user@remote_server.com')
@task
def DetailList(c):
c.run('ls -l') # will run on local server because the decorator @task does not contain the hosts parameter
然后,您可以使用以下命令在您的机器上运行
fab DetailList
如果你想混合应该在远程服务器和本地服务器上运行的代码,你应该将连接作为一个参数传递给@task装饰器。
示例2:在本地和远程上(但功能不同)
#python3
#fabfile.py
#imports
from fabric import task, Connection
#variables
list_of_hosts = ['user@yourserver.com'] #you should have already configure the ssh access
c = Connection(list_of_hosts[0])
working_dir = '/var/www/yourproject'
#will run on remote
@task(hosts = list_of_hosts)
def Update(c):
c.run('sudo apt get update') # will run on remote server because hosts are passed to the task decorator
c.run(f'cd {working_dir} && git pull') # will run on remote server because hosts are passed to the task decorator
c.run('sudo service apache2 restart') # will run on remote server because hosts are passed to the task decorator
#will run on local because you do not specify a host
@task
def DetailsList(c):
c.run('ls -l') # # will run on local server because hosts are NOT passed to the task decorator
正如Isma a所提到的,还有一个“local”方法可以在传递hosts参数时使用,“local”方法将在localhost上运行,尽管您已经为任务修饰器指定了host参数。请注意,如果您没有指定任何主机参数,则不能使用'local‘方法,而应使用run,如示例1和2所示。
示例3:在远程和本地服务器上都使用,但在相同的函数下,请注意,我们不会修饰在UpdateAndRestart函数中调用的函数。
#python3
#fabfile.py
#imports
from fabric import task, Connection
#variables
list_of_hosts = ['www.yourserver.com'] #you should have already configure the ssh access
c = Connection(list_of_hosts[0])
working_dir = '/var/www/yourproject'
def UpdateServer(c):
c.run('sudo apt get update') # will run on remote server because hosts are passed to the task decorator
c.local('echo the remote server is now updated') # will run on local server because you used the local method when hosts are being passed to the decorator
def PullFromGit(c):
c.run(f'cd {working_dir} && git pull') # will run on remote server because hosts are passed to the task decorator
c.local('echo Git repo is now pulled') # will run on local server because you used the local method when hosts are being passed to the decorator
def RestartServer(c):
c.run('sudo service apache2 restart') # will run on remote server because hosts are passed to the task decorator
c.local('echo Apache2 is now restarted') # will run on local server because you used the local method when hosts are being passed to the decorator
@task(hosts = list_of_hosts)
def UpdateAndRestart(c):
UpdateServer(c)
PullFromGit(c)
RestartServer(c)
c.local('echo you have updated, pulled and restarted Apache2') # will run on local server because you used the local method when hosts are being passed to the decorator
您将能够使用以下命令运行整个堆栈:
fab UpdateAndRestart
https://stackoverflow.com/questions/51536696
复制相似问题