Visit Our UNIX and Linux User Community


Transfer variable to an expect function


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Transfer variable to an expect function
# 1  
Old 08-19-2009
Error 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.

Code:
#!/bin/sh

HPPASS1="$2"


send_command()
{
        echo "spawn ssh login@10.10.10.10"
        echo 'set password [lindex $argv 0]'

        echo 'sleep 1'
        echo 'expect "*assword:*"'

        echo 'sleep 1'
        echo 'send  $password'
        ...
        echo 'expect oet'

}
conf_=`send_command "$HPPASS1" | /usr/bin/expect >> temp_log`

The device say me it's not the good password.

Thanks by advance.
# 2  
Old 08-19-2009
Code:
#!/bin/sh
send_command()
{
      password=$1
        echo "spawn ssh login@10.10.10.10"
        echo 'set password [lindex $argv 0]'

        echo 'sleep 1'
        echo 'expect "*assword:*"'

        echo 'sleep 1'
        echo 'send  $password'
        ...
        echo 'expect oet'

}
HPPASS1="$1"
conf_=`send_command "$HPPASS1" | /usr/bin/expect >> temp_log`



---------- Post updated at 09:44 AM ---------- Previous update was at 09:42 AM ----------

Code:
#!/bin/sh
send_command()
{
      password=$1
        echo "spawn ssh login@10.10.10.10"
        echo 'set password [lindex $argv 0]'

        echo 'sleep 1'
        echo 'expect "*assword:*"'

        echo 'sleep 1'
        echo 'send  $password'
        ...
        echo 'expect oet'

}
HPPASS1="$1"
conf_=`send_command "$HPPASS1" | /usr/bin/expect >> temp_log`

what menas $arg in your code? i said yoou because yo don't hava this variable in the shell.
# 3  
Old 08-20-2009
Thanks chipcmc for your answer, finnally I find another solution; I don't use the expect way of define variable and use the classical way to send variable to a function:


Code:
#!/bin/sh

HPPASS1="$2"

send_command()
{
        echo "spawn ssh login@10.10.10.10"

        echo 'sleep 1'
        echo 'expect "*assword:*"'

        echo 'sleep 1'
        echo 'send  $1'
        ...
        echo 'expect oet'

}
conf_=`send_command "$HPPASS1" | /usr/bin/expect >> temp_log`

After I don't know if it's the better way but it's shorter and it works!
# 4  
Old 08-20-2009
the are one think i don't understand

in main of your shell:
HPPASS1="$2"
send_command "$HPPASS1

this means that you call you script with 2 parameter?i think that only call with one paremeter for this reason i put HPPASS1=$1 xD....

in the fuction i put the varible password with the propouse that you see the relation betwen the call and the parameters of the function.

The are not great diferences:
password=$1 ........ echo "send $password"

than
.......................... echo "send $1"
the principal diferent is that one is more easy to read and to manteiniment of the shell, this is important in big shell.

In you case i think not diferent to use a temp variable or use de $1,$2 ....
# 5  
Old 08-24-2009
Haha, yes I'm sorry about this Smilie

Exactly, I works with 2 parameters, but for the example I delete one because it was not more interesting to explain with 2 on the forum.

So in my first message, the code should be

Code:
HPPASS1="$1"

and not

Code:
HPPASS1="$2"

Thanks for interesting Smilie
# 6  
Old 09-26-2009
test

please ignore

Previous Thread | Next Thread
Test Your Knowledge in Computers #646
Difficulty: Easy
Apple Computer acquired NeXT on February 4, 1997, for $929 million USD.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to set file transfer in binary mode in SFTP using Expect?

The below is my script. /usr/bin/expect<<EOD spawn /usr/bin/sftp -o Port=$PORT $USER@$HOST expect "sftp>" expect "password:" set timout 15 send "$password\r" expect "sftp>" send "lcd $remotedir\r" expect "sftp>" ... (1 Reply)
Discussion started by: Anilsaggu9
1 Replies

2. Shell Programming and Scripting

How to pass variable from one function to another function?

updateEnvironmentField() { linewithoutquotes=`echo $LINE | tr -d '"'` b() } I want to pass variable named $linewithoutquotes to another method called b(), which is called from updateEnvironmentField() method. How to do the above requirement with shell script (1 Reply)
Discussion started by: pottic
1 Replies

3. Shell Programming and Scripting

Passing variable value in a function to be used by another function

Hello All, I would like to ask help from you on how to pass variable value from a function that has been called inside the function. I have created below and put the variables in " ". Is there another way I can do this? Thank you in advance. readtasklist() { while read -r mod ver... (1 Reply)
Discussion started by: aderamos12
1 Replies

4. Shell Programming and Scripting

Calling a function which uses expect from a shells script

Hi all, This is the first time i am using expect. I am trying to call a function with in the shell script. The function will shh to a new server and will pass the password using expect and send. I need help in calling the fuction i am getting follaowing errors... here the script ... (8 Replies)
Discussion started by: firestar
8 Replies

5. Shell Programming and Scripting

How to pass a function with a variable parameter into another variable?

Hello again :) Am currently trying to write a function which will delete a record from a file. The code currently looks as such: function deleteRecord() { clear read -p "Please enter the ID of the record you wish to remove: " strID ... (2 Replies)
Discussion started by: U_C_Dispatj
2 Replies

6. Shell Programming and Scripting

Expect script file transfer failure

I use expect to spawn an sftp file transfer. On occasion the transfer fails or doesn't complete correctly, resulting in: "sftp> cd /returns sftp> rename /returns/TESTFILE.TXT /returns/archive/TESTFILE.TXT Couldn't rename file "/returns/TESTFILE.TXT" to "/returns/archive/TESTFILE.TXT": Failure... (0 Replies)
Discussion started by: tjb1959
0 Replies

7. UNIX for Dummies Questions & Answers

Help with FTP Variable Transfer

Hey all, I'm trying to upload a file to my ftp server through an automated batch program. I will be uploading a file that's name changes according to you IP Address, so I am using the variable %ip% to represent the file's name. The problem occurs when it transfers over to the ftp portion of the... (2 Replies)
Discussion started by: piking
2 Replies

8. Shell Programming and Scripting

Passing global variable to a function which is called by another function

Hi , I have three funcions f1, f2 and f3 . f1 calls f2 and f2 calls f3 . I have a global variable "period" which i want to pass to f3 . Can i pass the variable directly in the definition of f3 ? Pls help . sars (4 Replies)
Discussion started by: sars
4 Replies

9. UNIX for Dummies Questions & Answers

passing a variable inside a variable to a function

I would like to know how to pass a variable inside a variable to a function. sample code below -------------- for x in 1 9 do check_null $C$x ##call function to check if the value is null if then echo "line number:$var_cnt,... (2 Replies)
Discussion started by: KingVikram
2 Replies

10. Shell Programming and Scripting

transfer a variable in a shell script?

Hey guys, seems I can't transfer the $ip into gawk in the following bash script, where the problem is? #!/bin/bash while read ip do gawk '$1~"$ip"' /tmp/source done < /tmp/ip ++ cat /tmp/ip 192.16.1.1|192.168.1.2|192.168.1.3 10.1.1.1|10.1.1.2 10.8.1.0|10.8.1.1|10.8.1.2 -- cat... (11 Replies)
Discussion started by: fedora
11 Replies

Featured Tech Videos