我们从Python开源项目中,提取了以下5个代码示例,用于说明如何使用paramiko.MissingHostKeyPolicy()。
def upload(host, username, password): import paramiko client = paramiko.SSHClient() client.set_missing_host_key_policy(paramiko.MissingHostKeyPolicy()) logging.info('connect %s', host) client.connect(host, 22, username, password) client.exec_command('mkdir -p /opt/goagent/{vps,log}') logging.info('open sftp...') sftp = client.open_sftp() logging.info('open sftp ok') sftp.chdir('/opt/goagent/vps') uploadlist = ['../local/proxylib.py', 'vps/*'] for filename in sum((glob.glob(x) for x in uploadlist), []): logging.info('upload %s', filename) sftp.put(filename, '/opt/goagent/vps/%s' % os.path.basename(filename)) cmds = ['/bin/cp -f /opt/goagent/vps/sysctl.conf /etc/', '/bin/cp -f /opt/goagent/vps/limits.conf /etc/security/', '/bin/ln -sf /opt/goagent/vps/goagentvps.sh /etc/init.d/goagentvps', 'chmod +x /opt/goagent/vps/goagentvps.sh', 'which update-rc.d && update-rc.d goagentvps defaults' 'which chkconfig && chkconfig goagentvps on' 'sysctl -p'] client.exec_command(' ; '.join(cmds)) client.exec_command('/etc/init.d/goagentvps stop') client.exec_command('/etc/init.d/goagentvps start')
def __init__(self, host, username=None, password=None, pkey=None, port=22, timeout=15, connect=True): self.host = host self.username = username self.password = password if pkey: if isinstance(pkey, paramiko.rsakey.RSAKey): self.pkey = pkey elif isinstance(pkey, str) and os.path.isfile(os.path.expanduser(pkey)): pkey = os.path.expanduser(pkey) self.pkey = paramiko.RSAKey.from_private_key_file(pkey) else: raise DaskEc2Exception("pkey argument should be filepath or paramiko.rsakey.RSAKey") else: self.pkey = None self.port = port self.timeout = timeout self.client = paramiko.SSHClient() self.client.set_missing_host_key_policy(paramiko.MissingHostKeyPolicy()) self._sftp = None if connect: self.connect()
def init_ssh_client(self, hostname, user): self.ssh_client = paramiko.SSHClient() self.ssh_client.load_system_host_keys() self.ssh_client.set_missing_host_key_policy(paramiko.MissingHostKeyPolicy()) self.ssh_client.connect(hostname=hostname, username=user) return True
def ssh(ctx, domain_name): """Get the public key for a SSH server. Example: $ lokey fetch ssh chat.shazow.net """ class FetchKeyPolicy(paramiko.MissingHostKeyPolicy): def __init__(self): self.key = None def missing_host_key(self, client, hostname, key): self.key = key fetch_key_policy = FetchKeyPolicy() client = paramiko.SSHClient() client.set_missing_host_key_policy(fetch_key_policy) try: client.connect(domain_name, username='lokey', timeout=5) key = fetch_key_policy.key.public_numbers key = ErisPublic(e=key.e, n=key.n) print key.to('ssh') except Exception as e: msg = ('Got "{message}" when attempting ' 'to connect to {domain_name}').format( domain_name=domain_name, message=str(e)) raise click.ClickException(msg)
def attempt(Password): IP = "127.0.0.1" USER = "rejah" PORT=22 try: ssh = paramiko.SSHClient() ssh.load_system_host_keys() ssh.set_missing_host_key_policy(paramiko.MissingHostKeyPolicy()) try: ssh.connect(IP , port=PORT, username=USER, password=Password) print "Connected successfully. Password = "+Password except paramiko.AuthenticationException, error: print "Incorrect password: "+Password pass except socket.error, error: print error pass except paramiko.SSHException, error: print error print "Most probably this is caused by a missing host key" pass except Exception, error: print "Unknown error: "+error pass ssh.close() except Exception,error : print error