Skip to content

Commit

Permalink
Initial commit from Ch1 - Ch6, added Python foo for .gitignore
Browse files Browse the repository at this point in the history
  • Loading branch information
ericchou1 committed Feb 27, 2017
1 parent 46483ac commit 8063477
Show file tree
Hide file tree
Showing 96 changed files with 132,151 additions and 0 deletions.
98 changes: 98 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,101 @@ $RECYCLE.BIN/
Network Trash Folder
Temporary Items
.apdisk

# Python gitignore

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*,cover
.hypothesis/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# pyenv
.python-version

# celery beat schedule file
celerybeat-schedule

# SageMath parsed files
*.sage.py

# dotenv
.env

# virtualenv
.venv
venv/
ENV/

# Spyder project settings
.spyderproject

# Rope project settings
.ropeproject

3 changes: 3 additions & 0 deletions Chapter1/helloworld.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# This is a comment
print("hello world")

Empty file added Chapter1/math_stuff/__init__.py
Empty file.
Binary file not shown.
Binary file not shown.
4 changes: 4 additions & 0 deletions Chapter1/math_stuff/subtract.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
def subtract(a, b):
c = a - b
return c

28 changes: 28 additions & 0 deletions Chapter2/chapter2_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/python3

import pexpect

devices = {'iosv-1': {'prompt': 'iosv-1#', 'ip': '172.16.1.20'},
'iosv-2': {'prompt': 'iosv-2#', 'ip': '172.16.1.21'}}
username = 'cisco'
password = 'cisco'

for device in devices.keys():
device_prompt = devices[device]['prompt']
child = pexpect.spawn('telnet ' + devices[device]['ip'])
child.expect('Username:')
child.sendline(username)
child.expect('Password:')
child.sendline(password)
child.expect(device_prompt)
child.sendline('show version | i V')
child.expect(device_prompt)
print(child.before)
child.sendline('exit')







34 changes: 34 additions & 0 deletions Chapter2/chapter2_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/python3

import getpass
from pexpect import pxssh

devices = {'iosv-1': {'prompt': 'iosv-1#', 'ip': '172.16.1.20'},
'iosv-2': {'prompt': 'iosv-2#', 'ip': '172.16.1.21'}}
commands = ['term length 0', 'show version', 'show run']

username = input('Username: ')
password = getpass.getpass('Password: ')

# Starts the loop for devices
for device in devices.keys():
outputFileName = device + '_output.txt'
device_prompt = devices[device]['prompt']
child = pxssh.pxssh()
child.login(devices[device]['ip'], username.strip(), password.strip(), auto_prompt_reset=False)
# Starts the loop for commands and write to output
with open(outputFileName, 'wb') as f:
for command in commands:
child.sendline(command)
child.expect(device_prompt)
f.write(child.before)

child.logout()








39 changes: 39 additions & 0 deletions Chapter2/chapter2_3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/python3

import paramiko, getpass, time

devices = {'iosv-1': {'ip': '172.16.1.20'},
'iosv-2': {'ip': '172.16.1.21'}}
commands = ['show version\n', 'show run\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(2)
new_connection.send("terminal length 0\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()


41 changes: 41 additions & 0 deletions Chapter2/chapter2_4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/python3

import paramiko, getpass, time, json

with open('devices.json', 'r') as f:
devices = json.load(f)

with open('commands.txt', 'r') as f:
commands = [line for line in 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 length 0\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()


4 changes: 4 additions & 0 deletions Chapter2/commands.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
config t
logging buffered 30000
end
copy run start
5 changes: 5 additions & 0 deletions Chapter2/devices.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"iosv-1": {"ip": "172.16.1.20"},
"iosv-2": {"ip": "172.16.1.21"}
}

5 changes: 5 additions & 0 deletions Chapter2/hosts.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"iosv-1": {"ip": "172.16.1.20"},
"iosv-2": {"ip": "172.16.1.21"}
}

7 changes: 7 additions & 0 deletions Chapter2/iosv-1_output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
terminal length 0
iosv-1#config t
Enter configuration commands, one per line. End with CNTL/Z.
iosv-1(config)#logging buffered 30000
iosv-1(config)#end
iosv-1#copy run start
Destination filename [startup-config]?
7 changes: 7 additions & 0 deletions Chapter2/iosv-2_output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
terminal length 0
iosv-2#config t
Enter configuration commands, one per line. End with CNTL/Z.
iosv-2(config)#logging buffered 30000
iosv-2(config)#end
iosv-2#copy run start
Destination filename [startup-config]?
12 changes: 12 additions & 0 deletions Chapter3/Arista/eapi_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/python2

from __future__ import print_function
from jsonrpclib import Server
import ssl

ssl._create_default_https_context = ssl._create_unverified_context

switch = Server("https://admin:arista@192.168.199.158/command-api")

response = switch.runCmds( 1, [ "show version" ] )
print('Serial Number: ' + response[0]['serialNumber'])
22 changes: 22 additions & 0 deletions Chapter3/Arista/eapi_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/python2

from __future__ import print_function
from jsonrpclib import Server
import ssl, pprint

ssl._create_default_https_context = ssl._create_unverified_context

# Run Arista commands thru eAPI
def runAristaCommands(switch_object, list_of_commands):
response = switch_object.runCmds(1, list_of_commands)
return response


switch = Server("https://admin:arista@192.168.199.158/command-api")

commands = ["enable", "configure", "interface ethernet 1/3", "switchport access vlan 100", "end", "write memory"]

response = runAristaCommands(switch, commands)
pprint.pprint(response)


Loading

0 comments on commit 8063477

Please sign in to comment.