Commit c80e7eb5 authored by Mr Phong's avatar Mr Phong

add expect string

parent 5a5d46b1
...@@ -25,7 +25,7 @@ Trong cả 2 trường hợp, giả sử rằng thư mục source code đã down ...@@ -25,7 +25,7 @@ Trong cả 2 trường hợp, giả sử rằng thư mục source code đã down
### 1. File hosts.txt ### 1. File hosts.txt
Chứa thông tin về host, dữ liệu đầu vào theo chuẩn csv, các trường dữ liệu cách nhau vởi dấu , Chứa thông tin về host, dữ liệu đầu vào theo chuẩn csv, các trường dữ liệu cách nhau vởi dấu *,*
Thông tin các trường dữ liệu như sau : Thông tin các trường dữ liệu như sau :
- device_type : Loại thiết bị, lưu ý chọn đúng kiểu thiết bị. Tham khảo thêm tại https://github.com/ktbyers/netmiko/blob/develop/PLATFORMS.md - device_type : Loại thiết bị, lưu ý chọn đúng kiểu thiết bị. Tham khảo thêm tại https://github.com/ktbyers/netmiko/blob/develop/PLATFORMS.md
...@@ -42,13 +42,13 @@ Chứa tập lệnh cần chạy, cũng theo chuẩn csv giống như file hosts ...@@ -42,13 +42,13 @@ Chứa tập lệnh cần chạy, cũng theo chuẩn csv giống như file hosts
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 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
- expect_string : Đoạn string pattern regex cần tìm trong kết quả trả về để xác định là command đã hoàn thành, thường dùng nếu các câu lệnh thay đổi dấu nhắc của thiết bị. Ví dụ thiết bị mikrotik_routeros gửi command như sau - expect_string : Đoạn string pattern regex cần tìm trong kết quả trả về để xác định là command đã hoàn thành, thường dùng nếu các câu lệnh thay đổi dấu nhắc của thiết bị. Ví dụ thiết bị mikrotik_routeros gửi command như sau
/ip hotspot walled-garden */ip hotspot walled-garden*
Thì dấu nhắc mới có thể là Thì dấu nhắc mới có thể là
[admin@MikroTik] /ip hotspot walled-garden> *[admin@MikroTik] /ip hotspot walled-garden>*
Thì expect_string có thể là walled-garden> Thì expect_string có thể là *walled-garden>*
### 3. File output.txt ### 3. File output.txt
......
...@@ -21,12 +21,13 @@ file.setFormatter(fomartter) ...@@ -21,12 +21,13 @@ file.setFormatter(fomartter)
logger.addHandler(file) logger.addHandler(file)
def run_commands(connection: BaseConnection, df_command: DataFrame): def run_commands(connection: BaseConnection, df_command: DataFrame):
for index, row in df_command.iterrows(): for index, row in df_command.iterrows():
command = str(row['command']).strip() command = str(row['command']).strip()
expect_string = str(row['expect_string']) expect_string = str(row['expect_string'])
if(str(row['command']).strip()!=""): if(str(row['command']).strip()!=""):
""" if(command.strip()=="#enable_mode"): if(command.strip()=="#enable_mode"):
logger.info("Entering enable mode") logger.info("Entering enable mode")
connection.enable() connection.enable()
elif(command.strip()=="#end_enable_mode"): elif(command.strip()=="#end_enable_mode"):
...@@ -38,11 +39,12 @@ def run_commands(connection: BaseConnection, df_command: DataFrame): ...@@ -38,11 +39,12 @@ def run_commands(connection: BaseConnection, df_command: DataFrame):
elif(command.strip()=="#end_config_mode"): elif(command.strip()=="#end_config_mode"):
logger.info("Exiting config mode") logger.info("Exiting config mode")
connection.exit_config_mode() connection.exit_config_mode()
else: """ else:
output= connection.send_command(command, strip_prompt=False, strip_command=False, expect_string=expect_string) output= connection.send_command(command, strip_prompt=False, strip_command=False, expect_string=expect_string)
logger.info(output) logger.info(output)
df_host = read_csv(host_file) df_host = read_csv(host_file)
df_command = read_csv(command_file) df_command = read_csv(command_file)
with open(output_file, 'w') as file_output: with open(output_file, 'w') as file_output:
......
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