Skip to content

Commit 32ee02e

Browse files
committed
Added logging functionality
1 parent ed93b10 commit 32ee02e

1 file changed

Lines changed: 29 additions & 9 deletions

File tree

shell.py

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import argparse
2+
import logging
23
import pexpect
34
import subprocess
45
import sys
@@ -7,6 +8,22 @@
78
import pdb
89
#pdb.set_trace()
910

11+
logger = logging.getLogger(__name__)
12+
logger.setLevel(logging.DEBUG)
13+
14+
formatter1 = logging.Formatter('%(asctime)s - %(name)s -\
15+
%(pathname)s - %(funcName)s - %(lineno)s - %(levelname)s - %(message)s')
16+
17+
ch = logging.StreamHandler()
18+
ch.setLevel(logging.DEBUG)
19+
ch.setFormatter(formatter1)
20+
logger.addHandler(ch)
21+
22+
fh = logging.FileHandler('test.log')
23+
fh.setLevel(logging.DEBUG)
24+
fh.setFormatter(formatter1)
25+
logger.addHandler(fh)
26+
1027
parser = argparse.ArgumentParser()
1128
parser.add_argument("host", help="provide host name", nargs="?",
1229
default="localhost")
@@ -16,7 +33,7 @@
1633
args = parser.parse_args()
1734

1835
cmd_list = (
19-
("shellcopy", ("scp a.txt root@localhost:/home/netsim/ravi/cfiles"),(u".*password:","netsim")),
36+
("myscp", ("scp a.txt root@localhost:/home/netsim/ravi/cfiles"),(u".*password:","netsim")),
2037
("remove", ("rm -i cfiles/a.txt"), (u".*rm: remove.*\?", u"yes")),
2138
)
2239

@@ -35,34 +52,37 @@ def __init__(self, command, host):
3552

3653
class NonInteractive(RemoteCommand):
3754
def exec_cmd(self):
38-
print "Executing command {0}".format(self.command),
39-
print "on host {0}".format(self.host)
55+
logger.info("non-interactive")
56+
logger.info("Executing COMMAND {0}".format(self.command) +
57+
" - on HOST {0}".format(self.host))
4058
ssh = subprocess.Popen(["ssh", "%s" % self.host, self.command],
4159
shell=False,
4260
stdout=subprocess.PIPE,
4361
stderr=subprocess.PIPE)
4462
result = ssh.stdout.readlines()
4563
if result == []:
4664
error = ssh.stderr.readlines()
47-
print "ERROR: {0}".format(error)
65+
logger.error("ERROR: {0}".format(error))
4866
else:
49-
print "OUTPUT: {0}".format(result)
67+
logger.info("OUTPUT: {0}".format(result))
5068

5169
class Interactive(RemoteCommand):
5270
def exec_cmd_interactive(self):
53-
print "interactive"
71+
logger.info("interactive")
5472
cur_cmd = icmd
55-
print cur_cmd
73+
logger.info("Executing COMMAND {0}".format(cur_cmd[1]) +
74+
" - on HOST {0}".format(self.host))
5675
c = pexpect.spawnu(cur_cmd[1])
5776
c.expect(cur_cmd[2][0])
5877
c.sendline(cur_cmd[2][1])
5978
c.wait()
6079
c.kill(1)
6180

6281
def exec_cmd_interactive_all(self):
63-
print "interactive all"
82+
logger.info("interactive all")
6483
for cur_cmd in cmd_list:
65-
print cur_cmd
84+
logger.info("Executing COMMAND {0}".format(cur_cmd[1]) +
85+
" - on HOST {0}".format(self.host))
6686
c = pexpect.spawnu(cur_cmd[1])
6787
c.expect(cur_cmd[2][0])
6888
c.sendline(cur_cmd[2][1])

0 commit comments

Comments
 (0)