Skip to content

Commit 2cedbc9

Browse files
committed
added testshell for having multiple interactive commands in a single operation and modified shell.py to handle this
1 parent 22d76a1 commit 2cedbc9

2 files changed

Lines changed: 31 additions & 10 deletions

File tree

shell.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@
2828

2929
parser = argparse.ArgumentParser(description="Noninteractive and Interactive \
3030
command excution. Must provide all the details in cmd_list inside the \
31-
file shell.py for \
32-
interactive commands. host is an optional argument applicable only for \
3331
Non-interactive commands")
3432

3533
parser.add_argument("host", help="provide host name", nargs="?",
@@ -48,6 +46,7 @@
4846
("myscp", ("scp a.txt root@localhost:/home/netsim/ravi/cfiles"),(u".*password:","netsim")),
4947
("remove", ("rm -i cfiles/a.txt"), (u".*rm: remove.*\?", u"yes")),
5048
("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)+",""))
5150
)
5251

5352
interactive = False
@@ -87,8 +86,12 @@ def exec_cmd_interactive(self):
8786
logger.info("Executing COMMAND {0}".format(cur_cmd[1]) +
8887
" - on HOST {0}".format(self.host))
8988
c = pexpect.spawn(cur_cmd[1])
90-
c.expect(cur_cmd[2][0])
91-
c.sendline(cur_cmd[2][1])
89+
for i in cur_cmd[2:]:
90+
c.expect(i[0])
91+
if i[1] != "":
92+
c.sendline(i[1])
93+
logger.info(c.before)
94+
logger.info(c.after)
9295
c.wait()
9396
c.kill(1)
9497

@@ -98,18 +101,22 @@ def exec_cmd_interactive_all(self):
98101
logger.info("Executing COMMAND {0}".format(cur_cmd[1]) +
99102
" - on HOST {0}".format(self.host))
100103
c = pexpect.spawn(cur_cmd[1])
101-
c.expect(cur_cmd[2][0])
102-
c.sendline(cur_cmd[2][1])
104+
for i in cur_cmd[2:]:
105+
c.expect(i[0])
106+
if i[1] != "":
107+
c.sendline(i[1])
108+
logger.info(c.before)
109+
logger.info(c.after)
103110
c.wait()
104111
time.sleep(10)
105112
c.kill(1)
106113

107114
def exec_ssh(self):
108115
try:
109116
s = pxssh.pxssh()
110-
hostname = raw_input('hostname: ')
111-
username = raw_input('username: ')
112-
password = getpass.getpass('password: ')
117+
hostname = self.hostname
118+
username = self.username
119+
password = self.password
113120
s.login(hostname, username, password)
114121
s.sendline('uptime') # run a command
115122
s.prompt() # match the prompt
@@ -127,7 +134,6 @@ def exec_ssh(self):
127134
except pxssh.ExceptionPxssh as e:
128135
print("pxssh failed on login.")
129136
print(e)
130-
131137
if not interactive and args.command != 'all':
132138
n = NonInteractive(command=args.command, host=args.host)
133139
n.exec_cmd()

testshell.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
s = raw_input("operation\n")
2+
#print s
3+
t = input("number1\n")
4+
#print t
5+
u = input("number2\n")
6+
#print u
7+
if s=="add":
8+
print t+u
9+
elif s=="sub":
10+
print t-u
11+
elif s=="mul":
12+
print t*u
13+
elif s=="div":
14+
print t/u
15+

0 commit comments

Comments
 (0)