Skip to content

Instantly share code, notes, and snippets.

@ktnr74
Last active July 10, 2016 14:10
Show Gist options
  • Save ktnr74/12476817c27755d35e68e20ad75dabef to your computer and use it in GitHub Desktop.
Save ktnr74/12476817c27755d35e68e20ad75dabef to your computer and use it in GitHub Desktop.
Nortel LG 1535 VOIP video phone provisioning script
#!/usr/bin/python
import time
import sys
from telnetlib import Telnet, IAC, DO, DONT, WILL, WONT, SB, SE, ECHO, SGA
from argparse import ArgumentParser
from crypt import crypt
class Nortel1535(Telnet):
def parse_args(self):
parser = ArgumentParser(usage='%(prog)s [options] phone_address')
parser.add_argument("phone_address")
parser.add_argument("-u", "--user", dest="user", default="root")
parser.add_argument("-p", "--pass", dest="password", default="lvp2890")
parser.add_argument("-f", "--ftp", dest="VDCI_FTPSERVER")
parser.add_argument("-d", "--domain", dest="VDCI_DOMAIN")
parser.add_argument("-x", "--proxy", dest="VDCI_PROXY_ADDR")
parser.add_argument("-P", "--authpwd", dest="VDCI_AUTH_PWD")
parser.add_argument("-N", "--authname", dest="VDCI_AUTH_NAME")
parser.add_argument("-t", "--telnum", dest="VDCI_TEL_NUM")
parser.add_argument("-D", "--dispname", dest="VDCI_DISPLAY_NAME")
self.args = parser.parse_args()
def login(self):
self.open(self.args.phone_address)
self.read_until("login:", 1)
self.write("{}\r\n".format(self.args.user))
self.read_until("Password:", 1)
self.write("{}\r\n".format(self.args.password))
self.read_until("# ")
self.marker = "END_OF_OUTPUT_MARKER"
def run(self, cmd):
self.sock.sendall(IAC + DONT + ECHO)
self.read_eager()
self.write("{}; echo {}\r\n".format(cmd, self.marker))
self.read_until(" {}\r\n".format(self.marker), 1)
return self.read_until("{}\r\n".format(self.marker), 2)[:-len(self.marker)-2]
def set(self, name, value, path):
self.run(r'sed -i "s/^\({}\)\s*=.*/\1={}/" {}'.format(name, value, path))
def prepend_line(self, line, prefix, path):
self.run(r'sed -i "s/^\({}\)$/{}\1/" {}'.format(line, prefix, path))
def add_another_root_user(self, user, passw):
self.run(r"grep -q {0} {2} || echo '{0}:{1}:0:0:{0}:/:/bin/sh' >>{2}".format(user, crypt(passw, "$1$$").replace(r'/', r'\/'), "/usr/etc/passwd"))
def change_pass(self, user, passw):
self.run(r"sed -i 's/^\({}:\)[^:]*\(.*\)$/\1{}\2/' {}".format(user, crypt(passw, "$1$$").replace(r'/', r'\/'), "/usr/etc/passwd"))
def save_settings(self, path, **kw):
for k, v in kw.iteritems():
self.set(k, v, path)
def enable_telnet_on_boot(self):
self.prepend_line("killall inetd", "# ", "/usr/etc/rc.etc")
lvp = Nortel1535()
lvp.parse_args()
lvp.login()
lvp.change_pass("root", "secret")
lvp.enable_telnet_on_boot()
lvp.save_settings("/usr/local/lgvp/vdci.cfg", **{k[5:]:v for k, v in vars(lvp.args).iteritems() if v and k.startswith("VDCI_")})
lvp.save_settings("/usr/local/lgvp/setting_save.py",
ntp_enable="1",
ntp_summertime="1",
ntp_timezone="'-5'",
ringer_volume="3",
handset_volume="3",
speaker_volume="3"
)
lvp.set("ntp-server", "pool.ntp.org", "/usr/local/lgvp/ntp.cfg")
lvp.run("reboot")
sys.exit(0)
#!/usr/bin/python
import pexpect
import time
import sys
PROMPT = "# "
NUMBER = "101"
PASS = "12345"
SERVER = "192.168.1.200"
VDCICFG = {
"FTPSERVER": SERVER,
"PROXY_ADDR": SERVER,
"DOMAIN": SERVER,
"AUTH_PWD": PASS,
"AUTH_NAME": NUMBER,
"TEL_NUM": NUMBER,
"DISPLAY_NAME": NUMBER,
}
SETTTINGSAVEPY = {
"ntp_enable": "1",
"ntp_summertime": "1",
"ntp_timezone": "'-5'",
"ringer_volume": "3",
"handset_volume": "3",
"speaker_volume": "3",
}
telconn = pexpect.spawn("telnet {}".format(sys.argv[1]))
telconn.logfile = sys.stdout
telconn.expect("login:")
telconn.sendline("root")
telconn.expect("Password:")
telconn.sendline("lvp2890")
telconn.expect(PROMPT)
def VariableSet(name, value, path):
telconn.sendline(r'sed -i "s/^\({}\)\s*=.*/\1={}/" {}'.format(name, value, path))
telconn.expect(PROMPT)
for k, v in VDCICFG.iteritems():
VariableSet(k, v, "/usr/local/lgvp/vdci.cfg")
for k, v in SETTTINGSAVEPY.iteritems():
VariableSet(k, v, "/usr/local/lgvp/setting_save.py")
telconn.sendline(r'sed -i "s/^\(killall inetd\)$/# \1/" /usr/etc/rc.etc')
telconn.expect(PROMPT)
telconn.sendline("reboot")
sys.exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment