- 积分
- 16844
在线时间 小时
最后登录1970-1-1
|

楼主 |
发表于 2017-12-22 17:12:25
|
显示全部楼层
echo | ssh -l root passwd --stdin7 W" E# ?2 ~! ]6 F6 ^& ^* `, v" H% L
echo "newpass" | ssh -l root 10.11.103.151 passwd --stdin dewang
# R- O; C, \: y8 E% Wroot@10.11.103.151's password:
/ n+ J* u9 |2 ~9 i* F/ }0 qChanging password for user dewang.- q2 O7 h. E# w( `( \8 D2 d
passwd: all authentication tokens updated successfully.( G8 i5 A4 K* D$ K! j8 a' i( p
, j$ r$ i2 O9 o" t4 s
5 @! T, T. U5 m) n6 V% T#!/usr/bin/expect( F1 |$ z" w5 N7 T6 l
#@brief to change user password by ssh remote machine
; h* C" i$ |- q+ J) w) N9 J+ @( ]7 ] Q# [. z
proc usage {funcname} {/ y+ R! Z) e* O; ]2 i
puts "Usage: "
) X y! Y; \* t: {7 i puts " $funcname -user "% e: U7 x: d! `" W2 c- J
puts " $funcname -root "7 x. p4 \- B+ _8 {' C
}
( U6 ^# D% h/ Y% o! b0 k+ a- v, C% p
# check param! v% A9 x$ p' ^# H. b
if { $argc != 5 } {3 S% F) T+ |" D# i5 c' O1 D
usage $argv0
+ a P, F5 }- S exit 1
0 |5 i: X* w8 e# w4 |1 d, _}
) p* a5 w) p! j( E Q/ M6 H- O+ {
# get param& V* K9 h: D1 C# D+ Q' f7 N
set host [lindex $argv 0]& C, J7 L4 C. d3 J Q
set username [lindex $argv 1]1 n3 @. }- v3 `
set newpasswd [lindex $argv 2]. D5 `) \7 A: E! j6 b4 K! x
set loginname "root"
6 N" [8 z, [) p- Aif { [string compare [lindex $argv 3] "-user"] == 0 } {1 J# z: V9 T+ x/ S
set loginname $username. f# f. t: o3 [8 k; I
}
, k- l; n2 r2 W) Aset passwd [lindex $argv 4]
) P% V" d' G7 v/ L" q+ Oputs "$host $username $newpasswd $loginname $passwd"
) ~: A$ E* c4 y; ]2 V7 a2 A/ ?& D. Q6 \$ A
spawn ssh -l $loginname $host( H. g& W9 r8 {6 ]' z, G3 c
expect {2 C: b1 l) {8 m& i& q
"*(yes/no)*" { send "yes\r"; set sshkey 1 } }9 V0 n* o# |
"*assword:*" { send "$passwd\r"; set sshkey 0 }
3 c; J9 @8 J& H$ b" _if sshkey == 1 {8 Z* L7 h, V% N5 c0 d# i A
expect "*password:*"- b, L4 D4 j/ C |) I
send "$passwd\r"0 R' Q" Q& p6 u
}; `3 V: R' D+ ~1 t2 w
}
, c# I+ W# `$ L+ H% Qexpect "*#"
& |7 g% Y* E4 ]5 T; i- ?# V) v0 G' [: n4 J7 M2 ^/ h. x) s9 S/ b R- L
if { [string compare $loginname "root"] == 0 } {
/ w+ R9 C4 K7 `2 m* O) | O #send "echo \"$username:$newpasswd\" | chpasswd\r"1 A* i/ q6 |1 w* F% P: t' I
send "echo \"$newpasswd\" | passwd --stdin \"$username\"\r"5 ?4 J" c5 R9 y L" [ O
} else {5 g/ F: N7 N+ W0 U$ P U+ P( M
send "passwd\r"
9 H1 S4 f4 r3 p expect {" f; w) _. _& J6 w+ k1 T
"*current*assword:" {send "$passwd\r"}3 d# |' E9 }& Y( s
"passwd: Authentication token manipulation error" {exit}3 \! x7 ? ^3 {* o6 T
}7 y$ r S% L. i8 x
expect "New*assword:"* \; T; ?1 D. A, A$ w& }. j
send "$newpasswd\r"0 \! M: i% L% Z( m( O, h* P+ L
expect "Retype*assword:"# M$ M' u" M3 ?, _" q# U+ G- b. N& `
send "$newpasswd\r"
9 A' f: Y& T$ D}' ?9 f U( Y' h, N
expect "*#") B! _" P" M; H0 p
send "exit\r"- D: c8 S& N' U# J, R2 Y
#interact 是否将交互权接过来,如果接过来,则用户这时可进行交互操作 |
|