XL,
I would have to do some more trial & error method to make your suggested technique of this script to work -- I'll update this discussion as soon as I could get it work. FYI, the highlighted portion(in red) below doesn't work:
My_NIM_Server:msouvik:/export/home/msouvik>./expect_script_3.sh
msouvik@My_NIM_Client_1's password:
Last unsuccessful login: Thu Nov 14 12:13:52 GMT 2013 on ssh from My_NIM_Server
Last login: Mon Nov 18 12:18:57 GMT 2013 on /dev/pts/2 from x.x.x.x
*******************************************************************************
* *
* *
* Welcome to AIX Version 7.1! *
* *
* *
* Please see the README file in /usr/lpp/bos for information pertinent to *
* this release of the AIX Operating System. *
* *
* *
*******************************************************************************
My_NIM_Client_1:msouvik:/export/home/msouvik>PS1='PROMPT# '
PROMPT #oslevel -s
7100-02-02-1316
PROMPT #exit
Connection to My_NIM_Client_1 closed.
The script is not able to set the PS1 shell parameter, it does it without being able to set it as below(already shared before):
My_NIM_Server:msouvik:/export/home/msouvik>./expect_script_3.sh
msouvik@My_NIM_Client_1's password:
Last unsuccessful login: Thu Nov 14 12:13:52 GMT 2013 on ssh from My_NIM_Server
Last login: Mon Nov 18 12:18:57 GMT 2013 on /dev/pts/2 from x.x.x.x
*******************************************************************************
* *
* *
* Welcome to AIX Version 7.1! *
* *
* *
* Please see the README file in /usr/lpp/bos for information pertinent to *
* this release of the AIX Operating System. *
* *
* *
*******************************************************************************
My_NIM_Client_1:msouvik:/export/home/msouvik>oslevel -s
7100-02-02-1316
My_NIM_Client_1:msouvik:/export/home/msouvik>exit
Connection to My_NIM_Client_1 closed.
msouvik@My_NIM_Client_2's password:
Last unsuccessful login: Thu Nov 14 12:13:52 GMT 2013 on ssh from My_NIM_Server
Last login: Mon Nov 18 12:18:57 GMT 2013 on /dev/pts/2 from x.x.x.x
*******************************************************************************
* *
* *
* Welcome to AIX Version 7.1! *
* *
* *
* Please see the README file in /usr/lpp/bos for information pertinent to *
* this release of the AIX Operating System. *
* *
* *
*******************************************************************************
My_NIM_Client_2:msouvik:/export/home/msouvik>oslevel -s
7100-02-02-1316
My_NIM_Client_2:msouvik:/export/home/msouvik>exit
Connection to My_NIM_Client_2 closed.
in2nix4life,
Your recent suggestion gave me error too, modified code pasted below:
+1 #!/usr/bin/expect -f
+2 #
+3 #
+4
+5 # slurp in file containing hostnames
+6 set hostfile [open "server_list" r]
+7 set hostdata [read $hostfile]
+8 close $hostfile
+9
+10 # loop through hosts, connect, and run command
+11 foreach h [split $hostdata "\n"] {
+12 log_file myLog.log # log output to a local log file
+13 if {$h eq {}} continue # skip any blank lines encountered in the server file
+14 set u "msouvik"
+15 set p "pass^123"
+16 set cmd "oslevel -s"
+17 spawn ssh -t $u@$h $cmd
+18 expect "?assword:*"
+19 send -- "$p\r"
+20 send -- "exit\r"
+21 send -- "\r"
+22 expect eof
+23 }
Error:
My_NIM_Server:msouvik:/export/home/msouvik>./expect_script.sh
wrong # args: extra words after "else" clause in "if" command
while compiling
"if {$h eq {}} continue # skip any blank lines encountered in the server file"
("foreach" body line 3)
invoked from within
"foreach h [split $hostdata "\n"] {
log_file myLog.log # log output to a local log file
if {$h eq {}} continue # skip any blank lines ..."
(file "./expect_script_4.sh" line 11)
Good news is, I could get my job done with pieces of code taken from each of your suggestions.
So, my expect code to run the desired cmd on target servers looks like this:
#!/usr/bin/expect -f
#
#
# set credentials
set u "msouvik"
set p "pass^123"
# get host name from command-line
set h [lindex $argv 0]
# set the command to run
set cmd "oslevel -s >> /export/home/msouvik/expect.out; multibos -S >> /export/home/msouvik/expect.out"
# spawn a connection to the host and run the given command
spawn -noecho ssh -t $u@$h $cmd
# handle the password prompt
expect "?assword:*"
send -- "$p\r"
send -- "\r"
And the code to scp the above run cmd o/p's is as below:
#!/usr/bin/expect -f
#
# set credentials
set u "msouvik"
set p "pass^123"
# get host name from command-line
set h [lindex $argv 0]
# scp the data onto NIM server
spawn -noecho scp $u@$h:/export/home/msouvik/expect.out /export/home/msouvik/expect/expect.out.$h
# handle the password prompt for scp
expect "?assword:*"
send -- "$p\r"
send -- "\r"
# done
expect eof
The scp is successfully able to get me the cmd o/p's to my NIM server suffixing with the relevant hostname as in "expect.out.$h"
You may ask, why did I choose to have 2 separate scripts for this purpose - thats coz when I club these to scripts into one - it fails to do the job(reason not known to me at the moment!)
The discussion doesn't end here until I'm able to fix both of your recent suggestions.
-- Souvik