Skip to content

Commit fc26ad7

Browse files
committed
added log.py for logging and modified shell.py to use pxssh instead of pexpect to handle remote commands
1 parent 2cedbc9 commit fc26ad7

2 files changed

Lines changed: 71 additions & 77 deletions

File tree

log.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import logging
2+
3+
logger = logging.getLogger("project")
4+
logger.setLevel(logging.DEBUG)
5+
6+
formatter1 = logging.Formatter('%(asctime)s - %(name)s -\
7+
%(pathname)s - %(funcName)s - %(lineno)s - %(levelname)s - %(message)s')
8+
9+
ch = logging.StreamHandler()
10+
ch.setLevel(logging.DEBUG)
11+
ch.setFormatter(formatter1)
12+
logger.addHandler(ch)
13+
14+
fh = logging.FileHandler('test.log')
15+
fh.setLevel(logging.DEBUG)
16+
fh.setFormatter(formatter1)
17+
logger.addHandler(fh)

shell.py

Lines changed: 54 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,26 @@
11
import argparse
2-
import logging
32
import pexpect
43
import subprocess
54
import sys
6-
import getpass
7-
from pexpect import pxssh
8-
95
import time
10-
import pdb
11-
#pdb.set_trace()
12-
13-
logger = logging.getLogger(__name__)
14-
logger.setLevel(logging.DEBUG)
15-
16-
formatter1 = logging.Formatter('%(asctime)s - %(name)s -\
17-
%(pathname)s - %(funcName)s - %(lineno)s - %(levelname)s - %(message)s')
6+
from pexpect import pxssh
7+
import getpass
188

19-
ch = logging.StreamHandler()
20-
ch.setLevel(logging.DEBUG)
21-
ch.setFormatter(formatter1)
22-
logger.addHandler(ch)
9+
import log
2310

24-
fh = logging.FileHandler('test.log')
25-
fh.setLevel(logging.DEBUG)
26-
fh.setFormatter(formatter1)
27-
logger.addHandler(fh)
11+
logger = log.logger
2812

2913
parser = argparse.ArgumentParser(description="Noninteractive and Interactive \
3014
command excution. Must provide all the details in cmd_list inside the \
3115
Non-interactive commands")
3216

17+
parser.add_argument("command", help="provide command")
3318
parser.add_argument("host", help="provide host name", nargs="?",
3419
default="localhost")
35-
parser.add_argument("command", help="provide command")
20+
parser.add_argument("username", help="provide user name", nargs="?",
21+
default="root")
22+
parser.add_argument("password", help="provide password", nargs="?",
23+
default="netsim")
3624
#parser.add_argument("cmd_type", nargs='?',
3725
# help="provide i for interactive, n for non-interactive", default="n")
3826

@@ -43,10 +31,15 @@
4331
args = parser.parse_args()
4432

4533
cmd_list = (
46-
("myscp", ("scp a.txt root@localhost:/home/netsim/ravi/cfiles"),(u".*password:","netsim")),
47-
("remove", ("rm -i cfiles/a.txt"), (u".*rm: remove.*\?", u"yes")),
48-
("myscp2", ("scp a.txt [email protected]:/home/netsim/ravi/cfiles"),(u".*password:","netsim")),
49-
("testshell", "python testshell.py",(u"operation","add"),(u"number1","6"),(u"number2","7"),(u"(\d)+",""),(u"(\d)+",""))
34+
#("myscp", ("scp a.txt root@localhost:/home/netsim/ravi/cfiles"),(u".*password:","netsim")),
35+
# ("remove", ("rm -i cfiles/a.txt"), (u".*rm: remove.*\?", u"yes")),
36+
# ("myscp2", ("scp a.txt [email protected]:/home/netsim/ravi/cfiles"),(u".*password:","netsim")),
37+
("testshell1", "python testshell.py", (u"operation","add"), \
38+
(u"number1","26"),(u"number2","37"),(u"(\d)+",""),(u"(\d)+","")),
39+
("testshell2", "python testshell.py", (u"operation","sub"), \
40+
(u"number1","56"),(u"number2","14"),(u"(\d)+",""),(u"(\d)+","")),
41+
("testshell3", "python testshell.py", \
42+
(u"operation","mul"),(u"number1","65"),(u"number2","75"),(u"(\d)+",""),(u"(\d)+","")),
5043
)
5144

5245
interactive = False
@@ -59,9 +52,11 @@
5952

6053

6154
class RemoteCommand(object):
62-
def __init__(self, command, host):
55+
def __init__(self, command, host, username, password):
6356
self.command = command
6457
self.host = host
58+
self.username = username
59+
self.password = password
6560

6661
class NonInteractive(RemoteCommand):
6762
def exec_cmd(self):
@@ -80,72 +75,54 @@ def exec_cmd(self):
8075
logger.info("OUTPUT: {0}".format(result))
8176

8277
class Interactive(RemoteCommand):
83-
def exec_cmd_interactive(self):
84-
logger.info("interactive")
78+
def exec_remote(self, s):
79+
logger.info("remote interactive")
8580
cur_cmd = icmd
8681
logger.info("Executing COMMAND {0}".format(cur_cmd[1]) +
8782
" - on HOST {0}".format(self.host))
88-
c = pexpect.spawn(cur_cmd[1])
83+
s.sendline(cur_cmd[1])
8984
for i in cur_cmd[2:]:
90-
c.expect(i[0])
85+
s.expect(i[0])
9186
if i[1] != "":
92-
c.sendline(i[1])
93-
logger.info(c.before)
94-
logger.info(c.after)
95-
c.wait()
96-
c.kill(1)
87+
s.sendline(i[1])
88+
logger.info(s.before)
89+
logger.info(s.after)
9790

98-
def exec_cmd_interactive_all(self):
99-
logger.info("interactive all")
91+
def exec_remote_all(self, s):
92+
logger.info("remote interactive all")
10093
for cur_cmd in cmd_list:
10194
logger.info("Executing COMMAND {0}".format(cur_cmd[1]) +
102-
" - on HOST {0}".format(self.host))
103-
c = pexpect.spawn(cur_cmd[1])
95+
" - on HOST {0}".format(self.host))
96+
s.sendline(cur_cmd[1])
10497
for i in cur_cmd[2:]:
105-
c.expect(i[0])
98+
s.expect(i[0])
10699
if i[1] != "":
107-
c.sendline(i[1])
108-
logger.info(c.before)
109-
logger.info(c.after)
110-
c.wait()
111-
time.sleep(10)
112-
c.kill(1)
100+
s.sendline(i[1])
101+
logger.info(s.before)
102+
logger.info(s.after)
113103

114-
def exec_ssh(self):
104+
def remote(self):
115105
try:
106+
logger.info("remote")
116107
s = pxssh.pxssh()
117-
hostname = self.hostname
108+
hostname = self.host
118109
username = self.username
119110
password = self.password
120-
s.login(hostname, username, password)
121-
s.sendline('uptime') # run a command
122-
s.prompt() # match the prompt
123-
print(s.before) # print everything before the prompt.
124-
s.sendline('ls -l')
125-
s.prompt()
126-
print(s.before)
127-
s.sendline('df')
128-
s.prompt()
129-
print(s.before)
130-
s.sendline('pwd')
131-
s.prompt()
132-
print(s.before)
111+
s.login (hostname, username, password)
112+
if args.command != "all":
113+
self.exec_remote(s=s)
114+
else:
115+
self.exec_remote_all(s=s)
133116
s.logout()
134-
except pxssh.ExceptionPxssh as e:
135-
print("pxssh failed on login.")
136-
print(e)
137-
if not interactive and args.command != 'all':
138-
n = NonInteractive(command=args.command, host=args.host)
117+
except pxssh.ExceptionPxssh, e:
118+
print "pxssh failed on login."
119+
print str(e)
120+
121+
if not interactive and args.command != "all":
122+
n = NonInteractive(command=args.command, host=args.host, \
123+
username=args.username, password=args.password)
139124
n.exec_cmd()
140-
elif args.command != 'all':
141-
i = Interactive(command=args.command, host=args.host)
142-
i.exec_cmd_interactive()
143125
else:
144-
i = Interactive(command=args.command, host=args.host)
145-
i.exec_cmd_interactive_all()
146-
147-
'''
148-
# pxssh
149-
i = Interactive(command=args.command, host=args.host)
150-
i.exec_ssh()
151-
'''
126+
i = Interactive(command=args.command, host=args.host, \
127+
username=args.username, password=args.password)
128+
i.remote()

0 commit comments

Comments
 (0)