1 | #!/usr/bin/env python |
---|
2 | import socket, pexpect, ANSI |
---|
3 | import time, sys, os |
---|
4 | |
---|
5 | # Clearly having the password on the command line is not a good idea, but |
---|
6 | # then this entire enterprise is probably not the most security concious thing |
---|
7 | # I've ever built. This should be considered an experimental tool. |
---|
8 | # USER = sys.argv[1] |
---|
9 | # PASSWORD = sys.argv[2] |
---|
10 | # PORT = sys.argv[3] |
---|
11 | |
---|
12 | def daemonize (stdin='/dev/null', stdout='/dev/null', stderr='/dev/null'): |
---|
13 | '''This forks the current process into a daemon. |
---|
14 | Almost none of this is necessary (or advisable) if your daemon |
---|
15 | is being started by inetd. In that case, stdin, stdout and stderr are |
---|
16 | all set up for you to refer to the network connection, and the fork()s |
---|
17 | and session manipulation should not be done (to avoid confusing inetd). |
---|
18 | Only the chdir() and umask() steps remain as useful. |
---|
19 | |
---|
20 | References: |
---|
21 | UNIX Programming FAQ |
---|
22 | 1.7 How do I get my program to act like a daemon? |
---|
23 | http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC16 |
---|
24 | |
---|
25 | Advanced Programming in the Unix Environment |
---|
26 | W. Richard Stevens, 1992, Addison-Wesley, ISBN 0-201-56317-7. |
---|
27 | |
---|
28 | The stdin, stdout, and stderr arguments are file names that |
---|
29 | will be opened and be used to replace the standard file descriptors |
---|
30 | in sys.stdin, sys.stdout, and sys.stderr. |
---|
31 | These arguments are optional and default to /dev/null. |
---|
32 | Note that stderr is opened unbuffered, so |
---|
33 | if it shares a file with stdout then interleaved output |
---|
34 | may not appear in the order that you expect. |
---|
35 | ''' |
---|
36 | |
---|
37 | # Do first fork. |
---|
38 | try: |
---|
39 | pid = os.fork() |
---|
40 | if pid > 0: |
---|
41 | sys.exit(0) # Exit first parent. |
---|
42 | except OSError, e: |
---|
43 | sys.stderr.write ("fork #1 failed: (%d) %s\n" % (e.errno, e.strerror) ) |
---|
44 | sys.exit(1) |
---|
45 | |
---|
46 | # Decouple from parent environment. |
---|
47 | os.chdir("/") |
---|
48 | os.umask(0) |
---|
49 | os.setsid() |
---|
50 | |
---|
51 | # Do second fork. |
---|
52 | try: |
---|
53 | pid = os.fork() |
---|
54 | if pid > 0: |
---|
55 | sys.exit(0) # Exit second parent. |
---|
56 | except OSError, e: |
---|
57 | sys.stderr.write ("fork #2 failed: (%d) %s\n" % (e.errno, e.strerror) ) |
---|
58 | sys.exit(1) |
---|
59 | |
---|
60 | # Now I am a daemon! |
---|
61 | |
---|
62 | # Redirect standard file descriptors. |
---|
63 | si = open(stdin, 'r') |
---|
64 | so = open(stdout, 'a+') |
---|
65 | se = open(stderr, 'a+', 0) |
---|
66 | os.dup2(si.fileno(), sys.stdin.fileno()) |
---|
67 | os.dup2(so.fileno(), sys.stdout.fileno()) |
---|
68 | os.dup2(se.fileno(), sys.stderr.fileno()) |
---|
69 | |
---|
70 | # I now return as the daemon |
---|
71 | return 0 |
---|
72 | |
---|
73 | def main (): |
---|
74 | USER = sys.argv[1] |
---|
75 | PASSWORD = sys.argv[2] |
---|
76 | PORT = int(sys.argv[3]) |
---|
77 | |
---|
78 | #daemonize () |
---|
79 | #daemonize('/dev/null','/tmp/daemon.log','/tmp/daemon.log') |
---|
80 | |
---|
81 | sys.stdout.write ('Daemon started with pid %d\n' % os.getpid() ) |
---|
82 | |
---|
83 | vs = ANSI.ANSI (24,80) |
---|
84 | p = pexpect.spawn ('ssh %(USER)s@localhost'%locals(), timeout=9) |
---|
85 | p.expect ('assword') |
---|
86 | p.sendline (PASSWORD) |
---|
87 | time.sleep (0.2) |
---|
88 | #p.sendline ('stty -echo') |
---|
89 | #time.sleep (0.2) |
---|
90 | p.sendline ('export PS1="HAON "') |
---|
91 | time.sleep (0.2) |
---|
92 | p.expect (pexpect.TIMEOUT) |
---|
93 | print p.before |
---|
94 | vs.process (p.before) |
---|
95 | HOST = '' # Symbolic name meaning the local host |
---|
96 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
---|
97 | s.bind((HOST, PORT)) |
---|
98 | print 'Listen' |
---|
99 | s.listen(1) |
---|
100 | print 'Accept' |
---|
101 | while 1: |
---|
102 | conn, addr = s.accept() |
---|
103 | print 'Connected by', addr |
---|
104 | data = conn.recv(1024) |
---|
105 | print data |
---|
106 | |
---|
107 | if data == 'exit': |
---|
108 | p.sendline (exit) |
---|
109 | break |
---|
110 | if not data == "pass": |
---|
111 | p.sendline (data) |
---|
112 | time.sleep (0.1) |
---|
113 | p.expect (['HAON ',pexpect.TIMEOUT]) |
---|
114 | #response = p.before |
---|
115 | sh_response = p.before.replace ('\r', '') |
---|
116 | vs.process_list (sh_response) |
---|
117 | response = str (vs) |
---|
118 | print response |
---|
119 | sent = conn.send(response) |
---|
120 | if sent < len (response): |
---|
121 | print "Sent is too short" |
---|
122 | |
---|
123 | |
---|
124 | if __name__ == "__main__": |
---|
125 | # daemonize('/dev/null','/tmp/daemon.log','/tmp/daemon.log') |
---|
126 | main() |
---|
127 | |
---|
128 | |
---|
129 | |
---|