Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用SSH命令在python中生成字典_Python_Sockets_Dictionary_Ssh_Socks - Fatal编程技术网

使用SSH命令在python中生成字典

使用SSH命令在python中生成字典,python,sockets,dictionary,ssh,socks,Python,Sockets,Dictionary,Ssh,Socks,我有一个python脚本,它执行以下故事情节: 首先检查所有不需要VPN连接的ip,通过SSH连接,然后运行CAT命令 然后检查需要VPN的每个人,连接到VPN,并SSH运行CAT命令 这部分是功能性的,我的问题是将ssh的输出分配给python字典 #!/usr/bin/python # -*- coding: utf-8 -*- from datetime import datetime, date, time, timedelta import socket import socks

我有一个python脚本,它执行以下故事情节:

  • 首先检查所有不需要
    VPN
    连接的
    ip
    ,通过
    SSH
    连接,然后运行
    CAT
    命令

  • 然后检查需要
    VPN
    的每个人,连接到
    VPN
    ,并
    SSH
    运行
    CAT
    命令

这部分是功能性的,我的问题是将ssh的输出分配给python字典

#!/usr/bin/python
# -*- coding: utf-8 -*-

from datetime import datetime, date, time, timedelta
import socket
import socks
import paramiko
import sys
import re

# create script head
print ('-----------------------------------------------------------------------------------------')
print ('Initializing UP/DOWN script in: '+str(date.today()))
print ('-----------------------------------------------------------------------------------------')

Ips = {'123.45.44.33': {'customer' : 'webservice'}}
IPsocks = {'176.25.0.61': {'customer' : 'hosting'}}

output = []
outputSocks = []
outfinally = []
outfinallySocks = []
lds_data = {}

for ip in Ips:

    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(hostname=ip, username='admin', password="admin")
    output.append((ssh.exec_command('cat /tmp/hosts.txt')[1]).read().decode())
    ssh.close()

    outfinally.append([re.split(r'\s*#\s*',line) for line in output[0].splitlines()])

    for a, b in zip(Ips, outfinally):
        lds_data.update({b[:1] : {'hostname' : b[1:5], 'customer' : Ips[a]['customer']}})

for ip in IPsocks:

    sock=socks.socksocket()
    sock.set_proxy(
        proxy_type=socks.SOCKS5,
        addr='10.0.1.10',
        port=1080,
        username="vpn",
        password="vpn102030"
    )
    sock.connect((ip, 22))

    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect('ignored without host key verification', username='admin', password='admin', sock=sock)
    outputSocks.append((ssh.exec_command('cat /tmp/hosts.txt')[1]).read().decode())
    ssh.close()

    outfinallySocks.append([re.split(r'\s*#\s*',line) for line in outputSocks[0].splitlines()])

    for a, b in zip(IPsocks, outfinallySocks):
        lds_data.update({b[:1] : {'hostname' : b[1:5], 'customer' : IPsocks[a]['customer']}})

    print(lds_data)

print ('-----------------------------------------------------------------------------------------')
print ('Script successfully completed')
print ('-----------------------------------------------------------------------------------------')
输出

  File "SCRIPT.py", line 36, in <module>
    lds_data.update({tuple(b[:1]) : {'hostname' : b[1:5], 'customer' : Ips[a]['customer']}})
TypeError: unhashable type: 'list'

这很难重现,但假设
b
是一个列表列表,我们可以替换此行的实例:

lds_data.update({b[:1] : {'hostname' : b[1:5], 'customer' : IPsocks[a]['customer']}})
为此:

lds_data.update({tuple(b[0]) : {'hostname' : b[1:5], 'customer' : Ips[a]['customer']}})
这将用扁平元组替换列表列表-这是因为字典中的键不能是列表,因为它们是可变的。代码的潜在问题是您试图将字典键设置为列表

lds_data.update({tuple(b[0]) : {'hostname' : b[1:5], 'customer' : Ips[a]['customer']}})