]>
git.proxmox.com Git - mirror_ifupdown2.git/blob - sbin/ifupdown
7 from ifupdown
.ifupdownmain
import *
11 lockfile
="/run/network/.lock"
15 logger
.debug('args = %s' %str
(args
))
19 if len(args
.iflist
) == 0:
22 cachearg
=False if iflist
is not None or args
.nocache
== True else True
23 logger
.debug('creating ifupdown object ..')
24 if op
== 'up' or op
== 'down' or op
== 'reload':
25 ifupdown_handle
= ifupdownMain(force
=args
.force
,
26 nodepends
=args
.nodepends
,
27 perfmode
=args
.perfmode
,
32 ifupdown_handle
= ifupdownMain(nodepends
=args
.nodepends
,
33 perfmode
=args
.perfmode
,
38 logger
.debug('calling %s' %op
+ ' for all interfaces ..')
40 ifupdown_handle
.up(args
.all
, args
.CLASS
, iflist
,
41 excludepats
=args
.excludepats
,
42 printdependency
=args
.printdependency
)
44 ifupdown_handle
.down(args
.all
, args
.CLASS
, iflist
,
45 excludepats
=args
.excludepats
)
47 if args
.checkcurstate
== True:
48 qtype
='query-checkcurr'
49 elif args
.presumedstate
== True:
50 qtype
='query-presumed'
51 elif args
.presumedstatedetailed
== True:
52 qtype
='query-presumeddetailed'
53 elif args
.runningstate
== True:
55 iflist
= [i
for i
in os
.listdir('/sys/class/net/')
56 if os
.path
.isdir('/sys/class/net/%s' %i) == True]
62 ifupdown_handle
.query(qtype
, args
.all
, args
.CLASS
, iflist
,
63 excludepats
=args
.excludepats
)
65 if iflist
is not None:
66 raise Exception('iflist is currently not supported with reload')
68 ifupdown_handle
.reload(args
.all
, args
.CLASS
, iflist
,
69 excludepats
=args
.excludepats
)
76 log_level
= logging
.WARNING
78 if args
.verbose
== True:
79 log_level
= logging
.INFO
81 if args
.debug
== True:
82 log_level
= logging
.DEBUG
85 logging
.basicConfig(level
=log_level
,
86 format
='%(levelname)s: %(message)s')
87 logger
= logging
.getLogger('ifupdown')
95 def update_argparser(argparser
):
97 argparser
.add_argument('iflist', metavar
='IFACE',
98 nargs
='*', help='interfaces list')
99 argparser
.add_argument('-v', '--verbose', dest
='verbose',
100 action
='store_true', help='verbose')
101 argparser
.add_argument('-d', '--debug', dest
='debug',
103 help='output debug info')
104 argparser
.add_argument('-q', '--quiet', dest
='quiet',
106 help=argparse
.SUPPRESS
)
107 argparser
.add_argument('--allow', dest
='CLASS',
108 help='ignore non-\"allow-CLASS\" interfaces')
109 argparser
.add_argument('--nodepends', dest
='nodepends',
110 action
='store_true', help=argparse
.SUPPRESS
)
111 argparser
.add_argument('--perfmode', dest
='perfmode',
112 action
='store_true', help=argparse
.SUPPRESS
)
113 argparser
.add_argument('-j', '--jobs', dest
='jobs', type=int,
114 default
=-1, choices
=range(1,12), help=argparse
.SUPPRESS
)
115 argparser
.add_argument('--nocache', dest
='nocache', action
='store_true',
116 help=argparse
.SUPPRESS
)
117 argparser
.add_argument('--print-dependency',
118 dest
='printdependency', choices
=['list', 'dot'],
119 help=argparse
.SUPPRESS
)
120 argparser
.add_argument('-X', '--exclude', dest
='excludepats',
121 action
='append', help='exclude interfaces from the list of '
122 + 'interfaces to operate on by a PATTERN '
123 + '(note that this option doesn\'t disable mappings)')
126 def update_ifupdown_argparser(argparser
):
127 argparser
.add_argument('-a', '--all', action
='store_true',
128 help='process all interfaces marked \"auto\"')
129 argparser
.add_argument('-f', '--force', dest
='force',
131 help='force run all operations')
132 argparser
.add_argument('-n', '--no-act', dest
='noact',
133 action
='store_true', help='print out what would happen,' +
136 def update_ifup_argparser(argparser
):
137 update_ifupdown_argparser(argparser
)
139 def update_ifdown_argparser(argparser
):
140 update_ifupdown_argparser(argparser
)
142 def update_ifquery_argparser(argparser
):
143 argparser
.add_argument('-l', '--list', action
='store_true', dest
='all',
144 help='process all interfaces marked \"auto\"')
145 group
= argparser
.add_mutually_exclusive_group(required
=False)
146 group
.add_argument('-r', '--running-state', dest
='runningstate',
148 help='query running state of an interface')
150 group
.add_argument('-c', '--check-state', dest
='checkcurstate',
152 help='check running state of an interface')
154 # presumed-state is state maintained by ifupdown
155 group
.add_argument('--presumed-state', dest
='presumedstate',
156 action
='store_true', help=argparse
.SUPPRESS
)
158 # presumed-state-detailed prints all details about the object stored
160 group
.add_argument('--presumed-state-detailed',
161 dest
='presumedstatedetailed',
162 action
='store_true', help=argparse
.SUPPRESS
)
164 group
.add_argument('--format', dest
='format', default
='nwifaces',
165 choices
=['nwifaces', 'json'], help=argparse
.SUPPRESS
)
167 def update_ifreload_argparser(argparser
):
168 update_ifupdown_argparser(argparser
)
170 def parse_args(argsv
, op
):
171 descr
= 'interface management'
173 argparser
= argparse
.ArgumentParser(description
=descr
)
174 update_argparser(argparser
)
176 update_ifup_argparser(argparser
)
178 update_ifdown_argparser(argparser
)
180 update_ifquery_argparser(argparser
)
182 update_ifreload_argparser(argparser
)
184 return argparser
.parse_args(argsv
)
187 """ main function """
190 if re
.search(r
'ifup', argv
[0]) != None:
192 elif re
.search(r
'ifdown', argv
[0]) != None:
194 elif re
.search(r
'ifquery', argv
[0]) != None:
196 elif re
.search(r
'ifreload', argv
[0]) != None:
199 print ('Unexpected executable.' +
200 ' Should be \'ifup\' or \'ifdown\' or \'ifquery\'')
203 # Command line arg parser
204 args
= parse_args(argv
[1:], op
)
205 if len(args
.iflist
) > 0 and args
.all
is True:
206 print 'interface list cannot be specified with all option'
215 if args
.debug
== True:
219 print '\nRerun the command with \'-d\' for a detailed errormsg'
224 if __name__
== "__main__":
226 if not os
.geteuid() == 0:
227 print 'Error: Must be root to run this command'
231 XXX: Cannot use this. A spawned dhclient process can hold the lock
232 if not utilities.lockFile(lockfile):
233 print 'Another instance of this program is already running.'