Commit 4ec3058c authored by Mr Phong's avatar Mr Phong

Init project

parents
Pipeline #143 failed with stages
__pycache__/
ThaoTestLinhTinh/
*.ipynb_checkpoints
# *.ipynb
utils/
.coverage
coverage.xml
junit.xml
research results
\ No newline at end of file
Hướng dẫn sử dụng ssh-tool.
I. Mô tả
ssh-tool là ứng dụng viết bằng python và trên thư viện netmiko để chạy 1 tập lệnh cho 1 tập các thiết bị nhằm mục đích tiết kiệm thời gian và tránh sai sót khi cấu hình cùng lúc nhiều thiết bị theo một kịch bản (tập lệnh) cố định.
1. File hosts.csv chứa thông tin về host, lưu ý chọn kiểu thiết bị device_type cho phù hợp. Tham khảo thêm tại https://github.com/ktbyers/netmiko/blob/develop/PLATFORMS.md
2. File commands.txt chứa tập lệnh cần chạy, có các lưu ý sau
- Mỗi lệnh là 1 dòng
- Các lệnh đặc biệt #enable_mode, #exit_enable_mode, @config_mode, #exit_config_mode để đưa thiết bị vào trạng thái enable hay config mode
3. File output.txt chứa kết quả thực thi
4. File config.py chứa cấu hình đường dẫn các file trên, có thể đổi tên file hay đường dẫn nếu muốn. Ngoài ra có format ghi log file output, có thể điều chỉnh thêm thời gian ghi log nếu muốn
II. Cài đặt và chạy ứng dụng
1. Cài đặt python 3.7 trở lên, truy cập https://www.python.org/ và tìm gói cài đặt tương ứng
Sau khi cài đặt thành công thì kiểm tra xem python đã hoạt động chưa bằng cách mở command và gõ lệnh
python
Nếu hiển thị thông tin version và dấu nhắc >>> thì nghĩa là đã cài đặt thành công (ví dụ)
Python 3.7.9 (tags/v3.7.9:13c94747c7, Aug 17 2020, 18:58:18) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
2. Cài đặt pip (Preferred Installer Program), là trình quản lý package trên python
Đa phần khi cài đặt python thì đã kèm pip, để kiểm tra ta mở command và gõ lệnh
pip
Nếu hiển thị kết quả nghĩa là đã cài đặt, nếu chưa có thì tham khảo và cài đặt tại đường dẫn https://pip.pypa.io/en/stable/installation/
3. Cài đặt các gói cần thiết để chạy ứng dụng
Mở command, enter vào thư mục chứa ứng dụng và chạy lệnh sau để cài đặt các gói
pip install -r requirements.txt --no-cache-dir --compile
4. Chạy ứng dụng
Mở command, enter vào thư mục chứa ứng dụng và chạy lệnh sau để chạy ứng dụng
python app.py
\ No newline at end of file
from asyncio.log import logger
from typing import List
import sys
from netmiko import ConnectHandler, BaseConnection
from pandas import read_csv, DataFrame
from config import host_file, command_file, output_file, log_fomartter
import logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
fomartter = logging.Formatter(log_fomartter)
stream = logging.StreamHandler()
stream.setFormatter(fomartter)
logger.addHandler(stream)
file = logging.FileHandler(output_file)
file.setFormatter(fomartter)
logger.addHandler(file)
def read_devices(csv_file: str) -> DataFrame:
df = read_csv(csv_file)
return df
def read_commands(command_file: str) -> List[str]:
with open(command_file) as f:
return f.readlines()
def run_commands(connection: BaseConnection, command_list: List[str]):
for command in command_list:
if(command.strip()!=""):
if(command.strip()=="#enable_mode"):
logger.info("Entering enable mode")
connection.enable()
elif(command.strip()=="#end_enable_mode"):
logger.info("Exiting enable mode")
connection.exit_enable_mode()
elif(command.strip()=="#config_mode"):
logger.info("Entering config mode")
connection.config_mode()
elif(command.strip()=="#end_config_mode"):
logger.info("Exiting config mode")
connection.exit_config_mode()
else:
output= connection.send_command(command)
logger.info(output)
df = read_devices(host_file)
command_list = read_commands(command_file)
with open(output_file, 'w') as file_output:
file_output.truncate()
sys.stdout = file_output
for index, row in df.iterrows():
logger.info("Connect to host: " + str(row['host']))
try:
with ConnectHandler(**row) as device_connect:
logger.info("Connect to host : " + str(row['host']) + " success !")
run_commands(device_connect, command_list)
except Exception as ex:
logger.info(ex)
sys.stdout.close()
show clock
#enable_mode
show adoption status
#end_enable_mode
\ No newline at end of file
host_file = "hosts.csv"
command_file = "commands.txt"
output_file = "output.txt"
log_fomartter = "%(message)s"
# Nếu muốn thêm thời gian thì dùng format bên dưới
# log_fomartter = "%(asctime)s:%(message)s"
\ No newline at end of file
device_type,host,username,password,port
extreme_wing,172.16.20.2,admin,@wIng202!#manager,22
Connect to host: 172.16.20.2
Connect to host : 172.16.20.2 success !
2022-05-24 18:05:43 ICT
Entering enable mode
--------------------------------------------------------------------------------------------------------------------------------
DEVICE-NAME VERSION CFG-STAT MSGS ADOPTED-BY LAST-ADOPTION UPTIME IPv4-ADDRESS
--------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------------
Total number of devices displayed: 0
Exiting enable mode
pandas
netmiko
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment