Skip to content

Commit 2bcae1f

Browse files
committed
changed shell to use only pxssh and removed subprocess
1 parent 1101e86 commit 2bcae1f

1 file changed

Lines changed: 21 additions & 33 deletions

File tree

shell.py

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,15 @@
88
from remote_commands import cmd_list
99

1010
parser = argparse.ArgumentParser(description="Noninteractive and Interactive \
11-
command excution. Must provide all the details in cmd_list inside the \
12-
Non-interactive commands")
11+
command execution. Must provide all the details in cmd_list \
12+
in remote_commands.py for interactive commands.")
13+
14+
parser.add_argument("command", help="provide command. If interactive, \
15+
should be one of commands in remote_commands.py or all which would \
16+
execute every command in remote_commands.py one after another. e.g.\n \
17+
python shell.py testshell1 127.0.0.1 root password \n \
18+
python shell.py all 127.0.0.1 root password")
1319

14-
parser.add_argument("command", help="provide command")
1520
parser.add_argument("host", help="provide host name", nargs="?",
1621
default="localhost")
1722
parser.add_argument("username", help="provide user name", nargs="?",
@@ -35,35 +40,13 @@
3540
icmd = i
3641
break
3742

38-
3943
class RemoteCommand(object):
4044
def __init__(self, command, host, username, password):
4145
self.command = command
4246
self.host = host
4347
self.username = username
4448
self.password = password
4549

46-
class NonInteractive(RemoteCommand):
47-
def exec_cmd(self):
48-
try:
49-
logger.info("non-interactive")
50-
logger.info("Executing COMMAND {0}".format(self.command) +
51-
" - on HOST {0}".format(self.host))
52-
ssh = subprocess.Popen(["ssh", "%s" % self.host, self.command],
53-
shell=False,
54-
stdout=subprocess.PIPE,
55-
stderr=subprocess.PIPE)
56-
result = ssh.stdout.readlines()
57-
if result == []:
58-
error = ssh.stderr.readlines()
59-
#logger.error("ERROR: {0}".format(error))
60-
logger.error("ssh failed on login.")
61-
else:
62-
logger.info("OUTPUT: {0}".format(result))
63-
except:
64-
logger.error("ssh failed on login.")
65-
66-
class Interactive(RemoteCommand):
6750
def remote(self):
6851
try:
6952
logger.info("remote")
@@ -72,7 +55,9 @@ def remote(self):
7255
username = self.username
7356
password = self.password
7457
s.login(hostname, username, password)
75-
if args.command != "all":
58+
if not interactive and args.command != "all":
59+
self.exec_remote_normal(s=s)
60+
elif args.command != "all":
7661
self.exec_remote(s=s)
7762
else:
7863
self.exec_remote_all(s=s)
@@ -110,12 +95,15 @@ def exec_remote_all(self, s):
11095
logger.info(s.before)
11196
logger.info(s.after)
11297

98+
def exec_remote_normal(self,s):
99+
logger.info("remote non-interactive")
100+
cur_cmd = self.command
101+
logger.info("Executing COMMAND {0}".format(cur_cmd) +
102+
" - on HOST {0}".format(self.host))
103+
s.sendline(cur_cmd)
104+
s.prompt()
105+
logger.info(s.before)
113106

114-
if not interactive and args.command != "all":
115-
n = NonInteractive(command=args.command, host=args.host, \
116-
username=args.username, password=args.password)
117-
n.exec_cmd()
118-
else:
119-
i = Interactive(command=args.command, host=args.host, \
107+
i = RemoteCommand(command=args.command, host=args.host, \
120108
username=args.username, password=args.password)
121-
i.remote()
109+
i.remote()

0 commit comments

Comments
 (0)