1

I made a little script to change passwd of many servers. Its syntax is expect - an extension of tcl. It seems so bad but can work fine.

One problem is when I login to a server the first time, I need to enter 'yes' to confirm to add this server to ~/.ssh/know_hosts. It's boring me a long time. Is there any simple style to solve this?

Thanks and sorry for my terrible English.

proc passwd4mqm {ip_list} {
    foreach Arg $ip_list {
    set  city [lindex $Arg 0]
    set  ip [lindex $Arg 1]
    set  user [lindex $Arg 2]
    set  passwd [lindex $Arg 3]
    send_user  "\n\n>>> $city-$ip \n"
    spawn ssh $user@$ip
    expect {
        "yes" {
        send "yes\r"
        expect {
            "password" { 
            send "$passwd\r"
            expect {
                "$user" {
                send "passwd mqm\r"
                expect {
                    "New UNIX password:" {
                    send "$passwd\r"
                    expect {
                        "Retype new UNIX password:" {
                        send "$passwd\r"}
                    }
                    }
                }
                send "exit\r"
                }
            }
            }
        }
        }
        "password" { 
        send "$passwd\r"
        expect {
                    "$user" {
            send "passwd mqm\r"
            expect {
                "New UNIX password:" {
                send "$passwd\r"
                expect {
                    "Retype new UNIX password:" {
                    send "$passwd\r"}
                }
                }
            }
            send "exit\r"
                    }
        }
        }
    }
    interact
    }
}
Dennis Williamson
  • 60,515
  • 14
  • 113
  • 148
pluto
  • 13
  • 2

1 Answers1

0

Perhaps you are looking for something like exp_continue

expect {
  "*yes/no*" { send "yes\r"; exp_continue }
  "password:" { 
    ..... 
  }
}
gnarf
  • 713
  • 3
  • 8
  • 21