Coverage for icloudpy/cmdline.py: 62%

128 statements  

« prev     ^ index     » next       coverage.py v7.17.2, created at 2026-11-05 06:42 +0000

1#! /usr/bin/env python # noqa: EXE001 

2""" 

3A Command Line Wrapper to allow easy use of iCloudPy for 

4command line scripts, and related. 

5""" 

6 

7# from builtins import input 

8import argparse 

9import pickle 

10import sys 

11 

12from click import confirm 

13 

14from icloudpy import ICloudPyService, utils 

15from icloudpy.exceptions import ICloudPyFailedLoginException 

16 

17DEVICE_ERROR = "Please use the --device switch to indicate which device to use." 

18 

19 

20def create_pickled_data(idevice, filename): 

21 """ 

22 This helper will output the idevice to a pickled file named 

23 after the passed filename. 

24 

25 This allows the data to be used without resorting to screen / pipe 

26 scrapping. 

27 """ 

28 pickle_file = open(filename, "wb") 

29 pickle.dump(idevice.content, pickle_file, protocol=pickle.HIGHEST_PROTOCOL) 

30 pickle_file.close() 

31 

32 

33def main(args=None): 

34 """Main commandline entrypoint.""" 

35 if args is None: 

36 args = sys.argv[1:] 

37 

38 parser = argparse.ArgumentParser(description="Find My iPhone CommandLine Tool") 

39 

40 parser.add_argument( 

41 "--username", 

42 action="store", 

43 dest="username", 

44 default="", 

45 help="Apple ID to Use", 

46 ) 

47 parser.add_argument( 

48 "--password", 

49 action="store", 

50 dest="password", 

51 default="", 

52 help=("Apple ID Password to Use; if unspecified, password will be fetched from the system keyring."), 

53 ) 

54 parser.add_argument( 

55 "-n", 

56 "--non-interactive", 

57 action="store_false", 

58 dest="interactive", 

59 default=True, 

60 help="Disable interactive prompts.", 

61 ) 

62 parser.add_argument( 

63 "--delete-from-keyring", 

64 action="store_true", 

65 dest="delete_from_keyring", 

66 default=False, 

67 help="Delete stored password in system keyring for this username.", 

68 ) 

69 parser.add_argument( 

70 "--list", 

71 action="store_true", 

72 dest="list", 

73 default=False, 

74 help="Short Listings for Device(s) associated with account", 

75 ) 

76 parser.add_argument( 

77 "--llist", 

78 action="store_true", 

79 dest="longlist", 

80 default=False, 

81 help="Detailed Listings for Device(s) associated with account", 

82 ) 

83 parser.add_argument( 

84 "--locate", 

85 action="store_true", 

86 dest="locate", 

87 default=False, 

88 help="Retrieve Location for the iDevice (non-exclusive).", 

89 ) 

90 

91 # Restrict actions to a specific devices UID / DID 

92 parser.add_argument( 

93 "--device", 

94 action="store", 

95 dest="device_id", 

96 default=False, 

97 help="Only effect this device", 

98 ) 

99 

100 # Trigger Sound Alert 

101 parser.add_argument( 

102 "--sound", 

103 action="store_true", 

104 dest="sound", 

105 default=False, 

106 help="Play a sound on the device", 

107 ) 

108 

109 # Trigger Message w/Sound Alert 

110 parser.add_argument( 

111 "--message", 

112 action="store", 

113 dest="message", 

114 default=False, 

115 help="Optional Text Message to display with a sound", 

116 ) 

117 

118 # Trigger Message (without Sound) Alert 

119 parser.add_argument( 

120 "--silentmessage", 

121 action="store", 

122 dest="silentmessage", 

123 default=False, 

124 help="Optional Text Message to display with no sounds", 

125 ) 

126 

127 # Lost Mode 

