Thread: Cmd and Python

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    16
    Rep Power
    0

    Cmd and Python


    I'm getting into python, and I had a nice idea for a program to use at my school. As most of you probably did at High school, you can shut down any pc on the network with cmd. I would want to make a graphical interface for this. It would work the following:

    -Use net view to see which pc's are active, then try to find which is the user who's logged in.
    -Mark with python on a school map which pc's are active and who's connected with information about them (name, class, etc.)
    -Have an option to send messages with the net send command.
    -Execute the shutdown command, with the possibility of including a comment and designate a timer.
    -Display Net statistics

    Now my question is the following, can I execute cmd commands through python and also read/use the data received in python?
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2005
    Location
    U.S A.Z.
    Posts
    24
    Rep Power
    0
    You can get into serious trouble with your school and the law for doing this. But you can run system commands by using the os module. I highly suggest that you use this information for something more constructive.

    Comments on this post

    • leeuw01 agrees : A thank you for your help
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    16
    Rep Power
    0
    Originally Posted by knutrainer
    You can get into serious trouble with your school and the law for doing this. But you can run system commands by using the os module. I highly suggest that you use this information for something more constructive.
    Don't worry, my school isn't like that.

    Thanks, I found the list which consist of the functions in the os module, could you give me a hint where to start? (I'm thinking about the os.system(command) function, but can't get my head around it)
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2005
    Location
    U.S A.Z.
    Posts
    24
    Rep Power
    0
    try putting quotes are around the command like this.
    Code:
    os.system("ping google.com")
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    16
    Rep Power
    0
    Originally Posted by knutrainer
    try putting quotes are around the command like this.
    Code:
    os.system("ping google.com")
    Hmm, I didn't think about that
    It works perfectly now, I've only got one last quetion left.
    When I use the net view command a list of computers connected to a network is printed. How do I directly create a list with these names?
  10. #6
  11. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,892
    Rep Power
    481
    Your particular application may be better suited to a shell scripting language than to python.

    Python's a poor language choice if your program looks like

    import os
    os.system('this')
    os.system('that')
    os.system('etceteras')
    [code]Code tags[/code] are essential for python code and Makefiles!
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    16
    Rep Power
    0
    Originally Posted by b49P23TIvg
    Your particular application may be better suited to a shell scripting language than to python.

    Python's a poor language choice if your program looks like

    import os
    os.system('this')
    os.system('that')
    os.system('etceteras')
    Probally there is a better way but we use python alot at school, and I know my way around it. That's why I wanted to use it for this project. The example you showed me only consisted about excecuting system commands, I would like to know how to interpret the information given by it. Example after net view a list like this prints itself:

    \\PC-INFO-01
    \\PC-INFO-02
    \\PC-INFO-03
    \\PC-SVT-01
    \\PC-SVT-02

    How would I instead of printing it place it into a list. Ex: PClist["\\PC-INFO-01","\\PC-INFO-02", etc..]
  14. #8
  15. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,892
    Rep Power
    481
    A=subprocess.check_output('theCommandYouWouldHaveUsedFor_os_dot_system',shell=True).split('\n')

    warning, the list is a list of bytes objects (python 2) which you may have to mess with.
    [code]Code tags[/code] are essential for python code and Makefiles!
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    16
    Rep Power
    0
    Originally Posted by b49P23TIvg
    A=subprocess.check_output('theCommandYouWouldHaveUsedFor_os_dot_system',shell=True).split('\n')

    warning, the list is a list of bytes objects (python 2) which you may have to mess with.
    I'll try to figure something out. Thanks for the help

IMN logo majestic logo threadwatch logo seochat tools logo