1. getpass, telnetlib 모듈을 이용한 telnet 설정
import getpass # password를 설정하기 위한 모듈을 불러온다
import telnetlib
HOST = '192.168.100.20'
user = input('사용자를 입력하세요 : ')
password = getpass.getpass() # 화면 상에 password를 안보이게 함
print('HOST IP : ', HOST)
print('사 용 자 : ', user)
print('비밀번호 : ', password)
# 'telnetlib' 모듈에 있는 'telnet' 함수에 HOST 인자를 넣고, 그 값을 변수에 저장
tn = telnetlib.Telnet(HOST)
print(HOST)
# username 입력 정보를 기다림
tn.read_until(b'Username: ')
# user를 아스키코드로 인코드하여, 텔넷 가상 터미널로 보냄
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 lo 0\n')
tn.write(b'ip add 1.1.1.1 255.255.255.255\n')
tn.write(b'end\n')
tn.write(b'exit\n')
# 세션(write)에 실행된 모든 명령어를 보여주고, 아스키 코드로 디코드하여 사용자 화면에 출력
print(tn.read_all().decode('ascii'))
2. designer를 이용한 telnet 설정
2.1) designer로 설정한 ui를 py파일로 변환
2.1.1) menu02.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>733</width>
<height>754</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QTabWidget" name="tabWidget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>811</width>
<height>721</height>
</rect>
</property>
<property name="font">
<font>
<family>Arial</family>
<pointsize>12</pointsize>
</font>
</property>
<property name="currentIndex">
<number>1</number>
</property>
<widget class="QWidget" name="tab">
<attribute name="title">
<string>초기화면</string>
</attribute>
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>731</width>
<height>651</height>
</rect>
</property>
<property name="text">
<string/>
</property>
<property name="pixmap">
<pixmap>../../Desktop/zzzzz.JPG</pixmap>
</property>
</widget>
</widget>
<widget class="QWidget" name="tab_2">
<attribute name="title">
<string>라우터설정</string>
</attribute>
<widget class="QLabel" name="label_2">
<property name="geometry">
<rect>
<x>20</x>
<y>30</y>
<width>91</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>사용자 이름</string>
</property>
</widget>
<widget class="QLabel" name="label_3">
<property name="geometry">
<rect>
<x>280</x>
<y>20</y>
<width>56</width>
<height>31</height>
</rect>
</property>
<property name="text">
<string>암 호</string>
</property>
</widget>
<widget class="QLabel" name="label_4">
<property name="geometry">
<rect>
<x>470</x>
<y>20</y>
<width>81</width>
<height>31</height>
</rect>
</property>
<property name="text">
<string>ip address</string>
</property>
</widget>
<widget class="QTextEdit" name="UserID">
<property name="geometry">
<rect>
<x>120</x>
<y>20</y>
<width>104</width>
<height>31</height>
</rect>
</property>
</widget>
<widget class="QTextEdit" name="HOSTIp">
<property name="geometry">
<rect>
<x>560</x>
<y>20</y>
<width>131</width>
<height>31</height>
</rect>
</property>
</widget>
<widget class="QLineEdit" name="PWDText">
<property name="geometry">
<rect>
<x>330</x>
<y>20</y>
<width>113</width>
<height>31</height>
</rect>
</property>
<property name="text">
<string/>
</property>
<property name="echoMode">
<enum>QLineEdit::Password</enum>
</property>
</widget>
<widget class="QPlainTextEdit" name="Contents">
<property name="geometry">
<rect>
<x>10</x>
<y>60</y>
<width>701</width>
<height>581</height>
</rect>
</property>
</widget>
<widget class="QPushButton" name="RouterBtn">
<property name="geometry">
<rect>
<x>590</x>
<y>650</y>
<width>91</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>설정</string>
</property>
</widget>
</widget>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>733</width>
<height>21</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
2.1.1) menu02.py
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(733, 754)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.tabWidget = QtWidgets.QTabWidget(self.centralwidget)
self.tabWidget.setGeometry(QtCore.QRect(0, 0, 811, 721))
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(12)
self.tabWidget.setFont(font)
self.tabWidget.setObjectName("tabWidget")
self.tab = QtWidgets.QWidget()
self.tab.setObjectName("tab")
self.label = QtWidgets.QLabel(self.tab)
self.label.setGeometry(QtCore.QRect(0, 0, 731, 651))
self.label.setText("")
self.label.setPixmap(QtGui.QPixmap("../../Desktop/zzzzz.JPG"))
self.label.setObjectName("label")
self.tabWidget.addTab(self.tab, "")
self.tab_2 = QtWidgets.QWidget()
self.tab_2.setObjectName("tab_2")
self.label_2 = QtWidgets.QLabel(self.tab_2)
self.label_2.setGeometry(QtCore.QRect(20, 30, 91, 16))
self.label_2.setObjectName("label_2")
self.label_3 = QtWidgets.QLabel(self.tab_2)
self.label_3.setGeometry(QtCore.QRect(280, 20, 56, 31))
self.label_3.setObjectName("label_3")
self.label_4 = QtWidgets.QLabel(self.tab_2)
self.label_4.setGeometry(QtCore.QRect(470, 20, 81, 31))
self.label_4.setObjectName("label_4")
self.UserID = QtWidgets.QTextEdit(self.tab_2)
self.UserID.setGeometry(QtCore.QRect(120, 20, 104, 31))
self.UserID.setObjectName("UserID")
self.HOSTIp = QtWidgets.QTextEdit(self.tab_2)
self.HOSTIp.setGeometry(QtCore.QRect(560, 20, 131, 31))
self.HOSTIp.setObjectName("HOSTIp")
self.PWDText = QtWidgets.QLineEdit(self.tab_2)
self.PWDText.setGeometry(QtCore.QRect(330, 20, 113, 31))
self.PWDText.setText("")
self.PWDText.setEchoMode(QtWidgets.QLineEdit.Password)
self.PWDText.setObjectName("PWDText")
self.Contents = QtWidgets.QPlainTextEdit(self.tab_2)
self.Contents.setGeometry(QtCore.QRect(10, 60, 701, 581))
self.Contents.setObjectName("Contents")
self.RouterBtn = QtWidgets.QPushButton(self.tab_2)
self.RouterBtn.setGeometry(QtCore.QRect(590, 650, 91, 23))
self.RouterBtn.setObjectName("RouterBtn")
self.RouterBtn.clicked.connect(self.RouterConfig)
self.tabWidget.addTab(self.tab_2, "")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 733, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
self.tabWidget.setCurrentIndex(1)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), _translate("MainWindow", "초기화면"))
self.label_2.setText(_translate("MainWindow", "사용자 이름"))
self.label_3.setText(_translate("MainWindow", "암 호"))
self.label_4.setText(_translate("MainWindow", "ip address"))
self.RouterBtn.setText(_translate("MainWindow", "설정"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_2), _translate("MainWindow", "라우터설정"))
2.2) menu02.py에 ui창을 띄우기 위한 코드 추가
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
2.3) Telnet 접속을 위한 코드 추가
def RouterConfig(self):
print("RouterConfig")
import telnetlib
print('HOST IP : ', self.HOSTIp.toPlainText()) # designer에 있는 HOSTIp를 출력
print('사 용 자 : ', self.UserID.toPlainText()) # designer에 있는 UserID를 출력
print('암 호 : ', self.PWDText.text()) # designer에 있는 PWDText를 출력
HOST = self.HOSTIp.toPlainText()
password = self.PWDText.text()
user = self.UserID.toPlainText()
# 'telnetlib' 모듈에 있는 'telnet' 함수에 HOST 인자를 넣고, 그 값을 변수에 저장
tn = telnetlib.Telnet(HOST)
print(HOST)
# username 입력 정보를 기다림
tn.read_until(b'Username: ')
# user를 아스키코드로 인코드하여, 텔넷 가상 터미널로 보냄
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 lo 1\n')
tn.write(b'ip add 2.2.2.2 255.255.255.255\n')
tn.write(b'end\n')
tn.write(b'exit\n')
# 세션(write)에 실행된 모든 명령어를 보여주고, 아스키 코드로 디코드하여 사용자 화면에 출력
print(tn.read_all().decode('ascii'))
2.4) 버튼을 누르면 실행되게 하기 위한 코드 추가
self.RouterBtn.clicked.connect(self.RouterConfig)
<전체 코드>
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(733, 754)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.tabWidget = QtWidgets.QTabWidget(self.centralwidget)
self.tabWidget.setGeometry(QtCore.QRect(0, 0, 811, 721))
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(12)
self.tabWidget.setFont(font)
self.tabWidget.setObjectName("tabWidget")
self.tab = QtWidgets.QWidget()
self.tab.setObjectName("tab")
self.label = QtWidgets.QLabel(self.tab)
self.label.setGeometry(QtCore.QRect(0, 0, 731, 651))
self.label.setText("")
self.label.setPixmap(QtGui.QPixmap("../../Desktop/zzzzz.JPG"))
self.label.setObjectName("label")
self.tabWidget.addTab(self.tab, "")
self.tab_2 = QtWidgets.QWidget()
self.tab_2.setObjectName("tab_2")
self.label_2 = QtWidgets.QLabel(self.tab_2)
self.label_2.setGeometry(QtCore.QRect(20, 30, 91, 16))
self.label_2.setObjectName("label_2")
self.label_3 = QtWidgets.QLabel(self.tab_2)
self.label_3.setGeometry(QtCore.QRect(280, 20, 56, 31))
self.label_3.setObjectName("label_3")
self.label_4 = QtWidgets.QLabel(self.tab_2)
self.label_4.setGeometry(QtCore.QRect(470, 20, 81, 31))
self.label_4.setObjectName("label_4")
self.UserID = QtWidgets.QTextEdit(self.tab_2)
self.UserID.setGeometry(QtCore.QRect(120, 20, 104, 31))
self.UserID.setObjectName("UserID")
self.HOSTIp = QtWidgets.QTextEdit(self.tab_2)
self.HOSTIp.setGeometry(QtCore.QRect(560, 20, 131, 31))
self.HOSTIp.setObjectName("HOSTIp")
self.PWDText = QtWidgets.QLineEdit(self.tab_2)
self.PWDText.setGeometry(QtCore.QRect(330, 20, 113, 31))
self.PWDText.setText("")
self.PWDText.setEchoMode(QtWidgets.QLineEdit.Password)
self.PWDText.setObjectName("PWDText")
self.Contents = QtWidgets.QPlainTextEdit(self.tab_2)
self.Contents.setGeometry(QtCore.QRect(10, 60, 701, 581))
self.Contents.setObjectName("Contents")
self.RouterBtn = QtWidgets.QPushButton(self.tab_2)
self.RouterBtn.setGeometry(QtCore.QRect(590, 650, 91, 23))
self.RouterBtn.setObjectName("RouterBtn")
self.RouterBtn.clicked.connect(self.RouterConfig)
self.tabWidget.addTab(self.tab_2, "")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 733, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
self.tabWidget.setCurrentIndex(1)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), _translate("MainWindow", "초기화면"))
self.label_2.setText(_translate("MainWindow", "사용자 이름"))
self.label_3.setText(_translate("MainWindow", "암 호"))
self.label_4.setText(_translate("MainWindow", "ip address"))
self.RouterBtn.setText(_translate("MainWindow", "설정"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_2), _translate("MainWindow", "라우터설정"))
def RouterConfig(self):
print("RouterConfig")
import telnetlib
print('HOST IP : ', self.HOSTIp.toPlainText()) # designer에 있는 HOSTIp를 출력
print('사 용 자 : ', self.UserID.toPlainText()) # designer에 있는 UserID를 출력
print('암 호 : ', self.PWDText.text()) # designer에 있는 PWDText를 출력
HOST = self.HOSTIp.toPlainText()
password = self.PWDText.text()
user = self.UserID.toPlainText()
# 'telnetlib' 모듈에 있는 'telnet' 함수에 HOST 인자를 넣고, 그 값을 변수에 저장
tn = telnetlib.Telnet(HOST)
print(HOST)
# username 입력 정보를 기다림
tn.read_until(b'Username: ')
# user를 아스키코드로 인코드하여, 텔넷 가상 터미널로 보냄
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 lo 1\n')
tn.write(b'ip add 2.2.2.2 255.255.255.255\n')
tn.write(b'end\n')
tn.write(b'exit\n')
# 세션(write)에 실행된 모든 명령어를 보여주고, 아스키 코드로 디코드하여 사용자 화면에 출력
print(tn.read_all().decode('ascii'))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
'네트워크 > 자동화' 카테고리의 다른 글
python - telnet 자동화 2 (0) | 2024.02.21 |
---|