这是我的脚本:
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上运行命令?
答案 0 :(得分:2)
在Fabric2中,Connection
对象具有一个local()
方法。
看一下该对象的文档here。
答案 1 :(得分:1)
从2020年7月开始,使用fabric2,如果默认情况下您不将参数传递给任务装饰器,则您位于本地计算机上。
例如,以下内容将在本地计算机(本地主机)上运行:
示例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ïl所述,在传递hosts参数时,也可以使用“本地”方法,尽管您已将host参数指定给任务装饰器,但“ local”方法将在本地主机上运行。请注意,如果未指定任何主机参数,则不能使用'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