Start DOS prompt as Administrator.
for /l %i in (1,1,254) do ping -n 1 10.10.10.%i >> sweep.txt
Start DOS prompt as Administrator.
for /l %i in (1,1,254) do ping -n 1 10.10.10.%i >> sweep.txt
https://pypi.org/project/ciscoconfparse/
Building configuration files from a template…
VOSS.J2
This took me some time to get to work using Windows / Cygwin and my GNS3 VOSS simulated switch.
I struggled with getting Ansible + SSH to work from within Cygwin using key based authentication. But I wanted to test Ansible and Ansible-Playbook against VOSS so I persevered with simple user and password authentication which I got to work using a combination of files (ansible.cfg, inventory file = hosts and a test.yml file to run a single show command and display the output).
Hosts file
First stage, was to get a ‘Pong’ response to my ‘Ping’:
-bash-4.4$ ansible -i ./inventory/hosts voss -u rwa -m ping -c network_cli -e ansible_network_os=voss
192.168.211.10 | SUCCESS => {
“ansible_facts”: {
“discovered_interpreter_python”: “/usr/bin/python”
},
“changed”: false,
“ping”: “pong”
Next, I wanted to run Ansible which would login to the switch and run a simple command:
-bash-4.4$ ansible -i ./inventory/hosts voss -u rwa -m voss_command -a “commands=’show clock'”
192.168.211.10 | SUCCESS => {
“ansible_facts”: {
“discovered_interpreter_python”: “/usr/bin/python”
},
“changed”: false,
“stdout”: [
“Sat Jun 13 13:02:15 2020 UTC”
],
“stdout_lines”: [
[
“Sat Jun 13 13:02:15 2020 UTC”
]
]
}
Now that Ansible could login and carry out a task I progressed to put a task into a Playbook:
when: output.stdout is search(‘VOSS1’)
debug:
$ cat playbook1.yml
—
– name: “Play 1: Capture sys-info”
hosts: routers
connection: network_cli
tasks:
– name: “Task 1: Show sys-info”
voss_command:
commands: show sys-info
register: result
– name: “Task 2: Print output”
debug:
msg: “{{ result }}”
– name: “Task 3: Create files folder”
file:
path: “outputs”
state: directory
run_once: true
– name: “Task 4: Write stdout to file”
copy:
content: “{{ result.stdout[0] }}\n”
dest: “outputs/{{ inventory_hostname }}.txt”
Get ARP table from VSP switches…
from nornir import InitNornir
from nornir.plugins.tasks.networking import netmiko_send_command
from nornir.plugins.functions.text import print_result
nr = InitNornir()
result = nr.run(
task=netmiko_send_command,
command_string=”show ip arp”
)
print_result(result)
Hosts.yaml
—
voss-1:
hostname: ‘192.168.1.10’
port: 22
username: ‘rwa’
password: ‘rwa’
platform: ‘extreme_vsp’
voss-2:
hostname: ‘192.168.1.11’
port: 22
username: ‘rwa’
password: ‘rwa’
platform: ‘extreme_vsp’
Device_Type: Extreme_VSP
Get list of IP interfaces…
from netmiko import ConnectHandler
voss1 = {‘device_type’: ‘extreme_vsp’, ‘host’: ‘192.168.1.10’, ‘username’: ‘rwa’, ‘password’: ‘rwa’}
net_connect = ConnectHandler(**voss1)
net_connect.find_prompt()
output = net_connect.send_command(‘show ip interface’)
print(output)
Making a configuration change, for example, disabling FTPD…
from netmiko import ConnectHandler
voss2 = {‘device_type’: ‘extreme_vsp’, ‘host’: ‘192.168.1.11’, ‘username’: ‘rwa’, ‘password’: ‘rwa’}
net_connect = ConnectHandler(**voss2)
net_connect.find_prompt()
net_connect.enable()
net_connect.send_config_set([‘no boot config flags ftpd’])
Device_Type: Extreme_ERS
from netmiko import ConnectHandler
ers1 = {‘device_type’: ‘extreme_ers’, ‘host’: ‘192.168.1.5’, ‘username’: ‘RW’, ‘password’: ‘securepasswd’}
net_connect = ConnectHandler(**ers1)
net_connect.find_prompt()
output = net_connect.send_command(‘show system’)
print(output)
Paramiko script which logs in to VOSS and sets the FTPD boot flag.
Devices.json
{
“voss-1”: {“ip”: “192.168.1.10”},
“voss-2”: {“ip”: “192.168.1.11”}
}
Commands.txt
enable
config t
boot config flags ftpd
exit
save config
exit
Verify changes made to running-config and configuration file on intflash.
show run | I ftpd
show grep ftpd config.cfg
VSP-8284XSQ-1:1#show run | i ftpd
boot config flags ftpd
VSP-8284XSQ-1:1#grep ftpd config.cfg
boot config flags ftpd
VSP-8284XSQ-1:1#
Script:
import paramiko, getpass, time, json
with open(‘devices.json’, ‘r’) as f:
devices = json.load(f)
with open(‘commands.txt’, ‘r’) as f:
commands = f.readlines()
username = input(‘Username: ‘)
password = getpass.getpass(‘Password: ‘)
max_buffer = 65535
def clear_buffer(connection):
if connection.recv_ready():
return connection.recv(max_buffer)
# Starts the loop for devices
for device in devices.keys():
outputFileName = device + ‘_output.txt’
connection = paramiko.SSHClient()
connection.set_missing_host_key_policy(paramiko.AutoAddPolicy())
connection.connect(devices[device][‘ip’], username=username, password=password, look_for_keys=False, allow_agent=False)
new_connection = connection.invoke_shell()
output = clear_buffer(new_connection)
time.sleep(2)
new_connection.send(“terminal more disable\n”)
output = clear_buffer(new_connection)
with open(outputFileName, ‘wb’) as f:
for command in commands:
new_connection.send(command)
time.sleep(2)
output = new_connection.recv(max_buffer)
print(output)
f.write(output)
new_connection.close()
Mastering Python Networking (Paramiko).
import paramiko, getpass, time
devices = {‘voss-1’: {‘ip’: ‘192.168.1.10’},
‘voss-2’: {‘ip’: ‘192.168.1.11’}}
commands = [‘enable\n’, ‘show software\n’, ‘show sys-info card\n’, ‘exit\n’]
username = input(‘Username: ‘)
password = getpass.getpass(‘Password: ‘)
max_buffer = 65535
def clear_buffer(connection):
if connection.recv_ready():
return connection.recv(max_buffer)
# Starts the loop for devices
for device in devices.keys():
outputFileName = device + ‘_output.txt’
connection = paramiko.SSHClient()
connection.set_missing_host_key_policy(paramiko.AutoAddPolicy())
connection.connect(devices[device][‘ip’], username=username, password=password, look_for_keys=False, allow_agent=False)
new_connection = connection.invoke_shell()
output = clear_buffer(new_connection)
time.sleep(5)
new_connection.send(“terminal more disable\n”)
output = clear_buffer(new_connection)
with open(outputFileName, ‘wb’) as f:
for command in commands:
new_connection.send(command)
time.sleep(5)
output = new_connection.recv(max_buffer)
print(output)
f.write(output)
new_connection.close()
Mastering Python Networking (Pexpect).
import getpass
from pexpect import pxssh
import time
devices = {‘VSP-8284XSQ-1’: {‘prompt’: ‘VSP-8284XSQ-1:1>’, ‘ip’: ‘192.168.1.10’}, ‘VSP-8284XSQ-2’: {‘prompt’: ‘VSP-8284XSQ-2:1>’, ‘ip’: ‘192.168.1.11’}}
commands = [‘terminal more disable’, ‘show sys-info card’, ‘terminal more enable’]
username = input(‘Username: ‘)
password = getpass.getpass(‘Password: ‘)
for device in devices.keys():
outputFileName = device + ‘_output.txt’
device_prompt = devices[device] [‘prompt’]
device_ip = devices[device] [‘ip’]
child = pxssh.pxssh()
child.login(devices[device] [‘ip’], username.strip(), password.strip(), auto_prompt_reset=False)
print(‘Logged in to ‘ + device)
with open(outputFileName, ‘wb’) as f:
for command in commands:
child.expect(device_prompt)
child.sendline(command)
time.sleep(1)
f.write(child.before)
child.logout()