---
# System prepended metadata

title: Netmiko shell demo

---

# Netmiko shell demo

```python
import os
from netmiko import ConnectHandler
from netmiko_model import host_list

if os.path.exists('netmiko_session.log'):
    os.remove('netmiko_session.log')

username = 'ptc'
password = 'fgt@ptc'

for index in range(len(host_list)):
        with ConnectHandler(
        device_type='fortinet',
        ip=host_list[index],
        username=username,
        password=password,
        session_log=f'netmiko_session.log',
        session_log_file_mode='append'
    ) as connect:
        connect.send_command('show firewall addrgrp CloudOP', expect_string='.*#')
        # 調整 CloudOP 為 Group 並加入 SSL-VPN Policy
        connect.send_command('config firewall address', expect_string='.*#')
        connect.send_command('edit "Host_210.69.151.50"', expect_string='.*#')
        connect.send_command('set subnet 210.69.151.50 255.255.255.255', expect_string='.*#')
        connect.send_command('next', expect_string='.*#')
        connect.send_command('edit "Host_210.69.151.51"', expect_string='.*#')
        connect.send_command('set subnet 210.69.151.51 255.255.255.255', expect_string='.*#')
        connect.send_command('next', expect_string='.*#')
        connect.send_command('edit "Host_210.69.151.52"', expect_string='.*#')
        connect.send_command('set subnet 210.69.151.52 255.255.255.255', expect_string='.*#')
        connect.send_command('next', expect_string='.*#')
        connect.send_command('edit "Host_210.69.151.55"', expect_string='.*#')
        connect.send_command('set subnet 210.69.151.55 255.255.255.255', expect_string='.*#')
        connect.send_command('next', expect_string='.*#')
        connect.send_command('edit "Host_210.69.151.56"', expect_string='.*#')
        connect.send_command('set subnet 210.69.151.56 255.255.255.255', expect_string='.*#')
        connect.send_command('next', expect_string='.*#')
        connect.send_command('edit "Host_210.69.151.57"', expect_string='.*#')
        connect.send_command('set subnet 210.69.151.57 255.255.255.255', expect_string='.*#')
        connect.send_command('next', expect_string='.*#')
        connect.send_command('edit "Host_210.69.151.58"', expect_string='.*#')
        connect.send_command('set subnet 210.69.151.58 255.255.255.255', expect_string='.*#')
        connect.send_command('next', expect_string='.*#')
        connect.send_command('edit "Host_210.69.151.66"', expect_string='.*#')
        connect.send_command('set subnet 210.69.151.66 255.255.255.255', expect_string='.*#')
        connect.send_command('next', expect_string='.*#')
        connect.send_command('edit "Host_210.69.151.67"', expect_string='.*#')
        connect.send_command('set subnet 210.69.151.67 255.255.255.255', expect_string='.*#')
        connect.send_command('next', expect_string='.*#')
        connect.send_command('edit "Host_210.69.151.170"', expect_string='.*#')
        connect.send_command('set subnet 210.69.151.170 255.255.255.255', expect_string='.*#')
        connect.send_command('next', expect_string='.*#')
        connect.send_command('edit "Host_210.69.151.171"', expect_string='.*#')
        connect.send_command('set subnet 210.69.151.171 255.255.255.255', expect_string='.*#')
        connect.send_command('next', expect_string='.*#')
        connect.send_command('end', expect_string='.*#')

        connect.send_command('config firewall policy', expect_string='.*#')
        connect.send_command('edit 1011', expect_string='.*#')
        connect.send_command('set dstaddr "N-Cloud Server" "SFS Server" "NET_163.24.180.0/22" "SFS plug-in"', expect_string='.*#')
        connect.send_command('next', expect_string='.*#')
        connect.send_command('end', expect_string='.*#')
        connect.send_command('config firewall address', expect_string='.*#')
        connect.send_command('delete CloudOP', expect_string='.*#')
        connect.send_command('end', expect_string='.*#')
        connect.send_command('config firewall addrgrp', expect_string='.*#')
        connect.send_command('edit "CloudOP"', expect_string='.*#')
        connect.send_command('set member "Host_210.69.151.170" "Host_210.69.151.171" "Host_210.69.151.66" "Host_210.69.151.67" "Host_210.69.151.50" "Host_210.69.151.51" "Host_210.69.151.52" "Host_210.69.151.55" "Host_210.69.151.56" "Host_210.69.151.57" "Host_210.69.151.58"', expect_string='.*#')
        connect.send_command('next', expect_string='.*#')
        connect.send_command('end', expect_string='.*#')
        connect.send_command('config firewall policy', expect_string='.*#')
        connect.send_command('edit 1011', expect_string='.*#')
        connect.send_command('set dstaddr "N-Cloud Server" "SFS Server" "NET_163.24.180.0/22" "CloudOP" "SFS plug-in"', expect_string='.*#')
        connect.send_command('next', expect_string='.*#')
        connect.send_command('end', expect_string='.*#')
```