4
4
5
5
"""
6
6
7
- __version__ = '0.4 '
7
+ __version__ = '0.5 '
8
8
9
9
import os , sys
10
10
import ConfigParser
11
11
import getpass
12
- from socket import socket
12
+ import argparse
13
+ import logging
13
14
14
15
import eapauth
15
16
import usermgr
16
- import argparse
17
+
17
18
18
19
def parse_arguments ():
19
- parser = argparse .ArgumentParser (description = 'Yet Another H3C. ' , prog = 'yah3c' )
20
+ parser = argparse .ArgumentParser (description = 'Yet Another H3C Authentication Client ' , prog = 'yah3c' )
20
21
parser .add_argument ('-u' , '--username' ,
21
- help = 'auth username' )
22
- parser .add_argument ('-p' , '--password' ,
23
- help = 'auth password' )
24
- parser .add_argument ('-i' , '--interface' , default = 'eth0' ,
25
- help = 'Etherent interface used to send packet.eth0 by default.' )
26
- parser .add_argument ('-d' , '--daemon' , action = 'store_true' ,
27
- help = 'Go to background after authentication.' )
22
+ help = 'Login in with this username' )
23
+ # parser.add_argument('-p', '--password',
24
+ # help='Password')
25
+ # parser.add_argument('-i', '--interface', default='eth0',
26
+ # help='Etherent interface used. Set as eth0 by default.')
27
+ # parser.add_argument('-d', '--daemon', action='store_true',
28
+ # help='Fork to background after authentication.')
29
+ # parser.add_argument('-D', '--dhcp',
30
+ # help='DHCP cmd used to obtain ip after authentication.')
31
+ parser .add_argument ('-debug' , action = 'store_true' ,
32
+ help = 'Enable debugging mode' )
28
33
args = parser .parse_args ()
29
- print args
34
+ return args
30
35
31
36
def prompt_user_info ():
32
37
username = raw_input ('Input username: ' )
@@ -37,13 +42,26 @@ def prompt_user_info():
37
42
break
38
43
else :
39
44
print 'Password do not match!'
45
+
40
46
dev = raw_input ('Decice(eth0 by default): ' )
41
47
if not dev :
42
48
dev = 'eth0'
49
+
50
+ choice = raw_input ('Forked to background after authentication(Yes by default)\n <Y/N>: ' )
51
+ if choice == 'n' or choice == 'N' :
52
+ daemon = False
53
+ else :
54
+ daemon = True
55
+
56
+ dhcp_cmd = raw_input ('Dhcp command(Press Enter to pass): ' )
57
+ if not dhcp_cmd :
58
+ dhcp_cmd = ''
43
59
return {
44
60
'username' : username ,
45
61
'password' : password ,
46
- 'ethernet_interface' : dev
62
+ 'ethernet_interface' : dev ,
63
+ 'daemon' : daemon ,
64
+ 'dhcp_command' : dhcp_cmd
47
65
}
48
66
49
67
def enter_interactive_usermanager ():
@@ -80,22 +98,38 @@ def enter_interactive_usermanager():
80
98
else :
81
99
return users_info [choice - 1 ]
82
100
101
+ def start_yah3c (login_info ):
102
+ yah3c = eapauth .EAPAuth (login_info )
103
+ yah3c .serve_forever ()
104
+
83
105
def main ():
84
- # TODO: combine cli args with config
85
106
args = parse_arguments ()
107
+ args = vars (args )
86
108
87
109
# check for root privilege
88
110
if not (os .getuid () == 0 ):
89
111
print (u'亲,要加sudo!' )
90
112
exit (- 1 )
91
113
92
- if len (sys .argv ) == 1 :
93
- # enter interactive mode
94
- login_info = enter_interactive_usermanager ()
114
+ # check if debugging mode enabled
115
+ if args ['debug' ] is True :
116
+ logging .basicConfig (level = logging .DEBUG ,
117
+ format = '%(asctime)s %(levelname)s: %(message)s' ,
118
+ datefmt = '%Y-%m-%d %H:%M:%S' )
119
+ logging .debug ('Debugging mode enabled.' )
120
+ logging .debug (args )
95
121
96
- yah3c = eapauth .EAPAuth (login_info )
97
- yah3c .serve_forever ()
122
+ # if no username specified then enter interactive mode
123
+ if args ['username' ] is None :
124
+ login_info = enter_interactive_usermanager ()
125
+ logging .debug (login_info )
126
+ start_yah3c (login_info )
98
127
128
+ # if there is username, then get it's info
129
+ um = usermgr .UserMgr ()
130
+ login_info = um .get_user_info (args ['username' ])
131
+ logging .debug (login_info )
132
+ start_yah3c (login_info )
99
133
100
134
if __name__ == "__main__" :
101
135
main ()
0 commit comments