D
D
digam2015-03-31 15:12:42
linux
digam, 2015-03-31 15:12:42

How to read real ip from ZyXEL keenetic lite with a script?

Task: it is necessary to periodically connect to a home server with a gray IP (transtelecom provider), for example, for video surveillance.
Solution: let the cron script read the ip from the router and send it to email if it has changed, and when I need to log in to the server, I read the mail and log in using the real IP to the home server.
Everything worked out on bash:
#!/usr/bin/expect
spawn telnet 192.168.1.1
expect assword {send password\r}
expect "KEENETIC LITE>" {send "wan status\r"}
expect "KEENETIC LITE>" {send exit \r}
expect eof
now you can run this file like this
./script.sh > info_ip_info.txt
and parse the text file, pull out the IP, run cron sending to email. (you can parse and send in bash, but I'm not good at this).
Everything works, but for more convenient further operation, there is a great desire to do all this in Python:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import pexpect
serv = '192.168.1.1'
passw = 'password'
p = pexpect.spawn('telnet %s' % serv)
p.expect('Password :')
p.sendline(passw)
p.expect('KEENETIC LITE>')
p.sendline('wan status\ r')
what to insert here?
p.sendline('exit\r')
and the script then runs without errors. I just can't figure out how to get the information.
Question:
How to drive into a variable what it will output (where?) After wan status and convert it into a set of strings so that you can work with them calmly?
PS It is not necessary to advise you to buy a white IP, because I feel that we are solving the issue :)

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Saboteur, 2015-03-31
@saboteur_kiev

You can read about DDNS, maybe it will be more convenient.
For python, do expect EOF - paste this:
exp.expect(pexpect.EOF)
after that, use the received text:
print exp.before

V
Vasily Angapov, 2015-04-02
@celebrate

what is there to put in here?
output = p.before
lines = output.split("\n")
for line in lines:
here you process each line of output

M
Maxim, 2015-04-09
Syomochkin @mak_sim

At one time, I solved this problem without tormenting the router:

#!/usr/local/bin/python3
# -*- coding: utf-8 -*-

import urllib.request
from lxml import etree

url = 'http://myip.ru/'
query = '//*/table[@class="network-info"]'

parser = etree.HTMLParser()
req = urllib.request.urlopen(url)
tree = etree.parse(req, parser)

tbl = tree.xpath(query)
print(tbl[0][1][0].text)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question