Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fa7e347

Browse files
committed
Add task for listing connected users
This task can be used like this: ``` fab production all vm.connected_users ``` If there are users connected: ``` There are users connected to machine-1.vdc: - a_user - another_user ``` This is useful if you want to switch off a machine like the jumpbox that everyone is connected through.
1 parent c5311c8 commit fa7e347

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

vm.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,20 @@ def host_key(hostname):
122122
else:
123123
with hide('running'):
124124
run("ssh-keygen -l -f /dev/stdin <<< '{0}'".format(ssh_key))
125+
126+
@task
127+
@parallel(pool_size=5)
128+
def connected_users():
129+
"""
130+
Output a list of users who are logged in to a machine
131+
"""
132+
133+
with hide('running', 'stdout'):
134+
username = run('whoami')
135+
users = run('who | grep -v {0} || true'.format(username))
136+
137+
if users:
138+
print 'There are users connected to {0}:'.format(env.host_string)
139+
for user in users.split("\n"):
140+
print ' - {0}'.format(user.split(' ')[0])
141+
print "\n"

0 commit comments

Comments
 (0)