Documentation page for Mikrotik scripts.
create a file named l2tpclients.csv (with CSV format and ; as delimiter), in the directory: c:/Users/%username%Documents/Python-Networking/Mikrotik/
Get user input such as username, password and IP to connect to Mikrotik device later in script.
## Device system variables
mt_username = input("Username: ")
mt_password = getpass.getpass(prompt="Password: ", stream=None)
mt_host = input("IP address: ")
Set device type to Mikrotik so Netmiko knows how to process commands to device.
## Define Mikrotik variables
device_type = 'mikrotik_routeros'
port = '22'
Get username from currently logged in user for file handling later on in script.
## Get current username
pc_username = getpass.getuser()
Define file location for file handling later on in script.
## Define file variables
base_file_path = os.path.join("c:/Users/", pc_username, "Documents/Python-Networking/Mikrotik/")
input_csv_file = os.path.join(base_file_path, "l2tpclients.csv")
Get CSV file from file location and define delimiter.
## Loop trough l2tp clients
with open(input_csv_file, 'r') as configlist:
csv_reader = csv.reader(configlist, delimiter=';') ##Define path and delimiter
next(csv_reader, None) ##Skip first line where names are defined
Try logging in to Mikrotik device with specified device type, username, password, ip and port from earlier.
try: ##Connect to Mikrotik
net_connect = ConnectHandler(**mikrotik)
print('Successfully logged in')
except Exception as e: ##Return error when error occurs.
print("An error occurred for: ", mt_host, e)
exit ##Stop script if connection fails
Read CSV file line by line and use values later on in script.
## Create loop
for row in csv_reader:
## Define rows
name = row[0]
password = row[1]
dst_address = row[2]
Static values for creating L2TP PPP Secret
## L2TP Client
service = 'any'
profile = 'vpn-profile'
Combine name with 'L2TP-' for naming convention
## L2TP Server binding
l2tp_name = 'l2tp-' + name
user = name
Add static route to dial up PPP client.
## IP Route
gateway = l2tp_name
Create command set with values from CSV file and combined values from earlier in the script.
## Define commands
commands = [
'ppp secret add' + ' name=' + name + ' password=' + password + ' service=' + service + ' profile=' profile,
'interface l2tp-server add' + ' name=' + l2tp_name + ' user=' + user,
'ip route add' + ' dst-address=' + dst_address + ' gateway=' + gateway
]
Send created commands for every line and return which client has been configured
## Send commands and let user know which L2TP client is pushed
net_connect.send_config_set(commands)
print('Created: ' + l2tp_name)
Disconnect session after completion.
## Disconnect
net_connect.disconnect()
print('Done, logged out successfully')
File is almost completely the same as '01 - Create L2TP users from batch' but instead of specifying a password, password gets loaded dynamically from PMP (Password Manager Pro from Manage Engine)
File differences:
## Retrieve password from PMP
pmp_resource = 'Mikrotik'
pmp_apikey = 'xxxxxxxxxxxxxxxx'
pmp_accound_id = 'admin'
pmp_url = '1.1.1.1'
pmp_port = '7272'
## Create request
pmp_password = passwordmanpro_cli.getSinglePassword(
pmp_resource,
pmp_apikey,
skipSSLChecks=True
)
## Specify header with API key
pmp_header = {
'AUTHTOKEN': pmp_apikey
}
## Create payload URL with specified values
pmp_payload_url = f'https://{pmp_url}:{pmp_port}/restapi/json/v1/resources/{pmp_resource}/accounts/{pmp_accound_id}/password'