`
收藏列表
标题 标签 来源
expect获取某用户(id结果)并写入文件 expect http://bbs.chinaunix.net/thread-1639694-2-1.html
#!/usr/bin/expect

set host_ip "192.168.243.128"
set root_pwd "root"

set done 0
set donewitheof 0 

log_user 0

spawn ssh root@$host_ip "id mc"
while {!$done} {
    expect {
        eof {
            set done 1
            set donewitheof 1
        }
        -nocase -re "Permission denied*" {
            exit 1             
        }
        -nocase -re "password*" {
            send "$root_pwd\r"
        }
        -nocase -re "Are you sure you want to continue connecting*" {
            send "yes\r"
        }
        -nocase -re "\[\r\n\]+uid.+$" {
            set fd [open mc_groups "w+"]
            set result $expect_out(0,string)
            set result2 [string trim $result "\r\n"]
            puts $fd "$result2"
            flush $fd
            close $fd
        }
    }
}
Global site tag (gtag.js) - Google Analytics