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

2024-03-29 11:26:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个插件,我可以用paramiko实现ssh,但不能用Fabric3 1.14.post1 以下是带调试输出的工作paramiko(2.5.0)代码:

#!/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())

输出:

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

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

#!/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')

输出:

[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 env参数,但似乎什么都不起作用,例如,no\u代理,forward\u代理 关键的区别在于身份验证,如何在fabric中进行身份验证(none)?你知道吗


Tags: debugcomclientparamikoserverhmacsha1transport
1条回答
网友
1楼 · 发布于 2024-03-29 11:26:26

与@peshmerge不同,我喜欢Fabric3并衷心推荐它。也就是说,在运行时更改设置和env时,技巧是使用execute包装调用,对于无密码的情况,请确保使用key_filename参数将fabric指向要使用的私有ssh密钥文件:

#!/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)

相关问题 更多 >