128 parser.add_argument( 

129 "--lostmode", 

130 action="store_true", 

131 dest="lostmode", 

132 default=False, 

133 help="Enable Lost mode for the device", 

134 ) 

135 parser.add_argument( 

136 "--lostphone", 

137 action="store", 

138 dest="lost_phone", 

139 default=False, 

140 help="Phone Number allowed to call when lost mode is enabled", 

141 ) 

142 parser.add_argument( 

143 "--lostpassword", 

144 action="store", 

145 dest="lost_password", 

146 default=False, 

147 help="Forcibly active this passcode on the idevice", 

148 ) 

149 parser.add_argument( 

150 "--lostmessage", 

151 action="store", 

152 dest="lost_message", 

153 default="", 

154 help="Forcibly display this message when activating lost mode.", 

155 ) 

156 

157 # Output device data to an pickle file 

158 parser.add_argument( 

159 "--outputfile", 

160 action="store_true", 

161 dest="output_to_file", 

162 default="", 

163 help="Save device data to a file in the current directory.", 

164 ) 

165 

166 # Path to session directory 

167 parser.add_argument( 

168 "--session-directory", 

169 action="store", 

170 dest="session_directory", 

171 default=None, 

172 help="Path to save session information", 

173 ) 

174 

175 # Server region - global or china 

176 parser.add_argument( 

177 "--region", 

178 action="store", 

179 dest="region", 

180 default="global", 

181 help="Server region - global or china", 

182 ) 

183 

184 command_line = parser.parse_args(args) 

185 

186 username = command_line.username 

187 password = command_line.password 

188 session_directory = command_line.session_directory 

189 server_region = command_line.region 

190 

191 if username and command_line.delete_from_keyring: 

192 utils.delete_password_in_keyring(username) 

193 

194 failure_count = 0 

195 while True: 

196 # Which password we use is determined by your username, so we 

197 # do need to check for this first and separately. 

198 if not username: 

199 parser.error("No username supplied") 

200 

201 if not password: 

202 password = utils.get_password( 

203 username, 

204 interactive=command_line.interactive, 

205 ) 

206 

207 if not password: 

208 parser.error("No password supplied") 

209 

210 try: 

211 api = ( 

212 ICloudPyService( 

213 apple_id=username.strip(), 

214 password=password.strip(), 

215 cookie_directory=session_directory, 

216 home_endpoint="https://www.icloud.com.cn", 

217 setup_endpoint="https://setup.icloud.com.cn/setup/ws/1", 

218 ) 

219 if server_region == "china" 

220 else ICloudPyService( 

221 apple_id=username.strip(), 

222 password=password.strip(), 

223 cookie_directory=session_directory, 

224 ) 

225 ) 

226 

227 if ( 

228 not utils.password_exists_in_keyring(username) 

229 and command_line.interactive 

230 and confirm("Save password in keyring?") 

231 ): 

232 utils.store_password_in_keyring(username, password) 

233 

234 if api.requires_2fa: 

235 # Apple's auth flow (2026+) requires an explicit PUT to push 

236 # the code to trusted devices. Without this, the user is 

237 # prompted but no code is ever delivered. 

238 if not api.trigger_2fa_push_notification(): 

239 print( 

240 "(Could not trigger push notification — a code may still arrive via SMS or another path.)", 

241 ) 

242 

243 # fmt: off 

244 print( 

245 "\nTwo-step authentication required.", 

246 "\nPlease enter validation code", 

247 ) 

248 # fmt: on 

249 

250 code = input("(string) --> ") 

251 if not api.validate_2fa_code(code): 

252 print("Failed to verify verification code") 

253 sys.exit(1) 

254 

255 print("") 

256 

257 elif api.requires_2sa: 

258 # fmt: off 

259 print( 

260 "\nTwo-step authentication required.", 

261 "\nYour trusted devices are:", 

262 ) 

263 # fmt: on 

264 

265 devices = api.trusted_devices 

266 for i, device in enumerate(devices): 

