1
1
from __future__ import unicode_literals
2
+ import time
3
+ import re
2
4
from netmiko .cisco_base_connection import CiscoSSHConnection
5
+ from netmiko .cisco_base_connection import CiscoFileTransfer
3
6
from netmiko import log
4
7
5
8
@@ -10,6 +13,9 @@ def session_preparation(self):
10
13
self .set_base_prompt ()
11
14
self .disable_paging ()
12
15
self .set_terminal_width (command = 'terminal width 511' )
16
+ # Clear the read buffer
17
+ time .sleep (.3 * self .global_delay_factor )
18
+ self .clear_buffer ()
13
19
14
20
def check_config_mode (self , check_string = ')#' , pattern = '' ):
15
21
"""
@@ -21,10 +27,102 @@ def check_config_mode(self, check_string=')#', pattern=''):
21
27
Can also be (s2)
22
28
"""
23
29
log .debug ("pattern: {0}" .format (pattern ))
24
- self .write_channel (' \n ' )
30
+ self .write_channel (self . RETURN )
25
31
output = self .read_until_pattern (pattern = pattern )
26
32
log .debug ("check_config_mode: {0}" .format (repr (output )))
27
33
output = output .replace ("(s1)" , "" )
28
34
output = output .replace ("(s2)" , "" )
29
35
log .debug ("check_config_mode: {0}" .format (repr (output )))
30
36
return check_string in output
37
+
38
+
39
+ class AristaFileTransfer (CiscoFileTransfer ):
40
+ """Arista SCP File Transfer driver."""
41
+ def __init__ (self , ssh_conn , source_file , dest_file , file_system = None , direction = 'put' ):
42
+ msg = "Arista SCP Driver is under development and not fully implemented"
43
+ raise NotImplementedError (msg )
44
+ self .ssh_ctl_chan = ssh_conn
45
+ self .source_file = source_file
46
+ self .dest_file = dest_file
47
+ self .direction = direction
48
+
49
+ if file_system :
50
+ self .file_system = file_system
51
+ else :
52
+ raise ValueError ("Destination file system must be specified for Arista" )
53
+
54
+ # if direction == 'put':
55
+ # self.source_md5 = self.file_md5(source_file)
56
+ # self.file_size = os.stat(source_file).st_size
57
+ # elif direction == 'get':
58
+ # self.source_md5 = self.remote_md5(remote_file=source_file)
59
+ # self.file_size = self.remote_file_size(remote_file=source_file)
60
+ # else:
61
+ # raise ValueError("Invalid direction specified")
62
+
63
+ def put_file (self ):
64
+ """SCP copy the file from the local system to the remote device."""
65
+ destination = "{}/{}" .format (self .file_system , self .dest_file )
66
+ self .scp_conn .scp_transfer_file (self .source_file , destination )
67
+ # Must close the SCP connection to get the file written (flush)
68
+ self .scp_conn .close ()
69
+
70
+ def remote_space_available (self , search_pattern = r"(\d+) bytes free" ):
71
+ """Return space available on remote device."""
72
+ return super (AristaFileTransfer , self ).remote_space_available (
73
+ search_pattern = search_pattern
74
+ )
75
+
76
+ def verify_space_available (self , search_pattern = r"(\d+) bytes free" ):
77
+ """Verify sufficient space is available on destination file system (return boolean)."""
78
+ return super (AristaFileTransfer , self ).verify_space_available (
79
+ search_pattern = search_pattern
80
+ )
81
+
82
+ def check_file_exists (self , remote_cmd = "" ):
83
+ """Check if the dest_file already exists on the file system (return boolean)."""
84
+ raise NotImplementedError
85
+
86
+ def remote_file_size (self , remote_cmd = "" , remote_file = None ):
87
+ """Get the file size of the remote file."""
88
+ if remote_file is None :
89
+ if self .direction == 'put' :
90
+ remote_file = self .dest_file
91
+ elif self .direction == 'get' :
92
+ remote_file = self .source_file
93
+
94
+ if not remote_cmd :
95
+ remote_cmd = "dir {}/{}" .format (self .file_system , remote_file )
96
+
97
+ remote_out = self .ssh_ctl_chan .send_command (remote_cmd )
98
+ # Match line containing file name
99
+ escape_file_name = re .escape (remote_file )
100
+ pattern = r".*({}).*" .format (escape_file_name )
101
+ match = re .search (pattern , remote_out )
102
+ if match :
103
+ file_size = match .group (0 )
104
+ file_size = file_size .split ()[0 ]
105
+
106
+ if 'No such file or directory' in remote_out :
107
+ raise IOError ("Unable to find file on remote system" )
108
+ else :
109
+ return int (file_size )
110
+
111
+ @staticmethod
112
+ def process_md5 (md5_output , pattern = r"= (.*)" ):
113
+ raise NotImplementedError
114
+
115
+ def remote_md5 (self , base_cmd = 'show file' , remote_file = None ):
116
+ if remote_file is None :
117
+ if self .direction == 'put' :
118
+ remote_file = self .dest_file
119
+ elif self .direction == 'get' :
120
+ remote_file = self .source_file
121
+ remote_md5_cmd = "{} {}{} md5sum" .format (base_cmd , self .file_system , remote_file )
122
+ return self .ssh_ctl_chan .send_command (remote_md5_cmd , delay_factor = 3.0 )
123
+
124
+ def enable_scp (self , cmd = None ):
125
+ raise NotImplementedError
126
+
127
+ def disable_scp (self , cmd = None ):
128
+ raise NotImplementedError
0 commit comments