def dump(ss,minwin,maxwin): for win,type,title in sc.gen_all_windows(minwin,maxwin,ss.pid): if type==0: type_string="basic" elif type==1: type_string="group" elif type==-1: type_string="zombie" else: type_string="unknown" print("%s %s"%(win,type_string)) print("%s %s"%(win,title)) filter=ss.get_exec(win) if filter!=-1: print("%s %s"%(win,filter)) tty=ss.tty(win) print("%s %s"%(win,tty)) if type==0: try: pids=sc.get_tty_pids(tty) except: print ("%s No access"%win) pass for pid in pids: try: print ("%s %s %s"%(win,pid,sc.get_pid_info(pid))) except: print ("%s No permission"%(win)) print("\n")
def kill_win_last_proc(session,win="-1",sig="TERM"): import signal,os ss=ScreenSaver(session,'/dev/null','/dev/null') ctty=ss.tty(win) pids=sc.get_tty_pids(ctty) pid = pids[len(pids)-1] sig=eval('signal.SIG'+sig) os.kill(int(pid),sig)
def kill_win_last_proc(session,win="-1",sig="TERM"): import signal,os,platform ss=ScreenSaver(session,'/dev/null','/dev/null') ctty=ss.tty(win) if platform.system() == 'FreeBSD': pids=sc.get_tty_pids(ctty) else: pids=sc._get_tty_pids_pgrep(ctty) pid = pids[-1] sig=eval('signal.SIG'+sig) os.kill(int(pid),sig)
def dump(ss,showpid=True,reverse=True,sort=False,groupids=[]): from sys import stdout bShow=True windows=[] if groupids: windows=subwindows(ss.pid,groupids)[1] for cwin,cgroupid,cgroup,ctty,ctype,ctypestr,ctitle,cfilter,cscroll,ctime in sc.gen_all_windows_full(ss.pid,reverse,sort): if groupids: if cwin in windows: bShow=True else: bShow=False if bShow: print("----------------------------------------") lines=[] lines.append("%s TYPE %s\n"%(cwin,ctypestr)) if cgroupid=='-1': groupstr='-1' else: groupstr=cgroupid+' '+cgroup lines.append("%s GRP %s\n"%(cwin,groupstr)) lines.append("%s TITL %s\n"%(cwin,ctitle)) if cfilter!='-1': lines.append("%s EXEC %s\n"%(cwin,cfilter)) if ctype==0: lines.append("%s TTY %s\n"%(cwin,ctty)) if showpid: try: pids=sc.get_tty_pids(ctty) except: lines.append ("%s No access\n"%cwin) pass for pid in pids: try: cwd,exe,cmd=sc.get_pid_info(pid) lines.append ("%s PID %s CWD %s\n"%(cwin,pid,cwd)) lines.append ("%s PID %s EXE %s\n"%(cwin,pid,exe)) cmd=cmd.split('\0')[:-1] lines.append ("%s PID %s CMD %s\n"%(cwin,pid,cmd)) try: if cmd[0].endswith('screen-session-primer') and cmd[1]=='-p': lines[0]=lines[0][:-1]+" / primer\n" elif cmd[0] in ('vi','vim','viless','vimdiff'): lines[0]=lines[0][:-1]+" / VIM\n" except: pass except: lines.append ("%s PID > %s < No permission\n"%(cwin,pid)) map(stdout.write,lines)
def get_win_last_proc(session, win="-1", ctty = None): import platform if not ctty: ss = ScreenSaver(session, '/dev/null', '/dev/null') ctty = ss.tty(win) if ctty is None or ctty == -1: stderr.write("Window does not exist (%s)\n" % win) return False if platform.system() == 'FreeBSD': pids = sc.get_tty_pids(ctty) else: pids = sc._get_tty_pids_pgrep(ctty) if len(pids) > 0: return pids[-1] else: ## No processes for this window. return None
def get_win_last_proc(session, win="-1", ctty=None): import platform if not ctty: ss = ScreenSaver(session, '/dev/null', '/dev/null') ctty = ss.tty(win) if ctty is None or ctty == -1: stderr.write("Window does not exist (%s)\n" % win) return False if platform.system() == 'FreeBSD': pids = sc.get_tty_pids(ctty) else: pids = sc._get_tty_pids_pgrep(ctty) if len(pids) > 0: return pids[-1] else: ## No processes for this window. return None
bPrint = False session = "%s" % sys.argv[1] out("session = " + session, bPrint) session_arg = "-S %s" % session cwin = sc.get_current_window(session) windows_old = sc.parse_windows(sc.get_windows(session))[0] out(str(windows_old), bPrint) f = os.popen("screen %s -Q @tty" % session_arg) ctty = f.readline() f.close() out(ctty, bPrint) pids = sc.get_tty_pids(ctty) # pids=sc.sort_by_ppid(pids) thepid = pids[len(pids) - 1] thedir = sc.get_pid_info(thepid)[0] out(thedir, bPrint) os.chdir(thedir) # command='screen %s -X screen' % (session_arg) command = "screen" if len(sys.argv) > 2: command += ' -t "%s"' % (" ".join(["%s" % v for v in sys.argv[2:]])) else: command += ' -t "%s"' % (thedir)
def __save_screen(self): errors = [] homewindow = self.homewindow group_wins = {} group_groups = {} excluded_wins = [] excluded_groups = [] scroll_wins = [] scroll_groups = [] cwin = -1 ctty = None cppids = {} rollback = (None, None, None) ctime = self.time() findir = sc.datadir os.symlink(os.path.join(findir), os.path.join(self.basedir, self.savedir)) #sc_cwd=self.command_at(True,'hardcopydir') # it only works interactively # should be modified to properly restore hardcopydir(:dumpscreen settings) self.command_at(False, 'eval \'hardcopydir \"%s\"\' \'at \"\#\" hardcopy -h\' \'hardcopydir \"%s\"\'' % (findir, self.homedir)) mru_w = [homewindow + '\n'] for ( cwin, cgroupid, cgroup, ctty, ctype, ctypestr, ctitle, cfilter, cscroll, badctime, cmdargs, ) in sc.gen_all_windows_full(self.pid, sc.datadir, False, False): mru_w.append("%s\n" % cwin) cpids = None cpids_data = None if ctypestr[0] == 'g': pass elif ctypestr[0] == 'z': cpids = [] else: # get sorted pids associated with the window try: cpids = sc.get_tty_pids(ctty) cpids_data = [] ncpids = [] for pid in cpids: try: pidinfo = sc.get_pid_info(pid) (exehead, exetail) = os.path.split(pidinfo[1]) if exetail in self.blacklist: blacklist = True else: blacklist = False cpids_data.append(pidinfo + tuple([blacklist])) ncpids.append(pid) except Exception,x: if cwin != homewindow: errors.append('%s PID %s: Unable to access ( %s )' % (cwin, pid, str(x))) cpids = ncpids except Exception,x: errors.append('%s Unable to access %s ( %s )' % (cwin, str(ctty), str(x))) cpids=[]
def __save_screen(self): errors = [] homewindow = self.homewindow group_wins = {} group_groups = {} excluded_wins = [] excluded_groups = [] scroll_wins = [] scroll_groups = [] cwin = -1 ctty = None cppids = {} rollback = (None, None, None) ctime = self.time() findir = sc.datadir os.symlink(os.path.join(findir), os.path.join(self.basedir, self.savedir)) # sc_cwd=self.command_at(True,'hardcopydir') # it only works interactively # should be modified to properly restore hardcopydir(:dumpscreen settings) self.command_at( False, "eval 'hardcopydir \"%s\"' 'at \"\#\" hardcopy -h' 'hardcopydir \"%s\"'" % (findir, self.homedir) ) mru_w = [homewindow + "\n"] for ( cwin, cgroupid, cgroup, ctty, ctype, ctypestr, ctitle, cfilter, cscroll, badctime, cmdargs, ) in sc.gen_all_windows_full(self.pid, sc.datadir, False, False): mru_w.append("%s\n" % cwin) cpids = None cpids_data = None if ctypestr[0] == "g": pass elif ctypestr[0] == "z": cpids = [] else: # get sorted pids associated with the window try: cpids = sc.get_tty_pids(ctty) cpids_data = [] ncpids = [] for pid in cpids: try: pidinfo = sc.get_pid_info(pid) (exehead, exetail) = os.path.split(pidinfo[1]) if exetail in self.blacklist: blacklist = True else: blacklist = False cpids_data.append(pidinfo + tuple([blacklist])) ncpids.append(pid) except Exception, x: if cwin != homewindow: errors.append("%s PID %s: Unable to access ( %s )" % (cwin, pid, str(x))) cpids = ncpids except Exception, x: errors.append("%s Unable to access %s ( %s )" % (cwin, str(ctty), str(x))) cpids = []
def dump(ss, datadir, showpid=True, reverse=True, sort=False, groupids=[]): from sys import stdout bShow = True windows = [] sum_process_total = 0 sum_win = 0 sum_zombie = 0 sum_basic = 0 sum_group = 0 sum_telnet = 0 sum_primer = 0 sum_vim = 0 if groupids: (groups, windows) = subwindows(ss.pid, datadir, groupids) try: for ( cwin, cgroupid, cgroup, ctty, ctype, ctypestr, ctitle, cfilter, cscroll, ctime, cmdargs, ) in sc.gen_all_windows_full(ss.pid, datadir, reverse, sort): if groupids: if cwin in windows: bShow = True else: bShow = False if bShow: sum_win += 1 if ctype == -1: sum_zombie += 1 elif ctype == 0: sum_basic += 1 elif ctype == 1: sum_group += 1 elif ctype == 2: sum_telnet += 1 print "----------------------------------------" lines = [] lines.append("%s TYPE %s\n" % (cwin, ctypestr)) if cgroupid == "-1": groupstr = "-1" else: groupstr = cgroupid + " " + cgroup lines.append("%s GRP %s\n" % (cwin, groupstr)) lines.append("%s TITL %s\n" % (cwin, ctitle)) cmdargs = cmdargs.split('\0') pcmdargs = cmdargs[0] if cmdargs[1] != '': pcmdargs += " " + (" ").join(["\"%s\"" % v for v in cmdargs[1:-1]]) lines.append("%s CARG %s\n" % (cwin, pcmdargs)) if cfilter != "-1": lines.append("%s EXEC %s\n" % (cwin, cfilter)) if ctype == 0: lines.append("%s TTY %s\n" % (cwin, ctty)) if showpid: try: pids = sc.get_tty_pids(ctty) for pid in pids: sum_process_total += 1 try: (cwd, exe, cmd) = sc.get_pid_info(pid) lines.append("%s PID %s CWD %s\n" % (cwin, pid, cwd)) lines.append("%s PID %s EXE %s\n" % (cwin, pid, exe)) cmd = cmd.split('\0') pcmd = cmd[0] if cmd[1] != '': pcmd += " "+" ".join(["\"%s\"" % v for v in cmd[1:-1]]) lines.append("%s PID %s CMD %s\n" % (cwin, pid, pcmd)) if cmd[0].endswith('screen-session-primer') and cmd[1] == '-p': sum_primer += 1 lines[0] = lines[0][:-1] + " / primer\n" elif cmd[0] in ('vi', 'vim', 'viless', 'vimdiff'): sum_vim += 1 lines[0] = lines[0][:-1] + " / VIM\n" except OSError,x: lines.append("%s PID %s Unable to access pid data ( %s )\n" % (cwin, pid, str(x))) except Exception,x: lines.append("%s Unable to access PIDs associated with tty ( %s )\n" % (cwin,str(x))) try: map(stdout.write, lines) except: break print 'WINDOWS: %d\t[ %d basic | %d group | %d zombie | %d telnet ]' % \ (sum_win, sum_basic, sum_group, sum_zombie, sum_telnet) print 'PROCESS: %d\t[ %d primer | %d vim ]' % (sum_process_total, sum_primer, sum_vim)
def dump(ss, datadir, showpid=True, reverse=True, sort=False, groupids=[]): from sys import stdout bShow = True windows = [] sum_process_total = 0 sum_win = 0 sum_zombie = 0 sum_basic = 0 sum_group = 0 sum_telnet = 0 sum_primer = 0 sum_vim = 0 if groupids: (groups, windows) = subwindows(ss.pid, datadir, groupids) try: for ( cwin, cgroupid, cgroup, ctty, ctype, ctypestr, ctitle, cfilter, cscroll, ctime, cmdargs, ) in sc.gen_all_windows_full(ss.pid, datadir, reverse, sort): if groupids: if cwin in windows: bShow = True else: bShow = False if bShow: sum_win += 1 if ctype == -1: sum_zombie += 1 elif ctype == 0: sum_basic += 1 elif ctype == 1: sum_group += 1 elif ctype == 2: sum_telnet += 1 print "----------------------------------------" lines = [] lines.append("%s TYPE %s\n" % (cwin, ctypestr)) if cgroupid == "-1": groupstr = "-1" else: groupstr = cgroupid + " " + cgroup lines.append("%s GRP %s\n" % (cwin, groupstr)) lines.append("%s TITL %s\n" % (cwin, ctitle)) cmdargs = cmdargs.split('\0') pcmdargs = cmdargs[0] if cmdargs[1] != '': pcmdargs += " " + (" ").join( ["\"%s\"" % v for v in cmdargs[1:-1]]) lines.append("%s CARG %s\n" % (cwin, pcmdargs)) if cfilter != "-1": lines.append("%s EXEC %s\n" % (cwin, cfilter)) if ctype == 0: lines.append("%s TTY %s\n" % (cwin, ctty)) if showpid: try: pids = sc.get_tty_pids(ctty) for pid in pids: sum_process_total += 1 try: (cwd, exe, cmd) = sc.get_pid_info(pid) lines.append("%s PID %s CWD %s\n" % (cwin, pid, cwd)) lines.append("%s PID %s EXE %s\n" % (cwin, pid, exe)) cmd = cmd.split('\0') pcmd = cmd[0] if cmd[1] != '': pcmd += " " + " ".join( ["\"%s\"" % v for v in cmd[1:-1]]) lines.append("%s PID %s CMD %s\n" % (cwin, pid, pcmd)) if cmd[0].endswith('screen-session-primer' ) and cmd[1] == '-p': sum_primer += 1 lines[ 0] = lines[0][:-1] + " / primer\n" elif cmd[0] in ('vi', 'vim', 'viless', 'vimdiff'): sum_vim += 1 lines[0] = lines[0][:-1] + " / VIM\n" except OSError, x: lines.append( "%s PID %s Unable to access pid data ( %s )\n" % (cwin, pid, str(x))) except Exception, x: lines.append( "%s Unable to access PIDs associated with tty ( %s )\n" % (cwin, str(x))) try: map(stdout.write, lines) except: break print 'WINDOWS: %d\t[ %d basic | %d group | %d zombie | %d telnet ]' % \ (sum_win, sum_basic, sum_group, sum_zombie, sum_telnet) print 'PROCESS: %d\t[ %d primer | %d vim ]' % (sum_process_total, sum_primer, sum_vim)
if bRenumber: cwin = sc.get_current_window(session) if tdir.startswith('/') or tdir.startswith('~'): thedir = os.path.expanduser(tdir) else: if sourcenumber == "-1": f = os.popen(SCREEN + ' %s -Q @tty' % session_arg) else: f = os.popen(SCREEN + ' %s -p %s -Q @tty' % (session_arg, sourcenumber)) ctty = f.readline() f.close() if ctty.startswith('/dev'): if platform.system() == 'FreeBSD': pids = sc.get_tty_pids(ctty) else: pids = sc._get_tty_pids_pgrep(ctty) try: p_i = [i for (i, x) in enumerate(pids) if x == ppid][0] - 1 thepid = pids[p_i] except: p_i = len(pids) - 1 thepid = pids[p_i] info = None while not info and p_i >= 0: try: info = sc.get_pid_info(thepid) except:
def __save_screen(self): errors=[] homewindow=self.homewindow group_wins={} group_groups={} excluded_wins=[] excluded_groups=[] scroll_wins=[] scroll_groups=[] cwin=-1 ctty=None cppids={} rollback=None,None,None ctime=self.time() findir=os.path.join(self.basedir,self.savedir) #sc_cwd=self.command_at(True,'hardcopydir') #print(sc_cwd) self.command_at(False, 'at \# dumpscreen window %s'%os.path.join(self.basedir,self.savedir,"winlist")) self.command_at(False, 'at \# dumpscreen window %s -F'%os.path.join(self.basedir,self.savedir)) self.command_at(False, 'hardcopydir %s'%os.path.join(self.basedir,self.savedir)) self.command_at(False, 'at \# hardcopy -h') self.command_at(False, 'hardcopydir \"%s\"'%self.homedir) # should be modified to properly restore hardcopydir(:dumpscreen settings) try: f=open(os.path.join(findir,"winlist"),'r') f.close() except: self.command_at(False, 'at \# dumpscreen window %s'%os.path.join(self.basedir,self.savedir,"winlist")) fmru = open(os.path.join(findir,"mru"),"w") for line in open(os.path.join(findir,"winlist"),'r'): try: id,cgroupid,ctty,ctitle = line.strip().split(' ',3) except: id,cgroupid,ctty= line.strip().split(' ') ctitle=None cwin=id fmru.write("%s "%cwin) if(ctty[0]=='z'): # zombie continue if(ctty[0]=="g"): # group ctype="group" cpids = None cpids_data=None if self.excluded: if cwin in self.excluded or ctitle in self.excluded: excluded_groups.append(cwin) try: group_groups[cgroupid]+=[cwin] except: group_groups[cgroupid]=[cwin] if self.scroll: if cwin in self.scroll or ctitle in self.scroll: scroll_groups.append(cwin) try: group_groups[cgroupid]+=[cwin] except: group_groups[cgroupid]=[cwin] else: if self.excluded: if cwin in self.excluded or ctitle in self.excluded: excluded_wins.append(cwin) else: try: group_wins[cgroupid]+=[cwin] except: group_wins[cgroupid]=[cwin] if self.scroll: if cwin in self.scroll or ctitle in self.scroll: scroll_wins.append(cwin) else: try: group_wins[cgroupid]+=[cwin] except: group_wins[cgroupid]=[cwin] if(ctty[0]=="t"): # telnet ctype="telnet" cpids = None cpids_data=None else: ctype="basic" # get sorted pids in window cpids=sc.get_tty_pids(ctty) cpids_data=[] ncpids=[] for pid in cpids: try: pidinfo=sc.get_pid_info(pid) (exehead,exetail)=os.path.split(pidinfo[1]) if exetail in self.blacklist: blacklist=True else: blacklist=False cpids_data.append(pidinfo+tuple([blacklist])) ncpids.append(pid) except: errors.append('%s PID %s: Unable to access. No permission or no procfs.'%(cwin,pid)) cpids=ncpids if(cpids): for i,pid in enumerate(cpids): if(cpids_data[i][3]): text="BLACKLISTED" else: text="" l=cpids_data[i][2].split('\0') jremove=[] wprev=False for j,w in enumerate(l): if w == '-ic' or w == '-c': wprev=True elif wprev: if w.startswith(self.primer): jremove+=j,j-1 wprev=False if jremove: s=[] for j,w in enumerate(l): if j not in jremove: s.append(w) newdata=(cpids_data[i][0],cpids_data[i][1],"\0".join(["%s"%v for v in s]),cpids_data[i][3]) cpids_data[i]=newdata #out('%s pid = %s: cwd = %s; exe = %s; cmdline = %s' % (text,pid, cpids_data[i][0], cpids_data[i][1], cpids_data[i][2])) vim_name=str(None) args=cpids_data[i][2].split('\0') if args[0].endswith(self.primer_base) and args[1]=='-p': sys.stdout.write('(primer)') rollback=self.__rollback(cpids_data[i][2]) #out(str(rollback)) elif args[0] in self.vim_names and self.bVim: sys.stdout.write('(vim)') vim_name=self.__save_vim(id) nargs=[] rmarg=False for arg in args: if rmarg: rmarg=False pass elif arg in ('-S','-i'): rmarg=True else: nargs.append(arg) args=nargs newdata=(cpids_data[i][0],cpids_data[i][1],"\0".join(["%s"%v for v in args]),cpids_data[i][3]) cpids_data[i]=newdata cpids_data[i]=(cpids_data[i][0],cpids_data[i][1],cpids_data[i][2],cpids_data[i][3],vim_name) scrollback_filename=os.path.join(self.basedir,self.savedir,"hardcopy."+id) sys.stdout.write("%s %s | "%(cwin,ctype)) errors+=self.__save_win(id,ctype,cpids_data,ctime,rollback) rollback=None,None,None out('') fmru.close() util.remove(os.path.join(findir,"winlist")) # remove ignored scrollbacks if 'all' in self.scroll: for f in glob.glob(os.path.join(self.basedir, self.savedir, "hardcopy.*")): open(f,'w') elif self.scroll: scroll_groups_tmp=[] while scroll_groups: sgroup=scroll_groups.pop() if sgroup not in scroll_groups_tmp: scroll_groups_tmp.append(sgroup) try: ngroups = group_groups[sgroup] if ngroups: for g in ngroups: scroll_groups.append(g) except: pass scroll_groups = scroll_groups_tmp out('Scrollback excluded groups: %s'%str(scroll_groups)) for sgroup in scroll_groups: scroll_wins.append(sgroup) try: for w in group_wins[sgroup]: scroll_wins.append(w) except: pass out('All scrollback excluded windows: %s'%str(scroll_wins)) for w in scroll_wins: util.remove(os.path.join(self.basedir, self.savedir, "hardcopy.%s"%w)) # remove ignored windows if self.excluded: excluded_groups_tmp=[] while excluded_groups: egroup=excluded_groups.pop() if egroup not in excluded_groups_tmp: excluded_groups_tmp.append(egroup) try: ngroups = group_groups[egroup] if ngroups: for g in ngroups: excluded_groups.append(g) except: pass excluded_groups = excluded_groups_tmp out('Excluded groups: %s'%str(excluded_groups)) for egroup in excluded_groups: excluded_wins.append(egroup) try: for w in group_wins[egroup]: excluded_wins.append(w) except: pass out('All excluded windows: %s'%str(excluded_wins)) bpath1 = os.path.join(self.basedir, self.savedir, "win_") bpath2 = os.path.join(self.basedir, self.savedir, "hardcopy.") bpath3 = os.path.join(self.basedir, self.savedir, "vim_W") for win in excluded_wins: util.remove(bpath1+win) util.remove(bpath2+win) for f in glob.glob(bpath3+win+'_*'): util.remove(f) linkify(os.path.join(self.basedir,self.savedir),"win_"+homewindow,"last_win") if errors: out('Errors:') for error in errors: out(error) out('\nSaved: '+str(ctime))