Device Configuration with Telnetlib

berikut contoh program untuk mengkonfigurasi hostname di dalam vyos dengan telnetlib :

##### Configure The Router Hostname ######
import telnetlib
import time
import sys

host = str(sys.argv[1])
port = str(sys.argv[2])
user = str(sys.argv[3])
passwd = str(sys.argv[4])
hostname = str(sys.argv[5])

# open connection
connect = telnetlib.Telnet(host=host,port=port)

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

# enter username
connect.write(user+'\n')

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

# enter password
connect.write(passwd+'\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 '+hostname+'\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()

coba jalankan program tersebut :

zufar@zufar:~/Documents/Technology/Programming/Python/telnetlib$ python lab6.py 192.168.122.250 23 vyos vyos RRR1
set system host-name RRR1
[edit]
vyos@vyos# commit
[ system host-name RRR1 ]
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:bf:21:00
    }
    loopback lo {
    }
}
service {
    telnet {
    }
}
system {
    config-management {
        commit-revisions 20
    }
    console {
        device ttyS0 {
            speed 9600
        }
    }
    host-name RRR1
    login {
:
zufar@zufar:~/Documents/Technology/Programming/Python/telnetlib$

bisa dilihat host-name sudah terganti.

Comments are closed.