Wednesday, January 11, 2017

auto login telnet script

*** script, save it as telnetauth.py ***

#!/usr/bin/python
import os
import sys
import re
import pexpect
import yaml

def login(ip,user,passwd):
    # os.system("ssh -i " + key + " " + user+"@"+ip)
    child = pexpect.spawn("telnet " + ip)
    child.expect("Login:")
    child.sendline(user)
    child.expect("Password:")
    child.sendline(passwd)
    child.interact()

configf = "router.yaml"
c = 20
with open(configf, "r") as f:
    config = yaml.load(f)

rname = sys.argv[1]

for i in config:
   
    if re.search(rname, i):
        print i
        ip = config[i]['ip']
        user = config[i]['user']
        passwd = config[i]['password']

        login(ip, user, passwd)

        break




1- it need a config file "router.yaml" in the same directory, sample is below
router1:
    ip: 1.1.1.1
    user: user1
    password: pass1
router2:
    ip: 1.1.1.2
    user: user2
    password: pass2





2- usage
$ python telnetauth.py router1

No comments:

Post a Comment