Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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
    }
}
share|improve this question
    
Although this is very server related, It probably belongs on stackoverflow being that it is programming/scripting related? –  gnarf Sep 21 '09 at 9:05
    
:) ur answer solve my question.THX! I got. –  pluto Sep 21 '09 at 9:24
add comment

1 Answer

up vote 0 down vote accepted

Perhaps you are looking for something like exp_continue

expect {
  "*yes/no*" { send "yes\r"; exp_continue }
  "password:" { 
    ..... 
  }
}
share|improve this answer
    
Sine I'm keeping looking for the book Exploring Expect,any good stuff for expect learing? –  pluto Sep 21 '09 at 9:28
    
Honestly, that was a google result, I very rarely use expect. –  gnarf Sep 21 '09 at 17:14
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.