首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Fabric3连接到没有密码的服务器

Fabric3连接到没有密码的服务器
EN

Stack Overflow用户
提问于 2019-10-06 20:05:33
回答 1查看 352关注 0票数 0

我有一个写字板,我设法用paramiko做了ssh,但不是用Fabric3 1.14.post1,这里是一个paramiko (2.5.0)代码,带有调试输出:

代码语言:javascript
复制
#!/usr/bin/env python

from logging import DEBUG, getLogger, basicConfig
from paramiko import SSHClient, Transport

basicConfig()
getLogger("paramiko").setLevel(DEBUG)

class SSH(SSHClient):
    def __init__(self, host, user, password=None, port=22):
        self._host = host
        self._user = user
        self._password = password
        self._port = port
        super(SSH, self).__init__()

        self._transport = Transport((self._host, self._port))
        self._transport.connect(username=self._user, password=self._password)
        if not self._password:
            self._transport.auth_none(username=self._user)

    def __enter__(self):
        return self

    def __exit__(self, *args):
        self.close()


# Working with both
with SSH('192.168.1.32', 'pi', '-+') as ssh:
    i, o, e = ssh.exec_command('uname -n')
    print(o.read())

print("#"*80)

# Not wokring with fabric
with SSH('192.168.1.119', 'root') as ssh:
    i, o, e = ssh.exec_command('uname -n')
    print(o.read())

输出:

