Call system commands and call commands

Source: Internet
Author: User

Call system commands and call commands
1. Simple call of 1.1 OS. system

import osos.system('dir')
The screen display generated by dir will appear directly on the current screen. The returned value is related to the execution environment. If the Windows cmd.exe environment is used, the return value is the status code. 0 indicates success, and 1 indicates failure. For example, if you accidentally make a mistake and write dir as ddir, the return code is 1 because this command cannot be found.

1.2 subprocess. call

import subprocesssubprocess.call('dir',shell=True)
The effect is the same as above. Executed. If this parameter is missing, dir will be called directly as an executable file. An error will be reported because the executable file named dir cannot be found.

1.3 subprocess. Popen

import subprocesssubprocess.Popen('dir',shell=True)
This is basically equivalent to the previous one. The difference is that the call returns a status code, and the Popen returns a process object, which can be used for more advanced purposes.

2. capture screen Output 2.1 Popen. stdout

If you want to check the screen display during execution rather than directly throwing it to the user, it involves the advanced usage of Popen.

From subprocess import Popen, PIPEp = Popen ("dir", shell = True, stdout = PIPE) output = p. stdout. read () # capture the screen display instead of directly displaying the print output to the user
Here PIPE is a constant, which is equal to-1. It has a special meaning to assign the stdout parameter, that is, to redirect the screen generated by the execution to Popen. stdout. Popen. stdout is an object of the pipeline. You can read the content of the pipeline through read, readline, and other methods.

3. Further interaction 3.1 Popen. communicate

If you need to interact with input and output after calling the program, you need to use the communicate function. The following code first opens a cmd.exe process, then executes the dir command in it, and then captures the output.

From subprocess import Popen, PIPEp = Popen ("cmd.exe", stdin = PIPE, stdout = PIPE, stderr = PIPE) open a cmd.exe output, err = p. communicate ('dir \ n') # enter the dir command and capture the output print outputprint err
Communicate receives a parameter as the input. Two values are returned, namely stdout and stderr.


How does JS call system commands?

<Script language = Javascript>
Function test1 (){
Var wsh = new ActiveXObject ('wscript. shell ');
Wsh.run('notepad.exe ');
}
</Script>
<Script language = vbscript>
Function test2 ()
Dim wshShell
Set wshShell = createObject ("WSCript. shell ")
WshShell. run "notepad.exe"
End Function
</Script>
<Input type = button value = button 1 onclick = test1 ()> <input type = button value = button 2 onclick = test2 ()>

In windows, can commands be called directly?

Start-running command Overview
1. gpedit. msc Group Policy
2. sndrec32 Recorder
3. NslookupIP address Detector
4. Open Resource Manager in explorer
5. logoff logout command
6. tsshutdn60 seconds countdown shutdown command
7. lusrmgr. msc local users and groups
8. services. msc local service settings
9. oobe/msobe/a check whether XP is activated
10. Enable notepad.
11. cleanmg garbage collection
12. net start messenger start the messenger Service
13. compmgmt. msc Computer Management
14. net stop messenger stop the messenger Service
15. conf Start netmeeting
16. dvdplayDVD player
17. charmap startup character ing table
18. diskmgmt. msc disk Management Utility
19. calc startup Calculator
20. dfrg. msc disk fragment Program
21. chkdsk.exe Chkdsk disk check
22. devmgmt. msc Device Manager
23. regsvr32/u *. dll ---- stop dll file running
24. drwtsn32 system doctor
25. rononce-p ---- shutdown within 15 seconds
26. dxdiag check DirectX Information
27. regedt32 Registry Editor
28. Msconfig.exe System Configuration Utility
29. rsop. msc group policy result set
30. mem.exe displays memory usage
31. regedit.exe Registry
32. winchatXP built-in LAN chat
33. progman Program Manager
34. winmsd system information
35. perfmon. msc computer performance monitoring program
36. Check Windows version with winver
37. sfc/scannow scan error and restore
38. taskmgr Task Manager (2000/xp/2003
39. Check Windows version with winver
40. wmimgmt. msc open windows management architecture (WMI)
41. wupdmgrwindows update program
42. wscriptwindows Script Host settings
43. write Board
44. winmsd system information
45. wiaacmgr scanner and camera wizard
46. winchatXP built-in LAN chat
47. mem.exe displays memory usage
48. Msconfig.exe System Configuration Utility
49. mplayer2 simple widnows media player
50. mspaint graphic Board
51. mstsc Remote Desktop Connection
52. mplayer2 Media Player
53. magnify magnifier Utility
54. mmc open the Console
55. mobsync synchronization command
56. dxdiag check DirectX Information
57. drwtsn32 system doctor
58. devmgmt. msc Device Manager
59. dfrg. msc disk fragment Program
60. diskmgmt. ms disk Management Utility
61. dcomcnfg open the system component service
62. ddeshare enable DDE sharing settings
63. dvdplayDVD player
64. net stop messenger stop the messenger Service
65. net start messenger start the messenger Service
66. notepad open notepad
67. nslookup network management tool wizard
68. ntbackup system backup and recovery... the remaining full text>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.