Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python fabric没有fabfile-run和sudo给出错误,但不是本地错误_Python_Ssh_Fabric - Fatal编程技术网

Python fabric没有fabfile-run和sudo给出错误,但不是本地错误

Python fabric没有fabfile-run和sudo给出错误,但不是本地错误,python,ssh,fabric,Python,Ssh,Fabric,您好,我正在python脚本中使用以下代码(不是从命令行运行fab),在a行,当我将local更改为run或sudo时,会出现如下错误: 找不到主机。请为连接指定(单个)主机字符串: 代码是: env.host = "XXXX" env.user = "XXX" def execute(): local('uname -a') ### A : changing this gives error ### execute() 我的目标是将ssh连接到一台主机上。根据结

您好,我正在python脚本中使用以下代码(不是从命令行运行fab),在a行,当我将
local
更改为
run
sudo
时,会出现如下错误:

找不到主机。请为连接指定(单个)主机字符串:

代码是:

env.host = "XXXX"
env.user = "XXX"

def execute():
    local('uname -a')   ### A : changing this gives error ###       

execute()
我的目标是将ssh连接到一台主机上。

根据结构,如果您是从python脚本调用任务,那么应该使用:

希望有帮助。

为什么不使用:


thanx!!!但我的问题是-‘local’命令正在工作,但sudo/run不工作,特别是关于为什么sudo/run不工作nx的任何帮助!!!ssh设置有问题,或者您也可以尝试设置env.password变量
from fabric.tasks import execute
from fabric.api import *

env.user = "XXX"

def execute_task():
    sudo('uname -a')

execute(execute_task, host="XXX")
import sys
import traceback

import paramiko

paramiko.util.log_to_file('session.log')

username = 'someuser'
port = 22
hostname = 'foo.bar.com'

try:
    client = paramiko.SSHClient()
    client.load_system_host_keys()
    client.set_missing_host_key_policy(paramiko.WarningPolicy)
    client.connect(hostname, port, username, password)
    chan = client.invoke_shell()
    print repr(client.get_transport())
    print '*** Here we go!'
    print
    interactive.interactive_shell(chan)
    chan.close()
    client.close()

except Exception, e:
    print '*** Caught exception: %s: %s' % (e.__class__, e)
    traceback.print_exc()
    try:
        client.close()
    except:
        pass
    sys.exit(1)