Expect Redirecting o/p to an Variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Expect Redirecting o/p to an Variable
# 1  
Old 07-26-2016
Expect Redirecting o/p to an Variable

I have a password reset expect script which stores all the op to an file. I need to check the whether password is successfully changed by greping out the file and storing the o/p to a variable.

But we try to print the variable , its shows only the command instead of its o/p.

Code:
                exec /dbase/mesa/bin/passwd_reset  $login "$oldpass"  server1 "$newpassword" > /tmp/passwd_reset_out$login 2>&1
                sleep 1
                exec  /dbase/mesa/bin/passwd_reset  $login "$oldpass" server2  "$newpassword" >> /tmp/passwd_reset_out$login 2>&1
                sleep 1
                exec  /dbase/mesa/bin/passwd_reset  $login "$oldpass" server3  "$newpassword" >> /tmp/passwd_reset_out$login 2>&1
                set out "grep -i 'password changed' /tmp/passwd_reset_out$login | wc -l"
               send_user "$out"


Last edited by RudiC; 07-26-2016 at 06:06 AM.. Reason: Changed ICODE to CODE tags.
# 2  
Old 07-26-2016
Quote:
Originally Posted by sudharson
I have a password reset expect script which stores all the op to an file. I need to check the whether password is successfully changed by greping out the file and storing the o/p to a variable.

But we try to print the variable , its shows only the command instead of its o/p.

Code:
exec /dbase/mesa/bin/passwd_reset  $login "$oldpass"  server1 "$newpassword" > /tmp/passwd_reset_out$login 2>&1
sleep 1
exec  /dbase/mesa/bin/passwd_reset  $login "$oldpass" server2  "$newpassword" >> /tmp/passwd_reset_out$login 2>&1
sleep 1
exec  /dbase/mesa/bin/passwd_reset  $login "$oldpass" server3  "$newpassword" >> /tmp/passwd_reset_out$login 2>&1
set out "grep -i 'password changed' /tmp/passwd_reset_out$login | wc -l"
send_user "$out"

First, i do not understand what you need the "exec"s for. In fact

Code:
exec command

inside a script should make that script terminate at this point, running "command" in its place instead. This, it seems to me, is hardly what you want.

Second, i have no idea what this is does:

Code:
set out "grep -i 'password changed' /tmp/passwd_reset_out$login | wc -l"

But it definitely won't do what it is supposed to. If you want a process substitution to take place - that is, execute a command and assign the output of it to a variable - do it this way:

Code:
variable=$(command | command | [...])

Furthermore:

Code:
grep 'search' /path/to/file | wc -l

is a useless use of a pipeline and the wc utility. Use

Code:
grep -c 'search' /path/to/file

instead.

I hope this helps.

bakunin
# 3  
Old 07-26-2016
Thanks for the reply. Sorry i should have been more specific.

This is an expect script which internally calls another expect script to reset password on multiple servers by passing userid , oldpass , server name and newpassword.

Code:
#
!/dvlp/bin/expect
package require cgi

................................
......................
......................

                exec /dbase/mesa/bin/passwd_reset  $login "$oldpass"  server1 "$newpassword" > /tmp/passwd_reset_out$login 2>&1
                sleep 1
                exec  /dbase/mesa/bin/passwd_reset  $login "$oldpass" server2  "$newpassword" >> /tmp/passwd_reset_out$login 2>&1
                sleep 1
                exec  /dbase/mesa/bin/passwd_reset  $login "$oldpass" server3  "$newpassword" >> /tmp/passwd_reset_out$login 2>&1
                set out "grep -i 'password changed' /tmp/passwd_reset_out$login | wc -l"
               send_user "$out"


Below is the passwd_reset which gets called from the previous script


Code:
#!/usr/bin/expect -f

set username [lindex $argv 0]
set password [lindex $argv 1]
set serverid [lindex $argv 2]
set newpassword [lindex $argv 3]

