-
Notifications
You must be signed in to change notification settings - Fork 1
/
mksu
executable file
·65 lines (56 loc) · 1.34 KB
/
mksu
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
#!/usr/bin/env expect --
# vim:ts=4:sw=4:expandtab
#######################################################################
# NAME
# mksu - a toy for su with command
#
# SYNOPSIS
# mksu username password command
#
# AUTHORS
# neiku project <[email protected]>
#
# SEE ALSO
# su
# expect --help
#
# VERSION
# 2016/06/18: 支持使用su命令切换用户后执行命令
# 2017/01/16: 支持跨平台
# - 通过env程序+PATH变量动态查询bash,控制权
# 、准确性都得到保障
#
#######################################################################
set username "[lindex $argv 0]"
set password "[lindex $argv 1]"
set command "[lindex $argv 2]"
set timeout -1
if { $command == "" } {
send_user "usage: mksu username password command\n"
exit
}
if { $password == "" } {
# when MKX_MKSU_PASSWORD not exist
# expect will throw expection (to exit)
# now we catch and ignore it (password still empty)
catch { set password "$env(MKX_MKSU_PASSWORD)" }
}
send_user "run '$command' with $username\n"
set env(LC_CTYPE) en
spawn -noecho su $username --command=$command
expect {
# ask for password
"*assword:" {
send "$password\r"
}
# for command done
eof {
exit
}
# error occurred, then exit
"su" {
exit
}
}
expect eof
exit