DMS
DMS

Reputation: 357

Continue working on SSH session spawned on a previous proc

I wrote my first Expect script without using functions, its task is to connect to a remote host via SSH, then from the remote host start a TFTP copy.

I want to offer the user the option to copy another file, then I thought I would need to change my Expect code from a single sequence of lines and split it into functions.

First function would be to spawn SSH:

proc Connect {sshOptions userName switchIP userPassword aclFile} {

    spawn -noecho ssh "$sshOptions" $userName@$switchIP
    log_user 0
    expect {
            timeout exit
            -exact "Password:" { send -- "$userPassword\r" }
    }

    expect "*test#"
    send_user "Connection established\n"
}

Second function would be to start the copy:

proc Copy_ACL {aclFile} {
    send_user "Copying ACL file...\n"
    send "***copy command here**"
    expect "TFTP get operation was successful"
    send_user "ACL File copied\n"
}

Then I call functions as follows:

Connect $ssh_Options $user_Name $switch_IP $user_Password $acl_File
Copy_ACL $acl_File

At the moment, the issue I am facing is that, it seems function "Copy_ACL" does not know anything about the SSH session spawned by the previous function Connect. The copy command is sent to stdout although "log_user" has been set to 0.

What am I missing?

Also, is there another book about Expect besides "Exploring Expect"?

Upvotes: 0

Views: 92

Answers (1)

Erik Johnson
Erik Johnson

Reputation: 1164

I had some success modifying existing code to use the same Expect session in two procs. All that seemed to be necessary was to make expect_out and spawn_id global variables so they could be shared between the procs. Of course, this means that only one Expect session can run at once, but that isn't likely to be a problem.

Upvotes: 3

Related Questions