spawn ssh -q -o StrictHostKeyChecking=no $username@$serverid
expect "assword:"
send "$password\r"
expect  ">"
send "passwd\r"
expect "Old Password:"
send "$password\r"
expect "New password:"
send "$newpassword\r"
expect "Retype new password:"
send "$newpassword\r"
expect "password changed."
send "exit"


Moderator's Comments:
Mod Comment Please use code (NOT icode!) tags as required by forum rules!

Last edited by RudiC; 07-26-2016 at 08:41 AM.. Reason: Changed ICODE tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Redirecting terminal to variable

when i do something like this: bona=$(echo hi2 > /dev/pts/1 ; printf '%s\n' "" | sed '/^$/d') i get: hi2 and the $bona variable is empty, when I run: echo ${bona} i get the result "hi2" outside of the variable. I want it stored in the bona variable with nothing outputted to the... (6 Replies)
Discussion started by: SkySmart
6 Replies

2. Shell Programming and Scripting

Python get the expect value from a variable

the value of the variable is yes group=bsp_16 keyword="82599" test_var="-a xxx -b yyy" I want to get output with -a xxx -b yyy (0 Replies)
Discussion started by: yanglei_fage
0 Replies

3. Shell Programming and Scripting

Redirecting stdout to variable while printing it

Hi everybody, I am trying to do the thing you see in the title, and I can't simply do a=$(svn up) echo $a because the program (svn) gives output on lots of lines and in the variable the output is stored on only one line (resulting in a horribly formatted text). Any tips? Thanks,... (2 Replies)
Discussion started by: ocirne94
2 Replies

4. Shell Programming and Scripting

redirecting values from one variable to another in a loop.

The situation is like this: I am reading records from a file, depending upon some condition extracting fields from the file into different variables in a loop one by one. I need to print all the variable in line, so I am trying to redirect hose variables one by one to a variable called final_value... (1 Reply)
Discussion started by: mady135
1 Replies

5. Shell Programming and Scripting

Assigning last line to variable in expect

As part of an expect script, I have to convert a strange user ID to a conventional UNIX ID. To do this, I read the contents of a file and do a little awk magic. Here's that bit of the expect script: send "awk 'NF == 10 && \$NF == strange_user_id {print \$(NF-2)}' file_with_my_info\r" expect... (0 Replies)
Discussion started by: treesloth
0 Replies

6. Shell Programming and Scripting

Redirecting output that was redirected to variable and console

Hi all, I would like to store the output of a command in a variable and output it to the console at the same time. This is working fine using the following construct var=`command | tee /dev/tty` I use this in some scripts to display the output of the command on the console and, at the same... (2 Replies)
Discussion started by: script_man
2 Replies

7. Shell Programming and Scripting

passing variable to expect

Please tell me how to pass variable "a b c" to expect from the shell script 1/ example of input file # cat in-file var1 var2 a b c var4 2/ # this is my script - how to pass "a b c" as single variable ? cat in-file | while read x do my-expect x done 3/ # expect script - how to... (0 Replies)
Discussion started by: breaktime123
0 Replies

8. Shell Programming and Scripting

Transfer variable to an expect function

Hi There, I try to transfer a variable from the script to a function which use expect, but I don't succed. #!/bin/sh HPPASS1="$2" send_command() { echo "spawn ssh login@10.10.10.10" echo 'set password ' echo 'sleep 1' echo 'expect "*assword:*"'... (5 Replies)
Discussion started by: sylvainkalache
5 Replies

9. Shell Programming and Scripting

problem redirecting output of command to variable

Hi. I'm a newbie in scripting and i have this problem: i want to use the 'fuser' command on a file to tell if it's being accessed (for my purposes: still being written). I want to save the output of the command and later compare with the 'not being used' result. the script: #!/bin/bash... (2 Replies)
Discussion started by: nunovc
2 Replies

10. Shell Programming and Scripting

redirecting variable content to a file!

Hello! I'm having problems trying to extract the contents of a variable and placing it into a text file. Grateful for any help. Been trying something along the lines of: $variable > file.txt or `cat < $variable` > file.txt As you can see I'm a newbie to this :D (2 Replies)
Discussion started by: lloowen
2 Replies
Login or Register to Ask a Question