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

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

 

"""RancidCmd.""" 

 

from subprocess import Popen 

from subprocess import PIPE 

import shlex 

import os 

import stat 

 

 

class RancidCmd(object): 

 

    """The :class:`RancidCmd <RancidCmd>` object. 

 

    RancidCmd 

 

    """ 

 

    def __init__(self, **kwargs): 

        """Constructor Parameters. 

 

        login, user, password, address, [enable_password], [timeout]. 

        """ 

        self.login = kwargs['login'] 

        self.user = kwargs['user'] 

        self.password = kwargs['password'] 

        self.address = kwargs['address'] 

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

        self.timeout = kwargs.get('timeout', 10) 

        self.encoding = 'utf-8' 

        RancidCmd.check_cloginrc() 

 

    def generate_cmd(self, command): 

        """Make login command.""" 

        if self.enable_password: 

            return '%s -t %s -u "%s" -p "%s" -e "%s" -c "%s" %s' % ( 

                self.login, self.timeout, self.user, 

                self.password, self.enable_password, command, self.address) 

        return '%s -t %s -u "%s" -p "%s" -c "%s" %s' % ( 

            self.login, self.timeout, self.user, 

            self.password, command, self.address) 

 

    def cmd_token(self, command): 

        """Split one line command.""" 

        return shlex.split(command) 

 

    def decode_bytes(self, byte_data): 

        """Change string with encoding setting.""" 

        return byte_data.decode(self.encoding) 

 

    def cmd_exec(self, command): 

        """Login and command execution.""" 

        proc = Popen(command, 

                     shell=True, 

                     stdout=PIPE, 

                     stderr=PIPE) 

        std_out, std_err = proc.communicate() 

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

                'std_err': self.decode_bytes(std_err)} 

 

    def execute(self, command): 

        """Command execution.""" 

        cmd = self.generate_cmd(command) 

        return self.cmd_exec(cmd) 

 

    @staticmethod 

    def touch(path): 

        """Make empty file.""" 

        try: 

            with open(path, 'a'): 

                os.utime(path, None) 

                os.chmod(path, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR) 

        except: 

            print('[error] Could not write "%s".' % path) 

            raise 

 

    @staticmethod 

    def check_cloginrc(name='.cloginrc'): 

        """Check rancid settings file (default: .cloginrc).""" 

        home = os.environ['HOME'] 

        path = '%s/%s' % (home, name) 

        if not os.path.isfile(path): 

            RancidCmd.touch(path) 

        return path