Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/310.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
如果路由器在python中处于活动状态且可见,如何仅ssh到3个路由器中的1个_Python_Json_Networking_Ssh - Fatal编程技术网

如果路由器在python中处于活动状态且可见,如何仅ssh到3个路由器中的1个

如果路由器在python中处于活动状态且可见,如何仅ssh到3个路由器中的1个,python,json,networking,ssh,Python,Json,Networking,Ssh,我有3个路由器,一次我只需要ssh到3个路由器中的任何一个,然后运行一些命令。现在,我的代码将ssh连接到所有3个路由器。 我只需要ssh到1个活动路由器 情景 首先检查第一个路由器是否处于活动状态,如果处于活动状态..继续ssh到第一个路由器并运行命令..结束..但是。。。如果第一个路由器关闭且网络不可见,请尝试检查第二个路由器,如果该路由器处于活动状态..继续ssh到第二个路由器并运行命令..结束..如果关闭..检查第三个路由器..如果全部关闭,则发出通知 我正在使用netmiko通过ssh

我有3个路由器,一次我只需要ssh到3个路由器中的任何一个,然后运行一些命令。现在,我的代码将ssh连接到所有3个路由器。 我只需要ssh到1个活动路由器

情景 首先检查第一个路由器是否处于活动状态,如果处于活动状态..继续ssh到第一个路由器并运行命令..结束..但是。。。如果第一个路由器关闭且网络不可见,请尝试检查第二个路由器,如果该路由器处于活动状态..继续ssh到第二个路由器并运行命令..结束..如果关闭..检查第三个路由器..如果全部关闭,则发出通知

我正在使用netmiko通过ssh连接到设备

这是ssh到所有设备的代码:-

with open('routers.json') as dev_file:
  devices = json.load(dev_file)


for device in routers['router']:
  try:
    print('Connecting to device:', device['ip'])
    connection = netmiko.ConnectHandler(**device)
    if  device['device_type'] == "cisco_ios" :
        output = net_connect.send_command("show arp")
        print(output)
这是routers.json文件:-

{
  "router": [
    {
        "device_type": "cisco_ios",
        "ip": "192.168.100.10",
        "password": "cisco123",
        "username": "user1"
    },
    {
        "device_type": "cisco_ios",
        "ip": "192.168.100.20",
        "password": "cisco123",
        "username": "user1"
    },
    {
        "device_type": "cisco_ios",
        "ip": "192.168.100.30",
        "password": "cisco123",
        "username": "user1"
    }
  ]
}
只需要ssh到3个路由器中的1个,如果是活动的和可靠的。请进一步协助。多谢各位


**如果发现路由器已启动,则添加中断语句。如果没有,继续检查其他路由器。

要添加一个功能来检查IP是否可ping,我们可以使用
os.system
在IP上运行
ping
命令,如果我们可以ping IP,我们可以连接它

def check_ping(ip_address):

    response = -1

    #Check if we can ping the IP, if we can, response will be 0,
    try:
        response = os.system("ping -c 1 {}".format(ip_address))
    except Exception as exc:
        pass

    if response == 0:
        host_is_up = True
    else:
        host_is_up = False

    return host_is_up
使用该函数,我们可以将代码重写为

import os

#Open devices file
with open('routers.json') as dev_file:
  devices = json.load(dev_file)

#Function to check if ip address is pingable
def check_ping(ip_address):

    response = -1

    #Check if we can ping the IP, if we can, response will be 0,
    try:
        response = os.system("ping -c 1 {}".format(ip_address))
    except Exception as exc:
        pass

    if response == 0:
        host_is_up = True
    else:
        host_is_up = False

    return host_is_up

for device in routers['router']:
    try:
        #If device_type matches cisco_ios, do stuff
        if device['device_type'] == "cisco_ios":
            print('Connecting to device:', device['ip'])
            if check_ping(device['ip']):
                connection = netmiko.ConnectHandler(**device)
                output = net_connect.send_command("show arp")
                print(output)
    #Added the missing except
    except:
        pass
输出将如下所示

Connecting to device: 192.168.100.10
.....

你好,先生。。。首先感谢您的回复。仅供参考,设备类型不能更改,因为这是思科设备netmiko模块的一部分。设备类型必须设置为“思科ios”。唯一不同的是每个路由器的ip地址。实际上这里的3个路由器是为了冗余的目的。。。可能所有3个路由器都处于活动状态并向上运行…但同时我只需要访问其中的1个…而不是全部…例如…如果路由器192.168.100.10处于活动状态并向上运行…那么ssh到该路由器。。。而且不需要ssh连接到路由器的其余部分……如何确定需要访问哪一个?是随机的吗?您事先知道可以匹配的ip吗?到第一个激活/启动的路由器…如果第一个路由器(192.168.100.10)启动(can ping),则ssh到第一个路由器。。。。IP已经在disct文件中修复。这是connection object的属性,如果我无法ping IP,netmiko connecthandler函数是否会引发异常?或者在连接对象中是否有一个属性,您可以检查以确保ip是可ping的?我想类似于…首先ping ip(dict文件中的ip地址)…如果第一个ip可以ping…则继续ssh进程。只是这里的ping部分…不知道如何处理它