CMD Commands?

Discuss how to write good code, break bad code, your current pet projects, or the best way to approach novel problems

CMD Commands?

Post by xTractatorix on Tue Jun 26, 2012 5:45 pm
([msg=67541]see CMD Commands?[/msg])

Allright so to make a long story short im interested in making C programs that execute "CMD commands."Now, my question is, shouldnt i just learn CMD commands. What im saying is, do i really have to learn anthing else in C it self to communicate with the OS than the system() function?I hope im not confusing anyone with my rambling questions... :D
xTractatorix
Experienced User
Experienced User
 
Posts: 61
Joined: Sun May 13, 2012 8:42 am
Blog: View Blog (0)


Re: CMD Commands?

Post by shaqywacky on Tue Jun 26, 2012 6:08 pm
([msg=67542]see Re: CMD Commands?[/msg])

Well, if all you want to do is run commands for the command prompt then you should just use a batch file. Then you wouldn't have to compile all that VERY unnecessary C code(I guess this could be useful if you wanted an executable file).

do i really have to learn anthing else in C it self to communicate with the OS than the system()

This will only allow you to interact with the command prompt, which is very limited. I'm not sure I know what you mean by "communicate with the OS". The command prompt is very limited and can't really do all that much. The main way to use the OS would be through calling API functions and interrupts. This is much more powerful and can do much more than the command prompt.

Maybe given an example of what you would like to do.
shaqywacky
Experienced User
Experienced User
 
Posts: 85
Joined: Thu Jul 03, 2008 2:26 pm
Blog: View Blog (0)


Re: CMD Commands?

Post by xTractatorix on Tue Jun 26, 2012 7:53 pm
([msg=67544]see Re: CMD Commands?[/msg])

shaqywacky wrote:This will only allow you to interact with the command prompt, which is very limited. I'm not sure I know what you mean by "communicate with the OS". The command prompt is very limited and can't really do all that much. The main way to use the OS would be through calling API functions and interrupts. This is much more powerful and can do much more than the command prompt.

Maybe given an example of what you would like to do.


Will do, lets say i want to make a program that gets the IP adress of the computer and then sends it to me. That would be a very simple example, but programs that resemble worms are interesting me at the moment.
xTractatorix
Experienced User
Experienced User
 
Posts: 61
Joined: Sun May 13, 2012 8:42 am
Blog: View Blog (0)


Re: CMD Commands?

Post by shaqywacky on Tue Jun 26, 2012 8:19 pm
([msg=67545]see Re: CMD Commands?[/msg])

OK, that's a good example. There are two ways to do this which lend perfectly to this topic.

The first way uses the command prompt and the second would use API functions.

Using the CMD commands(with C) you first use 'ipconfig' and you would get an output similar to this:
Code: Select all
Windows IP Configuration


Ethernet adapter Bluetooth Network Connection:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :

Ethernet adapter Local Area Connection:

   Connection-specific DNS Suffix  . : gateway.2wire.net
   Link-local IPv6 Address . . . . . : fe80::a5ee:dbcb:33c0:269d%10
   IPv4 Address. . . . . . . . . . . : 192.168.1.100
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : 192.168.1.254

Ethernet adapter VMware Network Adapter VMnet1:

   Connection-specific DNS Suffix  . :
   Link-local IPv6 Address . . . . . : fe80::e053:77fb:5caa:f957%19
   IPv4 Address. . . . . . . . . . . : 192.168.126.1
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . :

Ethernet adapter VMware Network Adapter VMnet8:

   Connection-specific DNS Suffix  . :
   Link-local IPv6 Address . . . . . : fe80::c023:abba:dedd:e753%20
   IPv4 Address. . . . . . . . . . . : 192.168.101.1
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . :

Tunnel adapter isatap.{2AD4D38A-3F90-4C77-BEEE-E51678FE41D6}:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :

Tunnel adapter Teredo Tunneling Pseudo-Interface:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :

Tunnel adapter isatap.gateway.2wire.net:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . : gateway.2wire.net

Tunnel adapter isatap.{8EEF41DB-AD38-4F9F-8BDA-3C49701F1CE3}:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :

Tunnel adapter isatap.{1CB82128-B7C2-4FFD-8CA8-D2752F865A5C}:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :

That's my output of ipconfig so yours may be a little different. Assuming you only want the first IP address found(there can be multiple IPs for one computer). You would take that output and search for the term 'IPv4 Address' and then use some string manipulation to get the IP at the end. While this solution can get the IP address(although it could have many problems), it is pretty much limited to that.

The way using API functions would be conceptually simpler but probably more difficult at first. The only thing you would have to do is call the necessary API function that can give you the IP address. It would probably give it to you in a structure but it would be trivial to extract it. The reason this may be more difficult at first is because, at least in my experience, the required libraries aren't always standardized and the compiler options can be very specific.

I suggest you do both ways because both will give you very good experience, if you don't feel like doing both, I would recommend the API way because the CMD example can't really do much else. But the API example could then, using APIs, connect to different computers.

I don't think the command prompt can call APIs but if it can, I can guarantee it is much better and easier with C.

