Spawn ssh在父进程中工作,使用fork然后尝试生成失败返回缓冲区以期望

时间:2014-11-04 19:38:28

标签: ssh tcl expect spawn

我正在尝试使用TCL生成一个新的SSH会话,但首先我分配了一个4个不同的进程,每个进程都有唯一的ip,如下所示:

foreach  vendor $vendorList {
        set pid [fork]

        switch $pid {
            -1  {
                puts "Fork attempt #$vendor failed."
            }
            0   {
                puts "I am child process vendor = $vendor ."
                $vendor ConnectSSH
                exec kill [pid]
            }
            default {
                lappend pids $pid
                puts "The parent just spawned child process #$vendor."
            }
        }
    }

    foreach id $pids {
        wait $id ;#need to add maximum time to wait
        puts "process id $id is done"
    }
    puts "all done"

ConnectSSH是:

#ConnectSSH

    puts "===== Function - Vendor:openConnection ====="

    set sid [spawn ssh "$user@$managementIP "]
    puts $sid
    set sid $spawn_id
    set logged_in 0
    set retry 0
    while {!$logged_in} {
        expect -i $sid timeout {
            if {$retry < 5} {
                puts "retry = $retry"
                after 5000
                incr retry
            } else {
                timedout "in while loop"
                return -1
            }
        } eof {
        # for some reason we lost connection to the application.
            timedout "spawn failed with eof"
            return -1
        } "Are you sure you want to continue connecting (yes/no)? " {
            exp_send -i $sid -- "yes\r"
        } "password" {
            exp_send -i $sid -- "$password\r"
            exp_send -i $sid -- "\r\r\r"
        } "$user" {
            set logged_in 1
            exp_send -i $sid -- "\r\r"
        } "No route to host" {
            puts "Failed to open ssh connection. No route to host. Exit."
            return -1
        } "timed out" {
            puts "Failed to open ssh connection. Connection timed out. Exit."
            return -1
        }

    }

我从Spawn获得了新的sid,但期望返回&#34;在while循环中#34;似乎是超时。 当不在&#34; fork&#34;一切都很棒。

0 个答案:

没有答案