@@ -57,3 +57,64 @@ def get_commands(command, directories=None):
57
57
commands .append (cmd )
58
58
59
59
return commands
60
+
61
+ def get_systemd_service_active (command , service ):
62
+ """Returns True if service is active, False in all other cases
63
+ Args:
64
+ command (CommandProtocol): An instance of a Driver implementing the CommandProtocol
65
+ service (str): name of the service
66
+ Returns:
67
+ bool: True if service is active, False otherwise
68
+ """
69
+ assert isinstance (command , CommandProtocol ), "command must be a CommandProtocol"
70
+ _ , _ , exitcode = command .run (
71
+ "systemctl --quiet is-active {}" .format (service )
72
+ )
73
+ return exitcode == 0
74
+
75
+ def get_interface_ip (command , interface = "eth0" ):
76
+ import re
77
+ """Returns the global valid IPv4 address of the supplied interface
78
+ Args:
79
+ command (CommandProtocol): An instance of a Driver implementing the CommandProtocol
80
+ interface (string): name of the interface
81
+ Returns:
82
+ str: IPv4 address of the interface, None otherwise
83
+ """
84
+ try :
85
+ ip_string = command .run_check ("ip -o -4 addr show" )
86
+ except ExecutionError :
87
+ self .logger .debug ('No ip address found' )
88
+ return None
89
+
90
+ regex = re .compile (
91
+ r"""\d+: # Match the leading number
92
+ \s+(?P<if>\w+) # Match whitespace and interfacename
93
+ \s+inet\s+(?P<ip>[\d.]+) # Match IP Adress
94
+ /(?P<prefix>\d+) # Match prefix
95
+ .*global # Match global scope, not host scope""" , re .X
96
+ )
97
+ result = {}
98
+
99
+ for line in ip_string :
100
+ match = regex .match (line )
101
+ if match :
102
+ match = match .groupdict ()
103
+ result [match ['if' ]] = match ['ip' ]
104
+ if result :
105
+ return result [interface ]
106
+
107
+ return None
108
+
109
+ def get_hostname (command ):
110
+ """Returns the hostname
111
+ Args:
112
+ command (CommandProtocol): An instance of a Driver implementing the CommandProtocol
113
+ Returns:
114
+ str: hostname of the target, None otherwise
115
+ """
116
+ try :
117
+ hostname_string = command .run_check ("hostname" )
118
+ except ExecutionError :
119
+ return None
120
+ return hostname_string [0 ]
0 commit comments