main.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #pragma warning(disable:4786)
  14. #include <list>
  15. #include <iostream>
  16. #include <fstream>
  17. #include "jlib.hpp"
  18. #include "winremote.hpp"
  19. #include "jmutex.hpp"
  20. #include "jexcept.hpp"
  21. #include "jmisc.hpp"
  22. #include <process.h>
  23. using namespace std;
  24. void usage()
  25. {
  26. printf("Usage: psexec (\\\\computer | -mFile) -uUser -pPassword [-cfd] [-h dir] cmd [arguments]\n");
  27. printf(" -m Specifies file with machine addresses (one per line).\n");
  28. printf(" -u Specifies user name for login to remote computer.\n");
  29. printf(" -p Specifies password for user name.\n");
  30. printf(" -c Copy the specified program to the remote system for execution.\n");
  31. printf(" -f Always copy the specified program to the remote system.\n");
  32. printf(" -d Do not wait for the process.\n");
  33. printf(" -n Do not send user/password, the command will not be able to access network.\n");
  34. printf(" -h Home directory for the process on the remote system.\n");
  35. printf(" cmd Name of application to execute.\n");
  36. printf(" arguments Arguments to pass .\n");
  37. exit(-2);
  38. }
  39. Owned<IRemoteAgent> ragent(0);
  40. bool ControlHandler()
  41. {
  42. if (ragent)
  43. {
  44. ragent->stop();
  45. }
  46. return false;
  47. }
  48. int main(int argc, char** argv)
  49. {
  50. try
  51. {
  52. ragent.setown(createRemoteAgent());
  53. std::list<std::string> remote;
  54. StringAttr user,password;
  55. Owned<IRemoteCommand> rcmd=createRemoteCommand();
  56. bool nowait=false, network=true;
  57. if (argc<2)
  58. usage();
  59. addAbortHandler(ControlHandler);
  60. int i=1;
  61. if(argv[i][0]=='\\' && argv[i][1]=='\\')
  62. {
  63. remote.push_back(argv[1]+2);
  64. i++;
  65. }
  66. for(;i<argc;i++)
  67. {
  68. if(argv[i][0]=='-' || argv[i][0]=='/')
  69. {
  70. const char* arg=argv[i]+1;
  71. switch(tolower(*arg))
  72. {
  73. case 'u':
  74. user.set(arg+1);
  75. break;
  76. case 'p':
  77. password.set(arg+1);
  78. break;
  79. case 'c':
  80. rcmd->setCopyFile(false);
  81. break;
  82. case 'f':
  83. rcmd->setCopyFile(true);
  84. break;
  85. case 'd':
  86. nowait=true;
  87. break;
  88. case 's':
  89. rcmd->setSession(atoi(arg+1));
  90. break;
  91. case 'h':
  92. rcmd->setWorkDirectory(arg+1);
  93. break;
  94. case 'n':
  95. network=false;
  96. break;
  97. case 'm':
  98. {
  99. char buf[256];
  100. for(ifstream ips(arg+1);ips && ips.getline(buf,sizeof(buf));)
  101. {
  102. remote.push_back(buf);
  103. }
  104. break;
  105. }
  106. default:
  107. usage();
  108. }
  109. }
  110. else
  111. break;
  112. }
  113. StringBuffer cmd;
  114. for(;i<argc;i++)
  115. {
  116. cmd.append(argv[i]).append(" ");
  117. }
  118. rcmd->setCommand(cmd.str());
  119. rcmd->setNetworkLogon(network);
  120. if(!nowait)
  121. {
  122. rcmd->setOutputStream(queryStdout());
  123. }
  124. rcmd->setErrorStream(queryStderr());
  125. for(std::list<std::string>::iterator it=remote.begin();it!=remote.end();it++)
  126. {
  127. ragent->addMachine(it->c_str(),user,password);
  128. }
  129. ragent->startCommand(rcmd);
  130. ragent->wait();
  131. if(nowait)
  132. {
  133. printf("Started process %d",rcmd->getPid());
  134. }
  135. }
  136. catch(IException* e)
  137. {
  138. StringBuffer buf;
  139. printf("%s",e->errorMessage(buf).str());
  140. e->Release();
  141. return -1;
  142. }
  143. catch(...)
  144. {
  145. printf("An unknown exception occurred!");
  146. return -1;
  147. }
  148. return 0;
  149. }