代码语言:javascript
复制
DEBUG:paramiko.transport:starting thread (client mode): 0x7c4a0f60
DEBUG:paramiko.transport:Local version/idstring: SSH-2.0-paramiko_2.5.0
DEBUG:paramiko.transport:Remote version/idstring: SSH-2.0-OpenSSH_7.9p1 Raspbian-10
INFO:paramiko.transport:Connected (version 2.0, client OpenSSH_7.9p1)
DEBUG:paramiko.transport:kex algos:['curve25519-sha256', 'curve25519-sha256@libssh.org', 'ecdh-sha2-nistp256', 'ecdh-sha2-nistp384', 'ecdh-sha2-nistp521', 'diffie-hellman-group-exchange-sha256', 'diffie-hellman-group16-sha512', 'diffie-hellman-group18-sha512', 'diffie-hellman-group14-sha256', 'diffie-hellman-group14-sha1'] server key:['rsa-sha2-512', 'rsa-sha2-256', 'ssh-rsa', 'ecdsa-sha2-nistp256', 'ssh-ed25519'] client encrypt:['chacha20-poly1305@openssh.com', 'aes128-ctr', 'aes192-ctr', 'aes256-ctr', 'aes128-gcm@openssh.com', 'aes256-gcm@openssh.com'] server encrypt:['chacha20-poly1305@openssh.com', 'aes128-ctr', 'aes192-ctr', 'aes256-ctr', 'aes128-gcm@openssh.com', 'aes256-gcm@openssh.com'] client mac:['umac-64-etm@openssh.com', 'umac-128-etm@openssh.com', 'hmac-sha2-256-etm@openssh.com', 'hmac-sha2-512-etm@openssh.com', 'hmac-sha1-etm@openssh.com', 'umac-64@openssh.com', 'umac-128@openssh.com', 'hmac-sha2-256', 'hmac-sha2-512', 'hmac-sha1'] server mac:['umac-64-etm@openssh.com', 'umac-128-etm@openssh.com', 'hmac-sha2-256-etm@openssh.com', 'hmac-sha2-512-etm@openssh.com', 'hmac-sha1-etm@openssh.com', 'umac-64@openssh.com', 'umac-128@openssh.com', 'hmac-sha2-256', 'hmac-sha2-512', 'hmac-sha1'] client compress:['none', 'zlib@openssh.com'] server compress:['none', 'zlib@openssh.com'] client lang:[''] server lang:[''] kex follows?False
DEBUG:paramiko.transport:Kex agreed: curve25519-sha256@libssh.org
DEBUG:paramiko.transport:HostKey agreed: ssh-ed25519
DEBUG:paramiko.transport:Cipher agreed: aes128-ctr
DEBUG:paramiko.transport:MAC agreed: hmac-sha2-256
DEBUG:paramiko.transport:Compression agreed: none
DEBUG:paramiko.transport:kex engine KexCurve25519 specified hash_algo <built-in function openssl_sha256>
DEBUG:paramiko.transport:Switch to new keys ...
DEBUG:paramiko.transport:Attempting password auth...
DEBUG:paramiko.transport:userauth is OK
INFO:paramiko.transport:Authentication (password) successful!
DEBUG:paramiko.transport:[chan 0] Max packet in: 32768 bytes
DEBUG:paramiko.transport:Received global request "hostkeys-00@openssh.com"
DEBUG:paramiko.transport:Rejecting "hostkeys-00@openssh.com" global request from server.
DEBUG:paramiko.transport:[chan 0] Max packet out: 32768 bytes
DEBUG:paramiko.transport:Secsh channel 0 opened.
DEBUG:paramiko.transport:[chan 0] Sesch channel 0 request ok
DEBUG:paramiko.transport:[chan 0] EOF received (0)
b'raspberrypi\n'
################################################################################
DEBUG:paramiko.transport:Ignoring message for dead channel 0
DEBUG:paramiko.transport:starting thread (client mode): 0x7c4a3cc0
DEBUG:paramiko.transport:Local version/idstring: SSH-2.0-paramiko_2.5.0
DEBUG:paramiko.transport:Remote version/idstring: SSH-2.0-dropbear_2017.75
INFO:paramiko.transport:Connected (version 2.0, client dropbear_2017.75)
DEBUG:paramiko.transport:kex algos:['curve25519-sha256@libssh.org', 'ecdh-sha2-nistp521', 'ecdh-sha2-nistp384', 'ecdh-sha2-nistp256', 'diffie-hellman-group14-sha1', 'diffie-hellman-group1-sha1', 'kexguess2@matt.ucc.asn.au'] server key:['ssh-rsa'] client encrypt:['aes128-ctr', 'aes256-ctr', 'aes128-cbc', 'aes256-cbc', 'twofish256-cbc', 'twofish-cbc', 'twofish128-cbc', '3des-ctr', '3des-cbc'] server encrypt:['aes128-ctr', 'aes256-ctr', 'aes128-cbc', 'aes256-cbc', 'twofish256-cbc', 'twofish-cbc', 'twofish128-cbc', '3des-ctr', '3des-cbc'] client mac:['hmac-sha1-96', 'hmac-sha1', 'hmac-sha2-256', 'hmac-sha2-512', 'hmac-md5'] server mac:['hmac-sha1-96', 'hmac-sha1', 'hmac-sha2-256', 'hmac-sha2-512', 'hmac-md5'] client compress:['zlib@openssh.com', 'none'] server compress:['zlib@openssh.com', 'none'] client lang:[''] server lang:[''] kex follows?False
DEBUG:paramiko.transport:Kex agreed: curve25519-sha256@libssh.org
DEBUG:paramiko.transport:HostKey agreed: ssh-rsa
DEBUG:paramiko.transport:Cipher agreed: aes128-ctr
DEBUG:paramiko.transport:MAC agreed: hmac-sha2-256
DEBUG:paramiko.transport:Compression agreed: none
DEBUG:paramiko.transport:kex engine KexCurve25519 specified hash_algo <built-in function openssl_sha256>
DEBUG:paramiko.transport:Switch to new keys ...
DEBUG:paramiko.transport:userauth is OK
INFO:paramiko.transport:Authentication (none) successful!
DEBUG:paramiko.transport:[chan 0] Max packet in: 32768 bytes
DEBUG:paramiko.transport:[chan 0] Max packet out: 32759 bytes
DEBUG:paramiko.transport:Secsh channel 0 opened.
DEBUG:paramiko.transport:[chan 0] Sesch channel 0 request ok
DEBUG:paramiko.transport:[chan 0] EOF received (0)
b'wandboard\n'
DEBUG:paramiko.transport:EOF in transport thread

我想对fabric (Fabric3 1.14.post1)做同样的事情,但它不起作用,下面是调试输出的代码:

代码语言:javascript
复制
#!/usr/bin/env python

from logging import DEBUG, getLogger, basicConfig
from fabric.api import settings, run

basicConfig()
getLogger("paramiko").setLevel(DEBUG)


# Working with both
with settings(host_string='192.168.1.32', user='pi', password='-+', abort_on_prompts=True):
    run('uname -n')

print("#"*80)