One final thing. C network programming can be pretty difficult when you first start. I would REALLY suggest you have a good grasp on the language or be prepared to become very frustrated.
shaqywacky
Experienced User
Experienced User
 
Posts: 85
Joined: Thu Jul 03, 2008 2:26 pm
Blog: View Blog (0)


Re: CMD Commands?

Post by xTractatorix on Wed Jun 27, 2012 11:00 am
([msg=67564]see Re: CMD Commands?[/msg])

shaqywacky wrote:One final thing. C network programming can be pretty difficult when you first start. I would REALLY suggest you have a good grasp on the language or be prepared to become very frustrated.

I am relatively new to programing in general, with around 4 months of experience over all. I started with FreeBASIC tried it out and made a couple calculators, then moved to C++ for around a month and then finally went to C. Ive only been coding in C for around 2 months, although i will say i am a very fast learner. With that being said, i don't want to skip anything crucial or important in the C language, and especially going into something like C Network Programing while having a hole in my knowledge of C. I am comfortable with if statements, logical operotors{ || and && }, i can manipulate strings,mathematical operators{ *, /, +, -, =, ==}. and i know who to read, write and edit files using C. I don't believe i have missed anything, that is all i know at the moment in C, should i move up to C networking and API, or am i missing somenthing? Oh and one last thing ive been noticing that for more technical things in C its becoming much harder to find usefull websites on these subjects, could you link a good website, or video for things C related. Thankyou :D
xTractatorix
Experienced User
Experienced User
 
Posts: 61
Joined: Sun May 13, 2012 8:42 am
Blog: View Blog (0)


Re: CMD Commands?

Post by mShred on Thu Jun 28, 2012 3:41 pm
([msg=67596]see Re: CMD Commands?[/msg])

Firstly... Using ipconfig won't give you the IP address that you're looking for. It'll only give you the internal IP, the one that your home network uses to get around. You won't see your external IP address doing it that way. The external is the one that you're probably going to be looking for; it's the one that other computers will use to talk to it.
Secondly, if you were just gonna do it that way, you could just output the ipconfig results, and use your string manipulation shizz to get the IP like shaqywacky said. Then you could use that as the input for your C program. But that is a long tedious way, which could help you learn both batch and C.
The funner, and maybe even more tedious, way would to add in a web server in there somewhere. You could set up a little apache PHP page on a box somewhere and have your C program on another box visit it. Use your PHP skills to have it output their IP. I think it's something like "echo $_SERVER['REMOTE_ADDR'];" IIRC. Then have your C program use that IP as input. No batch needed, just like a true virus. But in the end, it's all redundant considering you would have to know the IP to have a program on it. The only way I could see someone using this is if they had a some sort of worm that infects shit tons of computers. Each infected computer would report back to your box so you could have their IP for when you need to give them back commands for malicious stuff, say a botnet or something of the like.
Image

For those about to rock.
User avatar
mShred
Administrator
Administrator
 
Posts: 1713
Joined: Tue Jun 22, 2010 4:22 pm
Blog: View Blog (2)


Re: CMD Commands?

Post by shaqywacky on Thu Jun 28, 2012 5:16 pm
([msg=67605]see Re: CMD Commands?[/msg])

Ah, yes. I misunderstood the question. I thought you meant that would just tell you the IP address of the machine you're on.
shaqywacky
Experienced User
Experienced User
 
Posts: 85
Joined: Thu Jul 03, 2008 2:26 pm
Blog: View Blog (0)


Re: CMD Commands?

Post by LoGiCaL__ on Thu Jun 28, 2012 5:51 pm
([msg=67613]see Re: CMD Commands?[/msg])

If you used linux you could also try:

Code: Select all
wget www.showmyip.me


and then include in the program a function/method to parse out the ip address in the returned index.html. That is assuming the pc wouldn't be going through any proxies. I've seen programs with wget for windows so I guess it would be possible to do with CMD.
User avatar
LoGiCaL__
Addict
Addict
 
Posts: 1060
Joined: Sun May 30, 2010 12:33 pm
Blog: View Blog (0)


Re: CMD Commands?

Post by mShred on Thu Jun 28, 2012 9:56 pm
([msg=67627]see Re: CMD Commands?[/msg])

Psh... I guess no one likes to do things the fun way anymore..
Image

For those about to rock.
User avatar
mShred
Administrator
Administrator
 
Posts: 1713
Joined: Tue Jun 22, 2010 4:22 pm
Blog: View Blog (2)


Re: CMD Commands?

Post by ghost107 on Fri Jun 29, 2012 4:38 am
([msg=67632]see Re: CMD Commands?[/msg])

Using CMD commands in C is pretty easily just look at the system function or the exec family:
http://www.gnu.org/software/libc/manual ... ing-a-File
http://en.cppreference.com/w/c/program/system

About what you want to do:
I suggest after finishing with C, you should start with c++, after finishing C++, you should look on the documentation of Data Structures and algorithms(most of this are implemented in c++, but its best to know how it works, you can read about this after finishing pointers and structs), then you should read something about Design Patterns and Antipatterns.

Meanwhile you can read about network programming too.
ghost107
Poster
Poster
 
Posts: 132
Joined: Wed Jul 02, 2008 7:57 am
Blog: View Blog (0)



Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests