manida
manida

Reputation: 41

fabric without fabfile - run and sudo gives error, but not local

Hi I am using following code in a python script (not running fab from command line), at line A when I change local to run or sudo, it gives me error like:

No hosts found. Please specify (single) host string for connection:

Code is:

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

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

execute()

My objective is to ssh into a single host machine.

Upvotes: 2

Views: 1927

Answers (2)

alecxe
alecxe

Reputation: 474041

According to the fabric docs, if you are calling tasks from the python script - you should use fabric.tasks.execute:

from fabric.tasks import execute
from fabric.api import *

env.user = "XXX"

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

execute(execute_task, host="XXX")

Hope that helps.

Upvotes: 8

Burhan Khalid
Burhan Khalid

Reputation: 174672

Why not use paramiko:

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)

Upvotes: 0

Related Questions