get_ttys.py 584 B

1234567891011121314151617181920212223
  1. """
  2. ls /dev/pts/*
  3. ps -t pts/8
  4. ps aux | ag 6996
  5. fcntl.ioctl(fd, request, arg=0, mutate_flag=True)
  6. ioctl (fd, TIOCSTI, nl);
  7. """
  8. import os
  9. import glob
  10. pts = glob.glob('/dev/pts/*')
  11. pids = [pid for pid in os.listdir('/proc') if pid.isdigit()]
  12. for pid in pids:
  13. name = os.path.join('/proc', pid, 'fd', '0')
  14. if os.path.islink(name):
  15. link = os.readlink(name)
  16. if link.startswith('/dev/pts') and not link.endswith('(deleted)'):
  17. print(pid, link)
  18. print(os.getpgid(int(pid)))
  19. print(open(os.path.join('/proc', pid, 'cmdline')).read())