Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

# -*- coding: utf-8 -*- 

 

"""RancidCmd.""" 

 

from subprocess import Popen 

from subprocess import PIPE 

from os.path import expanduser 

import os 

import re 

import stat 

import tempfile 

 

 

class RancidCmd(object): 

 

    """Class RancidCmd. 

 

    Attributes: 

 

        :login (str): RANCID login command(clogin, jlogin, etc). 

        :user (str): Login username. 

        :password (str): Login password. 

        :address (str): Host name or ip address. 

        :enable_password (str, optional): Enable password for clogin. 

            Default is None. 

        :option(int, optional): Option example: '-d -x "commands.txt"'. 

            Default is None. 

        :encoding(str, optional): Encoding type. 

            Default is 'utf-8'. 

 

    Using example: 

 

        * Please see README. 

            https://github.com/mtoshi/rancidcmd/blob/master/README.rst 

 

        * Sample code. 

            https://github.com/mtoshi/rancidcmd/blob/master/samples/sample.py 

 

    """ 

 

    def __init__(self, **kwargs): 

        """Constructor. 

 

        Args: 

 

            :login (str): RANCID login command(clogin, jlogin, etc). 

            :user (str): Login username. 

            :password (str): Login password. 

            :address (str): Host name or ip address. 

            :enable_password (str, optional): Enable password for clogin. 

                Default is None. 

            :option(int, optional): Option example: '-d -x "commands.txt"'. 

                Default is None. 

            :encoding(str, optional): Encoding type. 

                Default is 'utf-8'. 

 

        """ 

        self.login = kwargs['login'] 

        self.address = kwargs['address'] 

        self.encoding = 'utf-8' 

        self.default_native_cloginrc_path = RancidCmd.make_default_native_cloginrc_path()  # NOQA 

        self.native_cloginrc_path = kwargs.get( 

            u'native_cloginrc_path', self.default_native_cloginrc_path) 

        native_cloginrc = self.check_native_cloginrc() 

 

        if native_cloginrc is None: 

            self.user = kwargs['user'] 

            self.password = kwargs['password'] 

            self.port = kwargs.get('port', 23) 

            self.method = kwargs.get('method', u'telnet') 

            self.enable_password = kwargs.get('enable_password', None) 

            self.option = kwargs.get('option', None) 

        self.cloginrc = self.make_cloginrc(native_cloginrc) 

 

    def is_option_x(self): 

        """Check -x option. 

 

        "-c" gets commands from command-line. 

        "-x" gets commnads from file. 

 

        These are for command option and exclusive. 

        If "-x" option is specified, then "-c" command is ignored. 

        """ 

        if hasattr(self, u'option'): 

            if self.option: 

                pat = re.compile(r'(\s+)?-x\s+') 

                if pat.search(self.option): 

                    return True 

        return False 

 

    def generate_cmd(self, command): 

        """Generate command. 

 

        Args: 

 

            :command (str): Example is "show version". 

 

        Returns: 

 

            :str: Return the command string. 

 

            If there is the "enable_password". :: 

 

                'xlogin -u admin -c "show version"' 

 

        """ 

        if self.is_option_x(): 

            command = '' 

        else: 

            command = '-c "%s"' % command 

 

        res = [] 

        if hasattr(self, "login"): 

            res.append(self.login) 

        if hasattr(self, u'option'): 

            if self.option: 

                res.append(self.option) 

        if command: 

            res.append(command) 

        if hasattr(self, "cloginrc"): 

            res.append('-f') 

            res.append(self.cloginrc.name) 

        if hasattr(self, "address"): 

            res.append(self.address) 

 

        return u' '.join(res) 

 

    def decode_bytes(self, byte_data): 

        """Change string with encoding setting. 

 

        Args: 

 

            :byte_data (bytes): Popen output. 

 

        """ 

        return byte_data.decode(self.encoding) 

 

    def cmd_exec(self, command): 

        """Login and command execution. 

 

        Args: 

 

            :command (str): Command for execution. 

 

        Returns: 

 

            :dict: Example is below. :: 

 

            { 

                'std_err': '', 

                'std_out': '', 

                'rtn_code': '', 

            } 

        """ 

        env = os.environ.copy() 

        env['HOME'] = RancidCmd.get_home_path() 

        proc = Popen(command, 

                     shell=True, 

                     env=env, 

                     stdout=PIPE, 

                     stderr=PIPE) 

        std_out, std_err = proc.communicate() 

        rtn_code = proc.returncode 

        return {'std_out': self.decode_bytes(std_out), 

                'std_err': self.decode_bytes(std_err), 

                'rtn_code': rtn_code} 

 

    def show_config(self): 

        """cloginrc string check. 

 

        Returns: 

 

            :str: Return cloginrc config string. 

        """ 

        print(self.cloginrc.read().decode('utf-8')) 

 

    def show_command(self, command): 

        """Execute command string check. 

 

        Args: 

 

            :command (str): Example is "show version". 

 

        Returns: 

 

            :str: Return the command string. 

        """ 

        print(self.generate_cmd(command)) 

 

    def execute(self, command): 

        """Command execution. 

 

        Args: 

 

            :command (str): Example is "show version". 

 

        Returns: 

 

            :dict: Example is below. :: 

 

            { 

                'std_err': '', 

                'std_out': '', 

                'rtn_code': '', 

            } 

 

        """ 

        cmd = self.generate_cmd(command) 

        res = self.cmd_exec(cmd) 

        self.cloginrc.close() 

        return res 

 

    @staticmethod 

    def get_home_path(): 

        """Get home directory path. 

        Returns: 

            :str: User home directory path. 

        """ 

        return expanduser("~") 

 

    def make_cloginrc(self, cloginrc_config): 

 

        """Check rancid settings file. 

 

        Note: 

 

            If RANCID settings file is not exists, 

            then make temporary settings file. 

 

        Returns: 

 

            :NamedTemporaryFile: RANCID settings tempfile object. 

 

        """ 

        temp = tempfile.NamedTemporaryFile() 

 

        if cloginrc_config is None: 

            user = self.user 

            host = self.address 

            port = self.port 

            passwd = self.password 

            epasswd = self.enable_password 

            method = self.method 

 

            add_user = u'add user {host} {user}'.format( 

                host=host, user=user) 

 

            add_method = u'add method {host} {{{method}:{port}}}'.format( 

                host=host, method=method, port=port) 

 

            if self.enable_password: 

                add_passwd = u'add password {host} {passwd} {epasswd}'.format( 

                    host=host, passwd=passwd, epasswd=epasswd) 

            else: 

                add_passwd = u'add password {host} {passwd}'.format( 

                    host=host, passwd=passwd) 

 

            cloginrc_config = '\n'.join([add_user, add_method, add_passwd]) 

 

        temp.write(cloginrc_config.encode('utf-8')) 

        temp.seek(0) 

 

        os.chmod(temp.name, stat.S_IRUSR) 

 

        return temp 

 

    def check_native_cloginrc(self): 

        """Check native settings file. 

        Note: 

            If RANCID settings file is not exists or empty, 

            then make temporary cloginrc file. 

            So this method is for check cloginrc file. 

 

        Returns: 

            :str: RANCID settings file text. 

        """ 

        path = self.native_cloginrc_path 

        if os.path.isfile(path): 

            if os.path.getsize(path) == 0: 

                return None 

            with open(path, u'r') as _file: 

                return _file.read() 

        return None 

 

    @staticmethod 

    def make_default_native_cloginrc_path(name='.cloginrc'): 

        """Make native settings file.""" 

        home = RancidCmd.get_home_path() 

        return os.path.join(home, name)