Command Line in Python

For the discussion of Perl, Python, Ruby, and PHP and other interpreted languages.

Command Line in Python

Post by Draymire on Thu Sep 09, 2010 9:28 pm
([msg=45357]see Command Line in Python[/msg])

My school doesn't allow access to the command line, however I found that i can use batch files with no problem (as long as they end in pause). That got me thinking about an easier way to navigate through the computer without rewriting my batch file every time. That is where this program comes in. What do you guys think about this little project, what have I missed in the coding that may need to be fixed or added.

Code: Select all
#Windows command line access with python
import os

location=os.path.abspath('test.py')#Where is the file being run from
location=location.replace('\\test.py','')#remove the files name

while True:
   temp=raw_input(location+'>')

   temp=temp.replace('lolcat', location)#quick add in for current directory
   message=temp
   
   if temp=='Exit' or temp=='exit':#exit
      break
   
   if temp=='dir' or temp=='ls':#yay added ls
      message='dir \"'+location+'\"'
     
   if temp.find('cd')!=(-1):
      temp=temp.replace('cd ','')
      if temp.find('..')!=(-1):
         if temp=='..':#move back a directory(this works)
            temp3=location.split('\\')
            location=''
            for i in range(len(temp3)-1):
               location+=temp3[i]+'\\'
            location=location[:len(location)-1]
         else:#Move back multiple directories(currently doesn't work...Working on it)
            temp2=temp.count('..\\')
            temp2+=1
            temp3=location.split('\\')
            location=''
            for i in range(len(temp3)-temp2):
               location+=temp3[i]+'\\'
            location=location[:len(location)-1]
      else:
         location+='\\'+temp
           
   os.system(message)#use the command
   

Python 2.6.*
Best results running program not in IDLE
Baba Ram Dass "The quieter you become, the more you can hear"
User avatar
Draymire
Poster
Poster
 
Posts: 129
Joined: Sun Nov 22, 2009 12:01 am
Blog: View Blog (0)


Re: Command Line in Python

Post by centip3de on Fri Sep 10, 2010 4:41 pm
([msg=45419]see Re: Command Line in Python[/msg])

Draymire wrote:My school doesn't allow access to the command line


Do you know if they have python installed? >.>
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rick Cook
User avatar
centip3de
Moderator
Moderator
 
Posts: 1412
Joined: Fri Aug 20, 2010 5:46 pm
Blog: View Blog (0)


Re: Command Line in Python

Post by Goatboy on Fri Sep 10, 2010 7:53 pm
([msg=45433]see Re: Command Line in Python[/msg])

Just set your batch file to run "cmd".
Assume that everything I say is or could be a lie.
1UHQ15HqBRZFykqx7mKHpYroxanLjJcUk
User avatar
Goatboy
Expert
Expert
 
Posts: 2782
Joined: Mon Jul 07, 2008 9:35 pm
Blog: View Blog (0)


Re: Command Line in Python

Post by Draymire on Sat Sep 11, 2010 3:45 pm
([msg=45529]see Re: Command Line in Python[/msg])

@ centip3de: Yes they have python installed, that is the language they teach at my school, plus i can install anything i want to my "H:\ drive"(space on the server).

@Goatboy: When i set the batch file to run cmd it pops up your system admistrator has disabled the use of command prompt and then exits. Which is why i have to use pause after commands in batch if i want to actually see the output. Task Manager was also disabled this way but know i can use the tasklist and taskkill commands to take care of that.

Any thoughts on the code, i have improved upon it a bit but don't have time to edit that right now.
Baba Ram Dass "The quieter you become, the more you can hear"
User avatar
Draymire
Poster
Poster
 
Posts: 129
Joined: Sun Nov 22, 2009 12:01 am
Blog: View Blog (0)


Re: Command Line in Python

Post by mojo1948 on Sat Sep 11, 2010 8:04 pm
([msg=45544]see Re: Command Line in Python[/msg])

Have you tried copying cmd to a new folder or drive and renaming it. It might work, depends on how they are filtering it.
Never stop learning.
User avatar
mojo1948
Experienced User
Experienced User
 
Posts: 60
Joined: Sun Jul 18, 2010 5:45 am
Blog: View Blog (0)


