快捷搜索:

paramiko 连路由器看拨号ip 附上源码

 

这里我有2条外线。做负载均衡 ip 获取比较麻烦 虽然有很多 免费获取ip的 api 但无法同时获取俩条线路的ip

这里pppoe的拨号分别是 dialer 0 dialer 2

===

思路通过ssh连接华为路由器 看俩个拨号☎️ip 地址

华为拨号的配置文件

```

ip load-balance hash src-ip

acl number 3002

rule 5 permit ip source 192.168.1.0 0.0.0.255

rule 10 permit ip source 192.168.4.0 0.0.0.255

rule 15 permit ip source 192.168.7.0 0.0.0.255

rule 20 permit ip source 192.168.2.0 0.0.0.255

rule 25 permit ip source 192.168.3.0 0.0.0.255

rule 30 permit ip source 192.168.5.0 0.0.0.255

rule 35 permit ip source 192.168.6.0 0.0.0.255

rule 40 permit ip source 192.168.12.0 0.0.0.255

#

interface Dialer0

link-protocol ppp

ppp chap user xxxxxxxx

ppp chap password simple 321312321

mtu 1492

tcp adjust-mss 1200

ip address ppp-negotiate

dialer user server

dialer user arweb

dialer bundle 1

dialer-group 1

nat outbound 3002

#

interface Dialer1

link-protocol ppp

#

interface Dialer2

link-protocol ppp

ppp chap user xxxxxxxxx

ppp chap password cipher %^%#+J{SB*;q|9A5bF!oROE.@,;X~M+a>/~g#a1e@psK%^%#

ppp ipcp dns admit-any

ppp ipcp dns request

ipv6 enable

tcp adjust-mss 1200

ip address ppp-negotiate

dialer user arweb

dialer bundle 2

dialer-group 2

ipv6 address auto link-local

ipv6 address auto global default

undo ipv6 nd ra halt

nat outbound 3002

#

#

ip route-static 0.0.0.0 0.0.0.0 Dialer0

ip route-static 0.0.0.0 0.0.0.0 Dialer2

```

===

```python

#-*- coding: utf-8 -*-

#!/usr/bin/python

import paramiko

import time

import re

rip = '192.168.12.1'

def ssh2(ip,username,passwd,cmd):

try:

ssh = paramiko.SSHClient()

ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

ssh.connect(ip,33301,username,passwd,timeout=5)

ssh_shell = ssh.invoke_shell() #使用invoke是为了可以执行多条命令

r = ssh_shell.recv(4096)

#print(r.decode())

ssh_shell.sendall(cmd+'\r\n')

time.sleep(5)

r = ssh_shell.recv(4096)

#print(r.decode())

result = re.findall('\d+\.\d+\.\d+\.\d+/32',r.decode())[0]

if result:

print(result.split('/')[0])

else:

print('None')

ssh.close()

except Exception as e:

#print(str(e))

print('None')

if __name__=='__main__':

import sys

cmd = 'dis int dia '

username = "admin" #用户名

passwd = "admin" #密码

if len(sys.argv) > 1:

cmd = cmd + sys.argv[1]

else:

cmd = cmd + ' 0'

ssh2(rip,username,passwd,cmd)

```

[注:本文部分图片来自互联网!未经授权,不得转载!每天跟着我们读更多的书]


互推传媒文章转载自第三方或本站原创生产,如需转载,请联系版权方授权,如有内容如侵犯了你的权益,请联系我们进行删除!

如若转载,请注明出处:http://www.hfwlcm.com/info/297431.html