Difference between revisions of "16471 Grundlæggende programmering"

From Teknologisk videncenter
Jump to: navigation, search
m (Telnet)
m (Telnet)
Line 15: Line 15:
 
==Telnet==
 
==Telnet==
 
To use this module, you need to be able to telnet to one or more Cisco devices.
 
To use this module, you need to be able to telnet to one or more Cisco devices.
*[https://networksandrants.com/2018/10/24/using-python-and-telnet/ Using Python and Telnet]
 
  
 
Example of basic setup
 
Example of basic setup
Line 29: Line 28:
 
line vty 0 15
 
line vty 0 15
 
  password cisco
 
  password cisco
 +
</source>
 +
Python 3 telnet example
 +
<source lang=python>
 +
import getpass
 +
import sys
 +
import telnetlib
 +
host = "192.168.239.129"
 +
user = input("Username: ")
 +
password = input("Password: ")
 +
 +
tn = telnetlib.Telnet(host)
 +
tn.read_until(b"Username: ")
 +
tn.write(user.encode('ascii') + b"\n")
 +
if password:
 +
  tn.read_until(b"Password: ")
 +
  tn.write(password.encode('ascii') + b"\n")
 +
 +
tn.write(b"enable\n")
 +
tn.write(b"cisco\n")
 +
tn.write(b"conf t\n")
 +
tn.write(b"int loop 0\n")
 +
tn.write(b"ip address 1.1.1.1 255.255.255.255\n")
 +
tn.write(b"int loop 1\n")
 +
tn.write(b"ip address 1.1.1.2 255.255.255.255\n")
 +
tn.write(b"end\n")
 +
tn.write(b"exit\n")
 +
print(tn.read_all())
 
</source>
 
</source>
  

Revision as of 10:36, 6 April 2020

  1. Link til Google Hangout
  2. Vores facebookgruppe til at dele info via denne link - gruppen hedder Grundlæggende programmering - Python
  3. Kurset følger i første del video læringsmaterialet via denne link til Oreilly
  4. Materiale til øvelser i videomateriale via denne link til Github

Python and networking

Materials

Telnet

To use this module, you need to be able to telnet to one or more Cisco devices.

Example of basic setup

enable password cisco
!
username cisco password 0 cisco
!
aaa new-model
aaa authentication login default local enable
!
line vty 0 15
 password cisco

Python 3 telnet example

import getpass
import sys
import telnetlib
host = "192.168.239.129"
user = input("Username: ")
password = input("Password: ") 

tn = telnetlib.Telnet(host)
tn.read_until(b"Username: ")
tn.write(user.encode('ascii') + b"\n")
if password:
  tn.read_until(b"Password: ")
  tn.write(password.encode('ascii') + b"\n")

tn.write(b"enable\n")
tn.write(b"cisco\n")
tn.write(b"conf t\n")
tn.write(b"int loop 0\n")
tn.write(b"ip address 1.1.1.1 255.255.255.255\n")
tn.write(b"int loop 1\n")
tn.write(b"ip address 1.1.1.2 255.255.255.255\n")
tn.write(b"end\n")
tn.write(b"exit\n")
print(tn.read_all())

SSH

To use this module, you need to be able to ssh to one or more Cisco devices.