2828
2929parser = 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
3533parser .add_argument ("host" , help = "provide host name" , nargs = "?" ,
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
5352interactive = 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-
131137if not interactive and args .command != 'all' :
132138 n = NonInteractive (command = args .command , host = args .host )
133139 n .exec_cmd ()
0 commit comments