267 print( 

268 f" {i}: {device.get('deviceName', 'SMS to ' + device.get('phoneNumber'))}", 

269 ) 

270 

271 print("\nWhich device would you like to use?") 

272 device = int(input("(number) --> ")) 

273 device = devices[device] 

274 if not api.send_verification_code(device): 

275 print("Failed to send verification code") 

276 sys.exit(1) 

277 

278 print("\nPlease enter validation code") 

279 code = input("(string) --> ") 

280 if not api.validate_verification_code(device, code): 

281 print("Failed to verify verification code") 

282 sys.exit(1) 

283 

284 print("") 

285 break 

286 except ICloudPyFailedLoginException as error: 

287 # If they have a stored password; we just used it and 

288 # it did not work; let's delete it if there is one. 

289 if utils.password_exists_in_keyring(username): 

290 utils.delete_password_in_keyring(username) 

291 

292 message = f"Bad username or password for {username}" 

293 password = None 

294 

295 failure_count += 1 

296 if failure_count >= 3: 

297 raise RuntimeError(message) from error 

298 

299 print(message, file=sys.stderr) 

300 

301 for dev in api.devices: 

302 if not command_line.device_id or (command_line.device_id.strip().lower() == dev.content["id"].strip().lower()): 

303 # List device(s) 

304 if command_line.locate: 

305 dev.location() 

306 

307 if command_line.output_to_file: 

308 create_pickled_data( 

309 dev, 

310 filename=(dev.content["name"].strip().lower() + ".fmip_snapshot"), 

311 ) 

312 

313 contents = dev.content 

314 if command_line.longlist: 

315 print("-" * 30) 

316 print(contents["name"]) 

317 for key in contents: 

318 print(f"{key} - {contents[key]}") 

319 elif command_line.list: 

320 print("-" * 30) 

321 print(f"Name - {contents['name']}") 

322 print(f"Display Name - {contents['deviceDisplayName']}") 

323 print(f"Location - {contents['location']}") 

324 print(f"Battery Level - {contents['batteryLevel']}") 

325 print(f"Battery Status- {contents['batteryStatus']}") 

326 print(f"Device Class - {contents['deviceClass']}") 

327 print(f"Device Model - {contents['deviceModel']}") 

328 

329 # Play a Sound on a device 

330 if command_line.sound: 

331 if command_line.device_id: 

332 dev.play_sound() 

333 else: 

334 raise RuntimeError( 

335 f"\n\n\t\tSounds can only be played on a singular device. {DEVICE_ERROR}\n\n", 

336 ) 

337 

338 # Display a Message on the device 

339 if command_line.message: 

340 if command_line.device_id: 

341 dev.display_message( 

342 subject="A Message", 

343 message=command_line.message, 

344 sounds=True, 

345 ) 

346 else: 

347 raise RuntimeError( 

348 f"Messages can only be played on a singular device. {DEVICE_ERROR}", 

349 ) 

350 

351 # Display a Silent Message on the device 

352 if command_line.silentmessage: 

353 if command_line.device_id: 

354 dev.display_message( 

355 subject="A Silent Message", 

356 message=command_line.silentmessage, 

357 sounds=False, 

358 ) 

359 else: 

360 raise RuntimeError( 

361 f"Silent Messages can only be played on a singular device. {DEVICE_ERROR}", 

362 ) 

363 

364 # Enable Lost mode 

365 if command_line.lostmode: 

366 if command_line.device_id: 

367 dev.lost_device( 

368 number=command_line.lost_phone.strip(), 

369 text=command_line.lost_message.strip(), 

370 newpasscode=command_line.lost_password.strip(), 

371 ) 

372 else: 

373 raise RuntimeError( 

374 f"Lost Mode can only be activated on a singular device. {DEVICE_ERROR}", 

375 ) 

376 sys.exit(0) 

377 

378 

379if __name__ == "__main__": 

380 main()