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

    Join Date
    Nov 2012
    Rep Power

    Code that will write into cmd

    I am new to programming and I would like to create a program that will open cmd and then run a few commands. Using python 2.7 can i create a program that will open cmd (already done) and run a few commands like ipconfig, ping and a few others without me typing in the cmd window?
  2. #2
  3. Commie Mutant Traitor
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2004
    Alpharetta, GA
    Rep Power
    If you only need to run one program, and don't need to capture the program's output, you can use os.system():
    import os
    os.system("ping nytimes.com")
    However, you would have much better results overall using the subprocess module:
    import subprocess
    subprocess.call("ping nytimes.com")  # same as os.system()
    # capture the called program's output
    p = subprocess.Popen("ping nytimes.com", stdout=subprocess.PIPE)
    # get just the first line of output from 'ping'
    lines = p.stdout.read().splitlines()
    firstline = lines[1]
    # extract the IP address from the ping trace
    startpos = firstline.find('[')
    endpos = firstline.find(']')
    print firstline[startpos + 1:endpos]
    Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF
    #define KINSEY (rand() % 7) λ Scheme is the Red Pill
    Scheme in Short Understanding the C/C++ Preprocessor
    Taming Python A Highly Opinionated Review of Programming Languages for the Novice, v1.1

    FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. $4500 obo. tverres@et.ins.gov

IMN logo majestic logo threadwatch logo seochat tools logo