Re: Command Line in Python

Post by Draymire on Sat Sep 11, 2010 11:04 pm
([msg=45550]see Re: Command Line in Python[/msg])

The big problem with doing that is i can not access C:\ through normal methods because it has been restricted so i would have to use batch files to do it. Plus i like my program because it allows me to easily add things like ls or pwd which makes the command line easier to use.
Baba Ram Dass "The quieter you become, the more you can hear"
User avatar
Draymire
Poster
Poster
 
Posts: 129
Joined: Sun Nov 22, 2009 12:01 am
Blog: View Blog (0)


Re: Command Line in Python

Post by centip3de on Sun Sep 12, 2010 1:24 am
([msg=45554]see Re: Command Line in Python[/msg])

Well I really do wish I could help you out more, but unfortunetly I'm not sure all about the in's and out's of your school's system, it seems to be amazingly different than us Washington folks have to deal with. (Vnc up all the time so the admins can monitor us) :/ Do tell if you find your solution!
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rick Cook
User avatar
centip3de
Moderator
Moderator
 
Posts: 1412
Joined: Fri Aug 20, 2010 5:46 pm
Blog: View Blog (0)


Re: Command Line in Python

Post by Draymire on Sun Sep 12, 2010 10:56 am
([msg=45573]see Re: Command Line in Python[/msg])

Really all i was hoping for was some criticism on my code. It is nice thought that you would point out other things to try. As for a solution for some of the things i have run into:

The School uses NetSupport to monitor your screens, etc... through some searching on the c:\ drive via my command line program i was able to associate some processes to NetSupport(client32.exe and gateway.exe) by killing those i remove the teachers ability to monitor what i am doing without getting up(lazy teachers).

The School also uses the program USBDLM to mount all flash drives to X,Y,Z. Drives X,Y,Z have all lost the ability to use executable files of any type, so again using my program i was able to locate the program and have it mount flash drives to my choice of M,N,O which is allowed to use executable.
Baba Ram Dass "The quieter you become, the more you can hear"
User avatar
Draymire
Poster
Poster
 
Posts: 129
Joined: Sun Nov 22, 2009 12:01 am
Blog: View Blog (0)


Re: Command Line in Python

Post by the0nlyb0ss on Mon Sep 20, 2010 5:40 pm
([msg=46215]see Re: Command Line in Python[/msg])

It seems you're over-complicating things, correct me if I'm wrong, but this seems like a much easier approach:
Code: Select all
import os, subprocess

while True:
   current_dir = os.getcwd()
   commands = input(current_dir + '>')
   if commands.upper() == 'EXIT':
      break
   if commands.find('cd') != -1:
      commands = commands.replace('cd ', '')
      os.chdir(commands)
   else:
      data = subprocess.Popen(commands, shell=True, stdout=subprocess.PIPE).communicate()
      data = data[0]
      data = data.decode('utf-8')
      print(data)

Granted the only problem with this is it only returns the output AFTER the command has completed, so things like tree would take a long time to display.
EDIT: This is in Python 3.1
"Knowledge is knowing that a tomato is a fruit, but Wisdom is knowing not to put it in a fruit salad."
User avatar
the0nlyb0ss
Experienced User
Experienced User
 
Posts: 54
Joined: Thu Sep 02, 2010 11:24 pm
Location: California
Blog: View Blog (0)


Re: Command Line in Python

Post by Draymire on Fri Sep 24, 2010 12:47 pm
([msg=46424]see Re: Command Line in Python[/msg])

I probably have over complicated things, and i am still only using 2.6 so that might be a bit of a problem. Anyways since then i have added in more things like cp to shorten xcopy and pwd instead of lolcat(makes a bit more sense that way). It is fun to play with so i might look into your code and see what i could do with it, but i am also looking to learn c and implement the program in it.

Off-Topic:
No Internet at home really sucks especially when HTS is blocked at school.
Baba Ram Dass "The quieter you become, the more you can hear"
User avatar
Draymire
Poster
Poster
 
Posts: 129
Joined: Sun Nov 22, 2009 12:01 am
Blog: View Blog (0)


Next

Return to Interpreted Languages

Who is online

Users browsing this forum: No registered users and 0 guests

cron