Multi Device Configuration with telnetlib

untuk melakukan konfigurasi ke beberapa device sekaligus, kita dapat memanfaatkan fungsi looping seperti for untuk melakukan ini :

# Import module
import telnetlib
import time
import ast

# open json and read into raw text
raw = open('inventory.json').read()

# loads the raw into loads() function to create dict string type
data = ast.literal_eval(raw)

# define looping variable
looping = 0

# using looping
for host in data["host"]:

    # open connection
    connect = telnetlib.Telnet(host=data["host"][looping])

    # find prompt login?
    connect.read_until('login: ')

    # enter username
    connect.write(data["username"][looping]+'\n')

    # find prompt password?
    connect.read_until('Password: ')

    # enter password
    connect.write(data["password"][looping]+'\n')

    # find prompt $?
    connect.read_until('$ ')

    # Enter Configure Mode
    connect.write('configure\n')

    # find prompt #?
    connect.read_until('# ')

    # Enter Configure Mode
    connect.write('set system host-name '+data["hostname"][looping]+'\n')

    # Commit and save
    connect.write('commit\n')
    connect.write('save\n')

    # show interfaces
    connect.write('run show configuration\n')

    # read output
    time.sleep(1)
    output =  connect.read_very_eager()
    print (output)

    # close connection
    connect.close()

    # increase looping variable
    looping = looping + 1

lalu buat file inventorynya :

{
    "host":["192.168.122.250","192.168.122.146"],
    "username":["vyos","vyos"],
    "password":["vyos","vyos"],
    "hostname":["R1","R2"]
}

bisa dilihat bahwa  script python diatas akan membaca berapa jumlah host yang ada didalam dictionary lalu melakukan eksekusi untuk mengkonfigurasi hostname. berikut output yang dikeluarkan :

zufar@zufar:~/Documents/Technology/Programming/Python/telnetlib$ python lab7.py
set system host-name R1

  Configuration path: [system host-name R1] already exists

[edit]
vyos@R1# commit
No configuration changes to commit
[edit]
vyos@R1# save
Saving configuration to '/config/config.boot'...
Done
[edit]
vyos@R1# run show configuration
interfaces {
    ethernet eth0 {
        address dhcp
        hw-id 0c:a4:23:bf:21:00
    }
    loopback lo {
    }
}
service {
    telnet {
    }
}
system {
    config-management {
        commit-revisions 20
    }
    console {
        device ttyS0 {
            speed 9600
        }
    }
    host-name R1
    login {
:
set system host-name R2
[edit]
vyos@vyos# commit
[ system host-name R2 ]
Stopping enhanced syslogd: rsyslogd.
Starting enhanced syslogd: rsyslogd.

[edit]
vyos@vyos# save
Saving configuration to '/config/config.boot'...
Done
[edit]
vyos@vyos# run show configuration
interfaces {
    ethernet eth0 {
        address dhcp
        hw-id 0c:a4:23:a7:1f:00
    }
    loopback lo {
    }
}
service {
    telnet {
    }
}
system {
    config-management {
        commit-revisions 20
    }
    console {
        device ttyS0 {
            speed 9600
        }
    }
    host-name R2
    login {
:
zufar@zufar:~/Documents/Technology/Programming/Python/telnetlib$

 

Comments are closed.