I have to use a command-line tool and run specific and same set of commands over and over again. I was wondering if there is a way in which i can automate the whole process. I read online and found out that i can do that with shell codes or scripts.

Its a work thing i have to use a command line tool (metasploit) and i have to run same commands over and over again on different targets, my goal is to write a script of sort which when i run will start my tool and automatically run the commands i want. can I do that?

  • That is, you can write a script with the commands you have to execute often. – Zumo de Vidrio Mar 23 '17 at 12:06
  • alias, functions, scripts, even cron jobs, there are many ways to achieve what you want. Can you clarify what your goal is? – pfnuesel Mar 23 '17 at 12:12
  • Do you have a list of commands you are looking to run? It may be as simple as putting this list of commands inside a file, in the order you want them to run, and giving the file execute permissions. This is a shell script. Please be more specific. – MikeD Mar 23 '17 at 12:51
  • Updated the thread – HeroicJokester Mar 23 '17 at 14:19

If your commandline tool is an interactive one, you could write an expect - script. In such a script, you can "expect" some output (e.g. $> as the user-prompt) and when it comes, send a command to stdin of your commandline.

See for example the wikipedia article https://en.wikipedia.org/wiki/Expect

If your tool is not interactive, you can write a simple script (as mentioned in the comment), which will call every command you have to use, for example:

command1 arg1
command2 arg1 arg2

When you call this script via ./path/to/script it will run every command until it

  1. Want some input on stdin, or
  2. finished the last command
  • Yes I do have to wait for a few seconds to get the metasploit started, but i dont think expect will be needed, cause after that all i have to do is run the commands and get a meterpreter session started. around 4-5 commands Also i have never written a script before so i can be totally wrong, can you refer a tutorial or a guild where i can learn a bit more about it? – HeroicJokester Mar 23 '17 at 14:17
  • If you don't use the & sign at the end of the commands in the shell script every command will wait for the one before to finish. So a simple script should be enough to start metasploit. So if your start-command will return your prompt after it finished you can put this command also to the script. – Michael Mar 23 '17 at 14:19
  • so it will be something like "START msfconsole" "search ms08_067" is that how the script should look like? – HeroicJokester Mar 23 '17 at 14:29
  • It depends on the command line interface. If your commands are all run from the bash prompt (the linux command line), you can use the commands in the normal bash script. If your first command will end in an extra command line interface (CLI) where you enter the commands for the program, you have to use expect to achive an automated run of all commands. Another method could be the use of command options if your program supports that. As i am not familiar with metasploit, I can only give you tips about scripting with bash or expect, not specialized to metasploit. – Michael Mar 23 '17 at 14:40
  • (Note: I also have no familiarity with metasploit) If metasploit acts as an interpreter reading from stdin there are a couple more options: a file containing commands can be used like metasploit < commands, or the file could have metasploit in a shebang line, as is done for sh or perl – Fox Mar 23 '17 at 17:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.