# Not wokring with fabric
with settings(host_string='192.168.1.119', user='root', abort_on_prompts=True):
    run('uname -n')

输出:

代码语言:javascript
复制
[192.168.1.32] run: uname -n
DEBUG:paramiko.transport:starting thread (client mode): 0xe3ec2da0
DEBUG:paramiko.transport:Local version/idstring: SSH-2.0-paramiko_2.5.0
DEBUG:paramiko.transport:Remote version/idstring: SSH-2.0-OpenSSH_7.9p1 Raspbian-10
INFO:paramiko.transport:Connected (version 2.0, client OpenSSH_7.9p1)
DEBUG:paramiko.transport:kex algos:['curve25519-sha256', 'curve25519-sha256@libssh.org', 'ecdh-sha2-nistp256', 'ecdh-sha2-nistp384', 'ecdh-sha2-nistp521', 'diffie-hellman-group-exchange-sha256', 'diffie-hellman-group16-sha512', 'diffie-hellman-group18-sha512', 'diffie-hellman-group14-sha256', 'diffie-hellman-group14-sha1'] server key:['rsa-sha2-512', 'rsa-sha2-256', 'ssh-rsa', 'ecdsa-sha2-nistp256', 'ssh-ed25519'] client encrypt:['chacha20-poly1305@openssh.com', 'aes128-ctr', 'aes192-ctr', 'aes256-ctr', 'aes128-gcm@openssh.com', 'aes256-gcm@openssh.com'] server encrypt:['chacha20-poly1305@openssh.com', 'aes128-ctr', 'aes192-ctr', 'aes256-ctr', 'aes128-gcm@openssh.com', 'aes256-gcm@openssh.com'] client mac:['umac-64-etm@openssh.com', 'umac-128-etm@openssh.com', 'hmac-sha2-256-etm@openssh.com', 'hmac-sha2-512-etm@openssh.com', 'hmac-sha1-etm@openssh.com', 'umac-64@openssh.com', 'umac-128@openssh.com', 'hmac-sha2-256', 'hmac-sha2-512', 'hmac-sha1'] server mac:['umac-64-etm@openssh.com', 'umac-128-etm@openssh.com', 'hmac-sha2-256-etm@openssh.com', 'hmac-sha2-512-etm@openssh.com', 'hmac-sha1-etm@openssh.com', 'umac-64@openssh.com', 'umac-128@openssh.com', 'hmac-sha2-256', 'hmac-sha2-512', 'hmac-sha1'] client compress:['none', 'zlib@openssh.com'] server compress:['none', 'zlib@openssh.com'] client lang:[''] server lang:[''] kex follows?False
DEBUG:paramiko.transport:Kex agreed: curve25519-sha256@libssh.org
DEBUG:paramiko.transport:HostKey agreed: ecdsa-sha2-nistp256
DEBUG:paramiko.transport:Cipher agreed: aes128-ctr
DEBUG:paramiko.transport:MAC agreed: hmac-sha2-256
DEBUG:paramiko.transport:Compression agreed: none
DEBUG:paramiko.transport:kex engine KexCurve25519 specified hash_algo <built-in function openssl_sha256>
DEBUG:paramiko.transport:Switch to new keys ...
DEBUG:paramiko.transport:Trying SSH agent key b'24a1a600a5a6c92ebfd5d46f86bcd30c'
DEBUG:paramiko.transport:userauth is OK
INFO:paramiko.transport:Authentication (publickey) failed.
DEBUG:paramiko.transport:Trying discovered key b'24a1a600a5a6c92ebfd5d46f86bcd30c' in /home/ramih/.ssh/id_rsa
DEBUG:paramiko.transport:userauth is OK
INFO:paramiko.transport:Authentication (publickey) failed.
DEBUG:paramiko.transport:userauth is OK
INFO:paramiko.transport:Authentication (password) successful!
DEBUG:paramiko.transport:[chan 0] Max packet in: 32768 bytes
DEBUG:paramiko.transport:Received global request "hostkeys-00@openssh.com"
DEBUG:paramiko.transport:Rejecting "hostkeys-00@openssh.com" global request from server.
DEBUG:paramiko.transport:[chan 0] Max packet out: 32768 bytes
DEBUG:paramiko.transport:Secsh channel 0 opened.
DEBUG:paramiko.transport:[chan 0] Sesch channel 0 request ok
DEBUG:paramiko.transport:[chan 0] Sesch channel 0 request ok
DEBUG:paramiko.transport:[chan 0] EOF received (0)
DEBUG:paramiko.transport:[chan 0] EOF sent (0)
[192.168.1.32] out: raspberrypi
[192.168.1.32] out: 

