前言:我是Python的新手。我经常写PHP。我也知道这可能被认为是一个主观问题,它可能会被标记为这样,但我确实认为可以从中得出一个答案,即#34;足够正确"它肯定会帮助我。我也不知道还能在哪里问。
现在,这只是游戏代码,但我正在玩的想法是有一些静态工厂,根据选项构建一个Paramiko SSHClient类。我不确定我最终会如何设计这个,但我有一种强烈的感觉,即使现在我还没有充分利用python设计功能,而是强迫它像PHP一样工作。基本上,我是否做错了?
import paramiko
import time
import sys
class Client:
@staticmethod
def build(host, **options):
default_connect_opts = {'look_for_keys': True}
client = paramiko.SSHClient()
# automatically add untrusted hosts
if options.get('special').get('auto_add_policy'):
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
our_connect_opts = options.get('connect');
# merge defaults with provided with preference for provided opts
connect_opts = dict(default_connect_opts.items() + our_connect_opts.items())
#print connect_opts
#sys.exit()
client.connect(host, **connect_opts)
return client
options = {
'connect': {
'username': 'root',
'password': 'p4assw0rd',
'look_for_keys': False,
},
'special': {
'auto_add_policy': True,
}
}
client = Client.build('10.0.0.13', **options)
print client
编辑:为了清楚我最终想要完成的事情 - 我可能希望为通用的unix / linux主机构建paramiko sshclients,但也希望能够为奇怪的球构建专门的sshclient类,例如没有运行真实 shell的Cisco交换机。要做到这一点,我必须通过其他选项来禁用分页,有时候命令之间会有sleep
等,具体取决于远程" shell"的性质。
答案 0 :(得分:1)
您应该了解NoneType Error
当options.get('special').get('auto_add_policy')
不在special
时,options
None.get()
# Deal with `options` without key 'special'
if options.get('special', {}).get('auto_add_policy'):
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
default_connect_opts.update(options.get('connect', {}))
client.connect(host, **default_connect_opts)
,SSHClient
将会抛出异常。
default_connect_opts
您可以为自己的目的派生look_for_key = True
,默认情况下摆脱class Client(paramiko.SSHClient):
def __init__(self, host, **options):
super(Client, self).__init__()
# set policy
if options.get('special', {}).get('auto_add_policy'):
self._policy = paramiko.AutoAddPolicy()
# look_for_key = True by default
self.connect(host, **options.get('connect', {}))
client = Client(host, **options)
new_client = Client(new_host, **new_options)
<object ng-click="zoomService.activateZoomAndPan();"
tb-refresh-graphic="tocService.active_node.activeGraphic">
</object>
。
$data['additional'] = $someAdditionalData;