Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
CIRCLE
/
agent
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
7
Merge Requests
0
Wiki
Members
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
26201eeb
authored
Dec 03, 2013
by
Bach Dániel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
first commit
parents
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
199 additions
and
0 deletions
+199
-0
.gitignore
+2
-0
agent.py
+153
-0
utils.py
+44
-0
No files found.
.gitignore
0 → 100644
View file @
26201eeb
*.swp
*.pyc
agent.py
0 → 100644
View file @
26201eeb
#!/usr/bin/env python
from
twisted.internet
import
protocol
,
reactor
,
defer
from
twisted.internet.task
import
LoopingCall
from
twisted.internet.serialport
import
SerialPort
import
psutil
import
uptime
import
subprocess
#import netifaces
import
platform
from
datetime
import
datetime
from
utils
import
SerialLineReceiverBase
class
Context
(
object
):
@staticmethod
def
change_password
(
new_password
):
system
=
platform
.
system
()
if
system
==
'Linux'
:
proc
=
subprocess
.
Popen
([
'/usr/bin/sudo'
,
'/usr/sbin/chpasswd'
],
stdin
=
subprocess
.
PIPE
)
proc
.
communicate
(
'cloud:
%
s
\n
'
%
new_password
)
elif
system
==
'Windows'
:
from
win32com
import
adsi
ads_obj
=
adsi
.
ADsGetObject
(
'WinNT://localhost/
%
s,user'
%
'cloud'
)
ads_obj
.
Getinfo
()
ads_obj
.
SetPassword
(
new_password
)
@staticmethod
def
restart_networking
():
system
=
platform
.
system
()
if
system
==
'Linux'
:
interfaces
=
'''
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet dhcp
'''
proc
=
subprocess
.
Popen
([
'/usr/bin/sudo'
,
'/usr/bin/tee'
,
'/etc/network/interfaces'
],
stdin
=
subprocess
.
PIPE
)
proc
.
communicate
(
interfaces
)
subprocess
.
call
([
'/usr/bin/sudo'
,
'/etc/init.d/networking'
,
'restart'
])
elif
system
==
'Windows'
:
import
wmi
nic_configs
=
wmi
.
WMI
()
.
Win32_NetworkAdapterConfiguration
(
IPEnabled
=
True
)
nic
=
nic_configs
[
0
]
assert
nic
.
EnableDHCP
()[
0
]
==
0
@staticmethod
def
set_time
(
new_time
):
system
=
platform
.
system
()
if
system
==
'Linux'
:
subprocess
.
call
([
'/usr/bin/sudo'
,
'/etc/init.d/openntpd'
,
'restart'
])
elif
system
==
'Windows'
:
import
win32api
t
=
datetime
.
utcfromtimestamp
(
float
(
new_time
))
win32api
.
SetSystemTime
(
t
.
year
,
t
.
month
,
0
,
t
.
day
,
t
.
hour
,
t
.
minute
,
t
.
second
,
0
)
@staticmethod
def
set_hostname
(
new_hostname
):
system
=
platform
.
system
()
if
system
==
'Linux'
:
pass
elif
system
==
'Windows'
:
import
wmi
wmi
.
WMI
()
.
Win32_ComputerSystem
()[
0
]
.
Rename
(
new_hostname
)
class
SerialLineReceiver
(
SerialLineReceiverBase
):
def
connectionMade
(
self
):
self
.
send_command
(
command
=
'agent_started'
,
args
=
{})
def
shutdown
():
self
.
connectionLost2
(
'shutdown'
)
d
=
defer
.
Deferred
()
reactor
.
callLater
(
0.3
,
d
.
callback
,
"1"
)
return
d
reactor
.
addSystemEventTrigger
(
"before"
,
"shutdown"
,
shutdown
)
def
connectionLost2
(
self
,
reason
):
self
.
send_command
(
command
=
'agent_stopped'
,
args
=
{})
def
tick
(
self
):
self
.
send_status
()
def
__init__
(
self
):
super
(
SerialLineReceiver
,
self
)
.
__init__
()
self
.
lc
=
LoopingCall
(
self
.
tick
)
self
.
lc
.
start
(
5
,
now
=
False
)
def
send_status
(
self
):
args
=
{
"cpu"
:
dict
(
psutil
.
cpu_times
()
.
__dict__
),
"ram"
:
dict
(
psutil
.
virtual_memory
()
.
__dict__
),
"swap"
:
dict
(
psutil
.
swap_memory
()
.
__dict__
),
"uptime"
:
{
"seconds"
:
uptime
.
uptime
()},
"user"
:
{
"count"
:
len
(
psutil
.
get_users
())}}
self
.
send_response
(
response
=
'status'
,
args
=
args
)
def
send_ipaddresses
(
self
):
args
=
{}
interfaces
=
netifaces
.
interfaces
()
for
i
in
interfaces
:
if
i
==
'lo'
:
continue
args
[
i
]
=
[]
addresses
=
netifaces
.
ifaddresses
(
i
)
args
[
i
]
=
([
x
[
'addr'
]
for
x
in
addresses
.
get
(
netifaces
.
AF_INET
,
[])]
+
[
x
[
'addr'
]
for
x
in
addresses
.
get
(
netifaces
.
AF_INET6
,
[])
if
'
%
'
not
in
x
[
'addr'
]])
self
.
send_response
(
response
=
'ipaddresses'
,
args
=
args
)
def
handle_command
(
self
,
command
,
args
):
if
command
==
'ping'
:
self
.
send_response
(
response
=
'pong'
,
args
=
args
)
elif
command
==
'status'
:
self
.
send_status
()
elif
command
==
'get_ipaddresses'
:
self
.
send_ipaddresses
()
elif
command
==
'change_password'
:
Context
.
change_password
(
str
(
args
[
'password'
]))
elif
command
==
'restart_networking'
:
Context
.
restart_networking
()
elif
command
==
'set_time'
:
Context
.
set_time
(
str
(
args
[
'time'
]))
def
handle_response
(
self
,
response
,
args
):
pass
def
main
():
system
=
platform
.
system
()
if
system
==
'Windows'
:
port
=
r'\\.\COM1'
else
:
port
=
'/dev/ttyS0'
SerialPort
(
SerialLineReceiver
(),
port
,
reactor
,
baudrate
=
115200
)
reactor
.
run
()
if
__name__
==
'__main__'
:
main
()
utils.py
0 → 100644
View file @
26201eeb
from
twisted.protocols.basic
import
LineReceiver
import
json
import
logging
root_logger
=
logging
.
getLogger
()
root_logger
.
setLevel
(
logging
.
DEBUG
)
class
SerialLineReceiverBase
(
LineReceiver
,
object
):
delimiter
=
'
\r
'
def
send_response
(
self
,
response
,
args
):
self
.
transport
.
write
(
json
.
dumps
({
'response'
:
response
,
'args'
:
args
})
+
'
\r\n
'
)
def
send_command
(
self
,
command
,
args
):
self
.
transport
.
write
(
json
.
dumps
({
'command'
:
command
,
'args'
:
args
})
+
'
\r\n
'
)
def
handle_command
(
self
,
command
,
args
):
raise
NotImplementedError
(
"Subclass must implement abstract method"
)
def
handle_response
(
self
,
response
,
args
):
raise
NotImplementedError
(
"Subclass must implement abstract method"
)
def
lineReceived
(
self
,
data
):
try
:
data
=
json
.
loads
(
data
)
args
=
data
.
get
(
'args'
,
{})
if
not
isinstance
(
args
,
dict
):
args
=
{}
command
=
data
.
get
(
'command'
,
None
)
response
=
data
.
get
(
'response'
,
None
)
logging
.
debug
(
'[serial] valid json:
%
s'
%
(
data
,
))
except
(
ValueError
,
KeyError
)
as
e
:
logging
.
error
(
'[serial] invalid json:
%
s (
%
s)'
%
(
data
,
e
))
return
if
command
is
not
None
and
isinstance
(
command
,
unicode
):
logging
.
debug
(
'received command:
%
s (
%
s)'
%
(
command
,
args
))
self
.
handle_command
(
command
,
args
)
elif
response
is
not
None
and
isinstance
(
response
,
unicode
):
logging
.
debug
(
'received reply:
%
s (
%
s)'
%
(
response
,
args
))
self
.
handle_response
(
response
,
args
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment