-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwhoareyou.py
executable file
·40 lines (36 loc) · 1.28 KB
/
whoareyou.py
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
#!/usr/bin/python
# Find out who is running this script over SSH, by:
# 1. Asking ssh-agent for their known key fingerprints
# 2. Looking for a matching key in .ssh/authorized_keys, and reporting the key
# comment.
import binascii
import os
import warnings
warnings.filterwarnings('ignore', '[Tt]he (md5|popen2) module is deprecated', DeprecationWarning)
import md5
import popen2
def determine_identity():
agent_fingerprints = []
ssh_add = popen2.Popen4(['ssh-add', '-l'])
for line in ssh_add.fromchild.readlines():
try:
keylength, fingerprint, remainder = line.split(' ', 2)
except ValueError:
pass
agent_fingerprints.append(fingerprint.replace(':', ''))
try:
for line in open(os.path.expanduser('~/.ssh/authorized_keys')):
try:
keytype, blob, comment = line.strip().split(' ', 2)
fingerprint = md5.md5(binascii.a2b_base64(blob)).hexdigest()
except (ValueError, binascii.Error):
continue
if fingerprint in agent_fingerprints:
# We exit at the first match
return comment
except IOError:
# no file, we ignore
pass
return "<unknown>"
if __name__ == '__main__':
print determine_identity()