Skip to content

Commit 2c569f8

Browse files
committed
Added command line support
1 parent 1bb9b8b commit 2c569f8

File tree

4 files changed

+91
-30
lines changed

4 files changed

+91
-30
lines changed

ChangeLog.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
# YaH3C changelog
22

3+
## 0.5
4+
5+
* add command line argument support
6+
* dhcp after authentication
7+
8+
## 0.4
9+
10+
* remove plugin module
11+
312
## 0.3
413

514
* add eap-md5 support

yah3c/eapauth.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import socket
1111
import os, sys, pwd
12+
from subprocess import call
1213

1314
from colorama import Fore, Style, init
1415
# init() # required in Windows
@@ -105,8 +106,14 @@ def EAP_handler(self, eap_packet):
105106
code, id, eap_len = unpack("!BBH", eap_packet[4:8])
106107
if code == EAP_SUCCESS:
107108
display_prompt(Fore.YELLOW, 'Got EAP Success')
108-
# invoke plugins
109-
daemonize('/dev/null','/tmp/daemon.log','/tmp/daemon.log')
109+
110+
if self.login_info['dhcp_command']:
111+
display_prompt(Fore.YELLOW, 'Obtaining IP Address:')
112+
call([self.login_info['dhcp_command'], self.login_info['ethernet_interface']])
113+
114+
if self.login_info['daemon'] == 'True':
115+
daemonize('/dev/null','/tmp/daemon.log','/tmp/daemon.log')
116+
110117
elif code == EAP_FAILURE:
111118
if (self.has_sent_logoff):
112119
display_prompt(Fore.YELLOW, 'Logoff Successfully!')

yah3c/usermgr.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@ class UserMgr:
1414
"username": "maple",
1515
"password": "valley",
1616
"ethernet_interface": "eth0",
17-
"carry_version_info": true,
18-
"broadcast_logoff": false
17+
"dhcp_command": "dhcpcd",
18+
"daemon": True,
19+
# following has not implemented yet
20+
"carry_version_info": True,
21+
"broadcast_logoff": False
1922
"packet_type": "unicast"
2023
}
2124
"""
@@ -26,6 +29,12 @@ def __init__(self, path=None):
2629
self.users_cfg_path = path
2730
self.config = ConfigParser.ConfigParser()
2831
self.config.read(self.users_cfg_path)
32+
33+
def save_and_reload(self):
34+
fp = open(self.users_cfg_path, 'w')
35+
self.config.write(fp)
36+
fp.close()
37+
self.config.read(self.users_cfg_path)
2938

3039
def get_user_number(self):
3140
return len(self.config.sections())
@@ -40,23 +49,25 @@ def get_all_users_info(self):
4049
return users_info
4150

4251
def get_user_info(self, username):
43-
return dict(self.config.items(username))
52+
user_info = dict(self.config.items(username))
53+
user_info['username'] = username
54+
return user_info
4455

4556
def add_user(self, user_info):
4657
self.config.add_section(user_info['username'])
4758
self.update_user_info(user_info)
4859

4960
def remove_user(self, username):
5061
self.config.remove_section(username)
51-
fp = open(self.users_cfg_path, 'w')
52-
self.config.write(fp)
53-
fp.close()
62+
self.save_and_reload()
5463

5564
def update_user_info(self, user_info):
5665
self.config.set(user_info['username'], 'password',
5766
user_info['password'])
5867
self.config.set(user_info['username'], 'ethernet_interface',
5968
user_info['ethernet_interface'])
60-
fp = open(self.users_cfg_path, 'w')
61-
self.config.write(fp)
62-
fp.close()
69+
self.config.set(user_info['username'], 'dhcp_command',
70+
user_info['dhcp_command'])
71+
self.config.set(user_info['username'], 'daemon',
72+
user_info['daemon'])
73+
self.save_and_reload()

yah3c/yah3c.py

Lines changed: 53 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,34 @@
44
55
"""
66

7-
__version__ = '0.4'
7+
__version__ = '0.5'
88

99
import os, sys
1010
import ConfigParser
1111
import getpass
12-
from socket import socket
12+
import argparse
13+
import logging
1314

1415
import eapauth
1516
import usermgr
16-
import argparse
17+
1718

1819
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')
2021
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')
2833
args = parser.parse_args()
29-
print args
34+
return args
3035

3136
def prompt_user_info():
3237
username = raw_input('Input username: ')
@@ -37,13 +42,26 @@ def prompt_user_info():
3742
break
3843
else:
3944
print 'Password do not match!'
45+
4046
dev = raw_input('Decice(eth0 by default): ')
4147
if not dev:
4248
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 = ''
4359
return {
4460
'username': username,
4561
'password': password,
46-
'ethernet_interface': dev
62+
'ethernet_interface': dev,
63+
'daemon': daemon,
64+
'dhcp_command': dhcp_cmd
4765
}
4866

4967
def enter_interactive_usermanager():
@@ -80,22 +98,38 @@ def enter_interactive_usermanager():
8098
else:
8199
return users_info[choice - 1]
82100

101+
def start_yah3c(login_info):
102+
yah3c = eapauth.EAPAuth(login_info)
103+
yah3c.serve_forever()
104+
83105
def main():
84-
# TODO: combine cli args with config
85106
args = parse_arguments()
107+
args = vars(args)
86108

87109
# check for root privilege
88110
if not (os.getuid() == 0):
89111
print (u'亲,要加sudo!')
90112
exit(-1)
91113

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)
95121

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)
98127

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)
99133

100134
if __name__ == "__main__":
101135
main()

0 commit comments

Comments
 (0)