Expect - assigning UNIX command output to a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Expect - assigning UNIX command output to a variable
# 1  
Old 05-22-2015
Expect - assigning UNIX command output to a variable

Hi,

I'm writing a script that connects through ssh (using "expect") and then is supposed to find whether a process on that remote machine is running or not. Here's my code (user, host and password are obviously replaced with real values in actual script):

Code:
#!/usr/bin/expect
set timeout 1
spawn ssh user@host
expect "*assword"
send "password\r"

expect "$"
#now I would do something like : 
# if [ -z "\$(pidof some_process)"]
# or set found_pid $(pidof some_process)

I'm at loss how to set a variable using the things that pidof would output and make a conditional statement with it...

I've googled it for quite a some time but couldnt find an answer... Thanks in advance!

Last edited by oseri; 05-22-2015 at 07:05 PM..
# 2  
Old 05-22-2015
When you use keys for passwordless authentication, feeding a script into ssh becomes as easy as just feeding the script into it:

Code:
ssh user@host exec /bin/sh <<EOF
if [ -z "\$(pidof some_process)"]
then
...
fi
EOF

This is because ssh is designed to work this way. It's not designed to have passwords forcefed into it -- it's designed to prevent you from doing that -- which is why you needed to install the expect brute-forcing utility to do so.
# 3  
Old 05-23-2015
Well, I know about keys, but I really need to know how to get variables like $pidof work in expect scripts
# 4  
Old 05-27-2015
Don't really know what you intend to do with the variable once you have it but this example might get you started:

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

set timeout 2
spawn -noecho ssh user@host
log_user 0
expect "*assword"
send "password\r"

expect {\$}
send "pidof some_process\r"
expect -re {\r\n(.*)\r\n(.*)\$}
set pidlist $expect_out(1,string)

if {[string length $pidlist] > 1} {
   puts "Found PID(s): $pidlist"
} else {
   puts "No PIDs found"
}

send "exit\r"
expect eof

Here we are matching on a $ symbol for the command prompt. Remove log_user 0 to see actual output while debugging.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Tcsh command for assigning output of awk to variable

Hi I have a text file with 2 values and I am trying to assign each value to a variable and then write those to text files. So if the textfile is data.txt with 2 values x and y I want to assign mean=x, and stdev=y and then write these out in text files alongwith the id ($id has already been... (6 Replies)
Discussion started by: violin
6 Replies

2. Shell Programming and Scripting

Assigning bc output to a variable

I'm converting decimal to integer with bc, and I'd like to assign the integer output from bc to a variable 'val'. E.g. In the code below: If b is 5000.000, lines 6 and 8 will output: 5000 (5000.000+0.5)/1 | bc I'd like val to take the value 5000 though, rather than 5000.000 Does someone... (3 Replies)
Discussion started by: pina
3 Replies

3. Shell Programming and Scripting

Assigning output from awk to variable

I have a script whose contents are as below result= awk 's=100 END {print s }' echo "The result is" $result The desired output is The result is 100 My script is running without exiting and i am also not getting the desired output. Please help (5 Replies)
Discussion started by: bk_12345
5 Replies

4. 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

5. UNIX for Dummies Questions & Answers

Assigning the output of a command to a variable, where there may be >1 line returned?

Hello I am using unix CLI commands for the Synergy CM software. The command basically searches for a folder ID and returns the names of the projects the folder sits in. The result is assigned to a variable: FIND_USE=`ccm folder -fu -u -f "%name"-"%version" ${FOLDER_ID}` When the command... (6 Replies)
Discussion started by: Glyn_Mo
6 Replies

6. Shell Programming and Scripting

Assigning output of a command to variable

When I run time -p <command>, it outputs: real X.XX user X.XX sys X.XXwhere X.XX is seconds. How I can take just that first number output, the seconds of real time, and assign that to a variable? (9 Replies)
Discussion started by: jeriryan87
9 Replies

7. Shell Programming and Scripting

Assigning output to a variable

I am new to unix shell scripting. I was trying to convert each lines in a file to upper case. I know how to convert the whole file. But here i have to do line by line. I am getting it in the below mentioned script #!/bin/bash #converting lower to upper in a file #tr "" "" <file1... (3 Replies)
Discussion started by: jpmena
3 Replies

8. Shell Programming and Scripting

Assigning output of command to a variable in shell

hi, I want to assign find command result into some temporary variable: jarPath= find /opt/lotus/notes/ -name $jarFile cho "the jar path $jarPath" where jarPath is temporary variable. Can anybody help on this. Thanks in advance ----Sankar (6 Replies)
Discussion started by: sankar reddy
6 Replies

9. Shell Programming and Scripting

assigning command output to a shell variable

I have the sql file cde.sql with the below contents: abcdefghij abcwhendefothers sdfghj when no one else when others wwhen%others exception when others Now I want to search for the strings containing when others together and ceck whether that does not occur more than once in the... (2 Replies)
Discussion started by: kprattip
2 Replies

10. Shell Programming and Scripting

Assigning output of command to a variable

Hi, I'm trying to assign the output of a command to a variable and then concat it with another string, however, it keeps overwriting the original string instead of adding on to the end of the string. Contents of test.txt --> This is a test var1="`head -n 1 test.txt`" echo $var1 (This is a... (5 Replies)
Discussion started by: oma04
5 Replies
Login or Register to Ask a Question