def __init__(self, host, user, password=None, ssh_private_key=None): """ Args: host (str): server host user (str): user name password (str): password, default None; If password is None, use ssh_key. ssh_private_key (Path, str): ssh public key path, default None; If ssh_key is None, use password. """ if password is None and ssh_private_key is None: logger.warning( "ssh_private_key and password are both none, ssh_private_key will use `~/.ssh/id_rsa`!" ) ssh_private_key = str(Path("~/.ssh/id_rsa").expanduser().resolve()) if ssh_private_key is not None: ssh_key = str(Path(ssh_private_key).expanduser().resolve()) connect_kwargs = {"key_filename": ssh_key} self.conn = Connection(host=host, user=user, connect_kwargs=connect_kwargs) else: config = Config(overrides={"sudo": {"password": password}}) connect_kwargs = {"password": password} self.conn = Connection(host=host, user=user, connect_kwargs=connect_kwargs, config=config) self._home_dir = None
def ssh_run(servername, cmd, sudo): try: user = os.environ['SSH_USER'] passw = os.environ['SSH_PASS'] sudotf = sudo command = cmd sname = servername print('user: %s passwd: %s servername: %s' % (user, passw, sname)) config = Config(overrides={'sudo': {'password': passw}}) c = Connection(host=sname, user=user, port=22, config=config, connect_kwargs={"password": passw}) if sudotf: result = c.sudo(command, pty=True, hide='stderr') if result.exited == 0: print("In SUDO") print(result.ok) print(result.stdout.strip()) return result.exited else: result = c.run(SSH_COMMAND, pty=True, hide='stderr') if result.exited == 0: print("In NOSUDO") print(result.ok) print(result.stdout.strip()) return result.exited except Exception as e: print("ssh_run Exception Caught: %s" % e) return "Exception Caught Clearing Certificate"
def config_test(): ''' 默认的配置文件~/.fabric.yml 只能使用固定的key,自己增加hosts,是不会被读入。 :return: ''' for c in Config.global_defaults().items(): print(c)
def __init__( self, host, user=None, port=None, config=None, gateway=None, forward_agent=None, connect_timeout=None, connect_kwargs=None, inline_ssh_env=None, key_filename=None, # part of connect_kwargs kwargs=None, ): connect_kwargs = set_default({}, connect_kwargs, { "key_filename": [File(f).abspath for f in listwrap(key_filename)] }) key_filenames = connect_kwargs.key_filename self.stdout = LogStream(host, "stdout") self.stderr = LogStream(host, "stderr") config = Config(**unwrap( set_default( {}, config, { "overrides": { "run": { # "hide": True, "out_stream": self.stdout, "err_stream": self.stderr, } } }, ))) self.warn = False cause = Except("expecting some private key to connect") for key_file in key_filenames: try: connect_kwargs.key_filename = File(key_file).abspath self.conn = _Connection( host, user, port, config, gateway, forward_agent, connect_timeout, connect_kwargs, inline_ssh_env, ) self.conn.run("echo") # verify we can connect return except Exception as e: cause = e Log.error("could not connect", cause=cause)
def connect_to_host(ctx): if isinstance(ctx, Connection): return ctx else: password = ENV_VALUES['password'] config = Config(overrides={'sudo': {'password': password}}) conn = Connection(ctx.host, ctx.user, connect_kwargs=ctx.connect_kwargs, config=config) return conn
def createConnect(user, host, password, port=22, timeout=120, keyfile='~/.ssh/id_rsa'): config = Config(overrides={'sudo': {'password': password}}) return Connection(host=host, user=user, port=22, config=config, connect_kwargs={ "key_filename": keyfile, "password": password }, connect_timeout=timeout)
def __init__( self, host, user=None, port=None, config=None, gateway=None, forward_agent=None, connect_timeout=None, connect_kwargs=None, inline_ssh_env=None, key_filename=None, # part of connect_kwargs kwargs=None, ): connect_kwargs = set_default( {}, connect_kwargs, {"key_filename": File(key_filename).abspath} ) self.stdout = LogStream(host, "stdout") self.stderr = LogStream(host, "stderr") config = Config(**unwrap(set_default( {}, config, {"overrides": {"run": { # "hide": True, "out_stream": self.stdout, "err_stream": self.stderr, }}}, ))) self.warn = False self.conn = _Connection( host, user, port, config, gateway, forward_agent, connect_timeout, connect_kwargs, inline_ssh_env, )
def setHostnameandHosts(host, n): hostfile = """127.0.0.1 localhost ::1 localhost ip6-localhost ip6-loopback ff02::1 ip6-allnodes ff02::2 ip6-allrouters 172.164.18.1 th01 172.164.18.2 th02 172.164.18.9 th03 172.164.18.10 th04""" config = Config(overrides={'sudo': {'password': '******'}}) with Connection(host=host, user='******', port=22, config=config, connect_kwargs={ "key_filename": "/Users/WANG/.ssh/id_rsa", "password": "******" }, connect_timeout=120) as c: c.sudo("echo \"%s\" | sudo tee /etc/hosts" % hostfile) c.sudo("hostnamectl set-hostname th0%d --transient --static " % n)
from fabric.api import sudo from fabric.operations import reboot from fabric2 import Connection, Config from invoke import Responder from fabric2.transfer import Transfer import os from time import sleep with open('./conf/master', 'r') as f: array = f.readline().split() masterHost = array[0] masterPort = array[1] user = array[2] host = array[3] config = Config(overrides={'user': user}) conn = Connection(host=host, config=config) configMaster = Config(overrides={ 'user': user, 'connect_kwargs': { 'password': '******' }, 'sudo': { 'password': '******' } }) master = Connection(host=masterHost, config=configMaster, gateway=conn) slaveConnections = [] configSlaves = Config(overrides={ 'user': user,
import os import math import time from enum import Enum from functools import singledispatch from typing import Union import crayons from fabric2 import Connection, Config from fabric2.util import get_local_user from invoke import Context from konverge.files import ConfigSerializer LOCAL = Context(Config()) KUBE_VERSION_MAP_DOCKER_CE = { '1.15': '18.06', '1.16': '18.09', '1.17': '19.03' } KUBE_VERSION_MAP_DOCKER_IO = { '1.15': '18.09', '1.16': '18.09', '1.17': '18.09' } def colorize_yes_or_no(msg, yes=True):
from fabric2 import Connection, Config config = Config(overrides={'sudo': {'password': '******'}}) c = Connection(host='server', user='******', port=22, config=config, connect_kwargs={"password": "******"}) result = c.sudo('whoami', pty=True, hide='stdout') print(result.exited) print(result.ok) #print(result.stdout.strip())