blob: 4a94436697e14beb8e4f2bae8b341d0aa007d494 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
#!/usr/bin/expect
if {$argc!=4&&$argc!=3} {
send_user "usage: execCmdBySu.sh resultFlag command name \[password\]\n"
exit 1
}
# 0��ִ�����ȡ�ý����1��ִ��&��̨����
set resultFlag [lindex $argv 0]
set cmd [lindex $argv 1]
set user [lindex $argv 2]
set pass [lindex $argv 3]
set timeout 30
set flag 0
#�رտ���̨�����Ϣ
log_user 0
spawn whoami
expect "root" {set flag 1}
# ��ִ������${cmd}����"echo $?",Ϊ��ȡcmd�������е�ʵ�ʽ��, �����ε���������ص������Ϣ
if {$resultFlag==0} {
spawn su - -c "${cmd};echo $?" ${user}
#��ǰ��¼�û�����root����������
if {$flag==0} {
expect {
"����*" {send ${pass}\r}
"password*" {send ${pass}\r}
default {
send_error "error\n"
exit 2
}
}
}
#����̨�����Ϣ
log_user 1
expect {
"* ���벻��ȷ*" {exit 1}
#----�˴���Ŀ����Ϊ���ܶ�ȡ������������
eof {exit 0}
}
} else {
spawn su - -c "${cmd}" ${user}
#��ǰ��¼�û�����root����������
if {$flag==0} {
expect {
"����*" {send ${pass}\r}
"password*" {send ${pass}\r}
default {
send_error "error\n"
exit 2
}
}
}
expect {
"* ���벻��ȷ*" {exit 1}
#----Ϊ��&��̨������������������
eof {
send_user "ok\n"
exit 0
}
}
}
interact
exit 0
#overlay chess
#overlay program args
#disconnect
#close -onexec 0 -i ${spawn_id}
#remove_nulls 1
|