Netmiko
- Netmiko 라이브러리는 SSH/TELNET 프로토콜을 사용하여 네트워크 장비를 CLI기반으로 접근하여 정보를 수집하거나 명령어를 전달 할 수 있게하는 라이브러리 입니다.
사용을 하기전에 Python, Netmiko 라이브러리를 설치를 하셔야 합니다.

Switch#1에서 ssh 통신이 되도록 기본 설정을 해줍니다 (생략)
from netmiko import ConnectHandler # 모듈 import
# 네트워크 장비의 접속 프로파일 작성
# Dict 형태로 작성
cisco1 = {
'device_type' : 'cisco_ios',
'ip' : '192.168.10.1',
'username' : 'admin',
'password' : 'admin',
'secret' : 'admin', # enable 모드로 진입하기 위함
'port': '22',
'verbose' : True,
"session_log" : "output.txt" # 파일을 만들지 않아도 로그저장
}
net_connect = ConnectHandler(**cisco1) # cisco1 장비 ssh 접속, Dict 형태로 작성되었기 때문에 **를 붙여 준다.
net_connect.enable() # enable 모드 활성화
print(net_connect.find_prompt()) #호스트네임 확인
output2 = net_connect.send_config_set('ip route 10.10.10.0 255.255.255.0 192.168.0.1','\n')# Config ter에서 여러개의 커맨드
print(output2)
conf_set = ["ip dhcp pool user","network 192.168.10.0 255.255.255.0","default-router 192.168.10.254", "dns-server 8.8.8.8",'\n']
output2 = net_connect.send_config_set(conf_set) # Config ter에서 여러개의 커맨드
print(output2)
output = net_connect.send_command('show int status') # 한개의 커맨드
print(output)
output = net_connect.disconnect() # ssh 종료
print(output)
-----------------------------------------------------------------------------------------------------------------
실행을 하면 아래 처럼 결과가 뜹니다.
그리고 위에 파이썬 파일 저장한 곳에 "output.txt" 생성되면 로그가 저장이 됩니다
SSH connection established to 192.168.10.1:22
Interactive SSH session established
L3-1#
config term
Enter configuration commands, one per line. End with CNTL/Z.
L3-1(config)#ip route 10.10.10.0 255.255.255.0 192.168.0.1
L3-1(config)#end
L3-1#
config term
Enter configuration commands, one per line. End with CNTL/Z.
L3-1(config)#ip dhcp pool user
L3-1(dhcp-config)#network 192.168.10.0 255.255.255.0
L3-1(dhcp-config)#default-router 192.168.10.254
L3-1(dhcp-config)#dns-server 8.8.8.8
L3-1(dhcp-config)#
L3-1(dhcp-config)#end
L3-1#
Port Name Status Vlan Duplex Speed Type
Gi0/0 connected routed a-full auto RJ45
Gi0/1 notconnect routed a-full auto RJ45
Gi0/2 notconnect 1 a-full auto RJ45
Gi0/3 notconnect 1 a-full auto RJ45
Gi1/0 notconnect 1 a-full auto RJ45
Gi1/1 notconnect 1 a-full auto RJ45
Gi1/2 notconnect 1 a-full auto RJ45
Gi1/3 notconnect 1 a-full auto RJ45
None
[Finished in 11.0s]