################################################################################
[192.168.1.119] run: uname -n
DEBUG:paramiko.transport:starting thread (client mode): 0xe3e94278
DEBUG:paramiko.transport:Local version/idstring: SSH-2.0-paramiko_2.5.0
DEBUG:paramiko.transport:Remote version/idstring: SSH-2.0-dropbear_2017.75
INFO:paramiko.transport:Connected (version 2.0, client dropbear_2017.75)
DEBUG:paramiko.transport:kex algos:['curve25519-sha256@libssh.org', 'ecdh-sha2-nistp521', 'ecdh-sha2-nistp384', 'ecdh-sha2-nistp256', 'diffie-hellman-group14-sha1', 'diffie-hellman-group1-sha1', 'kexguess2@matt.ucc.asn.au'] server key:['ssh-rsa'] client encrypt:['aes128-ctr', 'aes256-ctr', 'aes128-cbc', 'aes256-cbc', 'twofish256-cbc', 'twofish-cbc', 'twofish128-cbc', '3des-ctr', '3des-cbc'] server encrypt:['aes128-ctr', 'aes256-ctr', 'aes128-cbc', 'aes256-cbc', 'twofish256-cbc', 'twofish-cbc', 'twofish128-cbc', '3des-ctr', '3des-cbc'] client mac:['hmac-sha1-96', 'hmac-sha1', 'hmac-sha2-256', 'hmac-sha2-512', 'hmac-md5'] server mac:['hmac-sha1-96', 'hmac-sha1', 'hmac-sha2-256', 'hmac-sha2-512', 'hmac-md5'] client compress:['zlib@openssh.com', 'none'] server compress:['zlib@openssh.com', 'none'] client lang:[''] server lang:[''] kex follows?False
DEBUG:paramiko.transport:Kex agreed: curve25519-sha256@libssh.org
DEBUG:paramiko.transport:HostKey agreed: ssh-rsa
DEBUG:paramiko.transport:Cipher agreed: aes128-ctr
DEBUG:paramiko.transport:MAC agreed: hmac-sha2-256
DEBUG:paramiko.transport:Compression agreed: none
DEBUG:paramiko.transport:kex engine KexCurve25519 specified hash_algo <built-in function openssl_sha256>
DEBUG:paramiko.transport:Switch to new keys ...
DEBUG:paramiko.transport:Adding ssh-rsa host key for 192.168.1.119: b'6bac39c13ffdaa2687f3d283a7c4de3e'
DEBUG:paramiko.transport:Trying discovered key b'24a1a600a5a6c92ebfd5d46f86bcd30c' in /home/ramih/.ssh/id_rsa
DEBUG:paramiko.transport:userauth is OK
INFO:paramiko.transport:Authentication (publickey) failed.

Fatal error: Needed to prompt for a connection or sudo password (host: 192.168.1.119), but abort-on-prompts was set to True

Aborting.

我尝试了很多fabric环境参数,但似乎都不起作用,例如no_agent,forward_agent关键区别在于身份验证,我如何在fabric中进行身份验证(无)?

EN

回答 1

Stack Overflow用户

发布于 2019-12-22 00:36:30

与@peshmerge不同,我喜欢Fabric3并全心全意地推荐它。也就是说,在运行时更改设置和环境时,诀窍是使用execute包装您的调用,对于无密码的调用,请确保使用key_filename参数将fabric指向您想要使用的私有ssh密钥文件:

代码语言:javascript
复制
#!/usr/bin/env python

from logging import DEBUG, getLogger, basicConfig
from fabric.api import settings, run, execute

basicConfig()
getLogger("paramiko").setLevel(DEBUG)

def find_uname():
    run('uname -a')


# Working with both
with settings(host_string='192.168.1.32', user='pi', password='-+', abort_on_prompts=True):
    execute(find_uname)

print("#"*80)

# Not wokring with fabric
with settings(host_string='192.168.1.119', user='root', abort_on_prompts=True, key_filename='/path/to/my_ssh_key'):
    execute(find_uname)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58257067

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档