Skip to content

Commit 9c7f981

Browse files
author
Alexey Bogoslovskyi
authored
Merge pull request #58 from QualiSystems/3.1.210
3.1.0
2 parents ce2f712 + e233ed1 commit 9c7f981

16 files changed

Lines changed: 880 additions & 11 deletions

File tree

cloudshell/cli/command_template/command_template_executor.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ class CommandTemplateExecutor(object):
77
Execute command template using cli service
88
"""
99

10-
def __init__(self, cli_service, command_template, action_map=OrderedDict(), error_map=OrderedDict(),
11-
**optional_kwargs):
10+
def __init__(self, cli_service, command_template, action_map=None, error_map=None, **optional_kwargs):
1211
"""
1312
:param cli_service:
1413
:type cli_service: CliService
@@ -18,8 +17,8 @@ def __init__(self, cli_service, command_template, action_map=OrderedDict(), erro
1817
"""
1918
self._cli_service = cli_service
2019
self._command_template = command_template
21-
self._action_map = action_map
22-
self._error_map = error_map
20+
self._action_map = action_map or OrderedDict()
21+
self._error_map = error_map or OrderedDict()
2322
self._optional_kwargs = optional_kwargs
2423

2524
@property

cloudshell/cli/examples/__init__.py

Whitespace-only changes.

cloudshell/cli/examples/cli_demo/__init__.py

Whitespace-only changes.

cloudshell/cli/examples/cli_demo/connect_to_AWS/__init__.py

Whitespace-only changes.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
from cloudshell.cli.cli import CLI
2+
from cloudshell.cli.command_mode import CommandMode
3+
from cloudshell.cli.command_mode_helper import CommandModeHelper
4+
from cloudshell.cli.session.ssh_session import SSHSession
5+
from cloudshell.cli.session_pool_manager import SessionPoolManager
6+
from cloudshell.core.logger.qs_logger import get_qs_logger
7+
import paramiko
8+
import StringIO
9+
10+
11+
class CliCommandMode(CommandMode):
12+
PROMPT = r'$'
13+
ENTER_COMMAND = ''
14+
EXIT_COMMAND = 'exit'
15+
16+
def __init__(self,context):
17+
18+
CommandMode.__init__(self, CliCommandMode.PROMPT, CliCommandMode.ENTER_COMMAND,
19+
CliCommandMode.EXIT_COMMAND)
20+
21+
22+
LOGGER = get_qs_logger()
23+
24+
CommandMode.RELATIONS_DICT = {
25+
CliCommandMode: {}
26+
}
27+
if __name__ == '__main__':
28+
pool = SessionPoolManager(max_pool_size=1)
29+
cli = CLI(session_pool=pool)
30+
31+
context = type('context', (object,), {'resource': type('resource', (object,), {'name': 'test name'})})
32+
33+
host = '<AWS-IP>'
34+
35+
pem_file = open('mykey.pem', 'r')
36+
key_str = pem_file.read()
37+
keyfile = StringIO.StringIO(key_str)
38+
mykey = paramiko.RSAKey.from_private_key(keyfile)
39+
40+
modes = CommandModeHelper.create_command_mode(context)
41+
default_mode = modes[CliCommandMode]
42+
43+
session = SSHSession(host, username='<username>', password='', pkey=mykey)
44+
with cli.get_session(session, default_mode) as default_session:
45+
out = default_session.send_command('echo Cli Demo connected to AWS Machine')
46+
print(out)
47+
48+
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import re
2+
from cloudshell.cli.command_mode import CommandMode
3+
from cloudshell.cli.command_mode_helper import CommandModeHelper
4+
from cloudshell.networking.cli_handler_impl import CliHandlerImpl
5+
from cloudshell.shell.core.context_utils import get_attribute_by_name
6+
7+
class EnableCommandMode(CommandMode):
8+
PROMPT = r'(?:(?!\)).)#\s*$'
9+
ENTER_COMMAND = 'enable'
10+
EXIT_COMMAND = ''
11+
12+
def __init__(self, context):
13+
"""
14+
Initialize Enable command mode - default command mode for Cisco Shells
15+
16+
:param context:
17+
"""
18+
self._context = context
19+
20+
CommandMode.__init__(self, EnableCommandMode.PROMPT, EnableCommandMode.ENTER_COMMAND,
21+
EnableCommandMode.EXIT_COMMAND)
22+
23+
class DefaultCommandMode(CommandMode):
24+
PROMPT = r'>\s*$'
25+
ENTER_COMMAND = ''
26+
EXIT_COMMAND = ''
27+
28+
def __init__(self, context):
29+
"""
30+
Initialize Default command mode, only for cases when session started not in enable mode
31+
32+
:param context:
33+
"""
34+
self._context = context
35+
CommandMode.__init__(self, DefaultCommandMode.PROMPT, DefaultCommandMode.ENTER_COMMAND,
36+
DefaultCommandMode.EXIT_COMMAND)
37+
38+
class ConfigCommandMode(CommandMode):
39+
PROMPT = r'\(config.*\)#\s*$'
40+
ENTER_COMMAND = 'configure terminal'
41+
EXIT_COMMAND = 'exit'
42+
43+
def __init__(self, context):
44+
"""
45+
Initialize Config command mode
46+
47+
:param context:
48+
"""
49+
exit_action_map = {
50+
self.PROMPT: lambda session, logger: session.send_line('exit', logger)}
51+
CommandMode.__init__(self, ConfigCommandMode.PROMPT,
52+
ConfigCommandMode.ENTER_COMMAND,
53+
ConfigCommandMode.EXIT_COMMAND,
54+
exit_action_map=exit_action_map)
55+
56+
57+
CommandMode.RELATIONS_DICT = {
58+
DefaultCommandMode: {
59+
EnableCommandMode: {
60+
ConfigCommandMode: {}
61+
}
62+
}}
63+
64+
class SwitchCliHandler(CliHandlerImpl):
65+
def __init__(self, cli, context, logger):
66+
super(SwitchCliHandler, self).__init__(cli, context, logger,None)
67+
modes = CommandModeHelper.create_command_mode(context)
68+
self.default_mode = modes[DefaultCommandMode]
69+
self.enable_mode = modes[EnableCommandMode]
70+
self.config_mode = modes[ConfigCommandMode]
71+
72+
73+
74+
@property
75+
def username(self):
76+
return get_attribute_by_name('User', self._context)
77+
78+
@property
79+
def password(self):
80+
password = get_attribute_by_name(attribute_name='Password', context=self._context)
81+
return password
82+
83+
84+
def on_session_start(self, session, logger):
85+
"""Send default commands to configure/clear session outputs
86+
:return:
87+
"""
88+
89+
self.enter_enable_mode(session=session, logger=logger)
90+
session.hardware_expect('terminal length 0', EnableCommandMode.PROMPT, logger)
91+
session.hardware_expect('terminal width 300', EnableCommandMode.PROMPT, logger)
92+
self._enter_config_mode(session, logger)
93+
session.hardware_expect('no logging console', ConfigCommandMode.PROMPT, logger)
94+
session.hardware_expect('exit', EnableCommandMode.PROMPT, logger)
95+
96+
97+
def enter_enable_mode(self, session, logger):
98+
99+
enable_password = get_attribute_by_name(attribute_name='Enable Password',
100+
context=self._context)
101+
expect_map = {'[Pp]assword': lambda session, logger: session.send_line(enable_password, logger)}
102+
session.hardware_expect('enable', EnableCommandMode.PROMPT, action_map=expect_map, logger=logger)
103+
result = session.hardware_expect('', '{0}|{1}'.format(DefaultCommandMode.PROMPT, EnableCommandMode.PROMPT),
104+
logger)
105+
if not re.search(EnableCommandMode.PROMPT, result):
106+
raise Exception('enter_enable_mode', 'Enable password is incorrect')
107+
108+
109+
def _enter_config_mode(self, session, logger):
110+
error_message = 'Failed to enter config mode, please check logs, for details'
111+
output = session.hardware_expect(ConfigCommandMode.ENTER_COMMAND,
112+
'{0}|{1}'.format(ConfigCommandMode.PROMPT, EnableCommandMode.PROMPT), logger)
113+
if not re.search(ConfigCommandMode.PROMPT, output):
114+
raise Exception('_enter_config_mode', error_message)

cloudshell/cli/examples/cli_demo/connect_to_switch/__init__.py

Whitespace-only changes.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from cloudshell.cli.cli import CLI
2+
from cloudshell.cli.session_pool_manager import SessionPoolManager
3+
from cloudshell.core.logger.qs_logger import get_qs_logger
4+
from connect_to_switch.SwitchClihandler import SwitchCliHandler
5+
from cloudshell.shell.core.context import ResourceCommandContext, ResourceContextDetails, ReservationContextDetails,ConnectivityContext
6+
7+
logger = get_qs_logger()
8+
9+
def create_context():
10+
context = ResourceCommandContext()
11+
context.resource = ResourceContextDetails()
12+
context.resource.name = 'Switch for Demo'
13+
context.resource.address = '<IP>'
14+
context.resource.attributes = {}
15+
context.resource.attributes['CLI Connection Type'] = 'SSH'
16+
context.resource.attributes['User'] = '<username>'
17+
context.resource.attributes['AdminUser'] = 'admin'
18+
context.resource.attributes['Console Password'] = '<password>'
19+
context.resource.attributes['Password'] = '<password>'
20+
context.resource.attributes['Enable Password'] = '<password>'
21+
context.resource.attributes['Sessions Concurrency Limit'] = 2
22+
23+
return context
24+
25+
if __name__ == '__main__':
26+
context = create_context()
27+
28+
pool = SessionPoolManager(max_pool_size=1)
29+
cli = CLI(session_pool=pool)
30+
cli_handler = SwitchCliHandler(cli, context, logger)
31+
32+
with cli_handler.get_cli_service(cli_handler.enable_mode) as session:
33+
out = session.send_command('echo checking switch')
34+
with session.enter_mode(cli_handler.config_mode) as config_session:
35+
out = config_session.send_command('echo checking switch')
36+
print (out)
37+
out = config_session.send_command('echo checking switch')
38+
print (out)
39+
40+
41+
42+

cloudshell/cli/examples/cli_demo/connect_to_ubuntu/__init__.py

Whitespace-only changes.
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
The club isn't the best place to find a lover
2+
So the bar is where I go
3+
Me and my friends at the table doing shots
4+
Drinking fast and then we talk slow
5+
Come over and start up a conversation with just me
6+
And trust me I'll give it a chance now
7+
Take my hand, stop, put Van the Man on the jukebox
8+
And then we start to dance, and now I'm singing like
9+
10+
Girl, you know I want your love
11+
Your love was handmade for somebody like me
12+
Come on now, follow my lead
13+
I may be crazy, don't mind me
14+
Say, boy, let's not talk too much
15+
Grab on my waist and put that body on me
16+
Come on now, follow my lead
17+
Come, come on now, follow my lead
18+
I'm in love with the shape of you
19+
We push and pull like a magnet do
20+
Although my heart is falling too
21+
I'm in love with your body
22+
And last night you were in my room
23+
And now my bedsheets smell like you
24+
Every day discovering something brand new
25+
I'm in love with your body
26+
Oh�I�oh�I�oh�I�oh�I
27+
I'm in love with your body
28+
Oh�I�oh�I�oh�I�oh�I
29+
I'm in love with your body
30+
Oh�I�oh�I�oh�I�oh�I
31+
I'm in love with your body
32+
Every day discovering something brand new
33+
I'm in love with the shape of you
34+
35+
One week in we let the story begin
36+
We're going out on our first date
37+
You and me are thrifty, so go all you can eat
38+
Fill up your bag and I fill up a plate
39+
We talk for hours and hours about the sweet and the sour
40+
And how your family is doing okay
41+
Leave and get in a taxi, then kiss in the backseat
42+
Tell the driver make the radio play, and I'm singing like
43+
44+
Girl, you know I want your love
45+
Your love was handmade for somebody like me
46+
Come on now, follow my lead
47+
I may be crazy, don't mind me
48+
Say, boy, let's not talk too much
49+
Grab on my waist and put that body on me
50+
Come on now, follow my lead
51+
Come, come on now, follow my lead
52+
53+
I'm in love with the shape of you
54+
We push and pull like a magnet do
55+
Although my heart is falling too
56+
I'm in love with your body
57+
And last night you were in my room
58+
And now my bedsheets smell like you
59+
Every day discovering something brand new
60+
I'm in love with your body
61+
Oh�I�oh�I�oh�I�oh�I
62+
I'm in love with your body
63+
Oh�I�oh�I�oh�I�oh�I
64+
I'm in love with your body
65+
Oh�I�oh�I�oh�I�oh�I
66+
I'm in love with your body
67+
Every day discovering something brand new
68+
I'm in love with the shape of you
69+
70+
Come on, be my baby, come on
71+
Come on, be my baby, come on
72+
Come on, be my baby, come on
73+
Come on, be my baby, come on
74+
Come on, be my baby, come on
75+
Come on, be my baby, come on
76+
Come on, be my baby, come on
77+
Come on, be my baby, come on
78+
79+
I'm in love with the shape of you
80+
We push and pull like a magnet do
81+
Although my heart is falling too
82+
I'm in love with your body
83+
Last night you were in my room
84+
And now my bedsheets smell like you
85+
Every day discovering something brand new
86+
I'm in love with your body
87+
Come on, be my baby, come on
88+
Come on, be my baby, come on
89+
I'm in love with your body
90+
Come on, be my baby, come on
91+
Come on, be my baby, come on
92+
I'm in love with your body
93+
Come on, be my baby, come on
94+
Come on, be my baby, come on
95+
I'm in love with your body
96+
Every day discovering something brand new
97+
I'm in love with the shape of you
98+
99+
100+
101+
Read more: Ed Sheeran - Shape Of You Lyrics | MetroLyrics

0 commit comments

Comments
 (0)