0

Hi guys i need a shell script that can open for example msfconsole and then type commands in it, i made a very basic script but of course it stops when msfconsole starts... can you give me some example?

thanks!

this is the basic dumb script i made:

#!/bin/bash

service postgresql start && sleep 4 && msfconsole && sleep 15 && quit

As you can see i first start postgresql and then i start msfconsole and when i try to close it i can't because i'm not in the shell but im into metasploit.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
jamesk
  • 39
  • 6
  • 2
    Check out [Six Ways to Automate Metasploit](https://community.rapid7.com/community/metasploit/blog/2011/12/08/six-ways-to-automate-metasploit). Not being familiar with metasploit, it seems like the "Resource Script" in the first option would be the way to go. In a bash script you could call your mfsconsole with the `-r` flag and refer to your resource script for the commands to be executed (or use a Heredoc if that's your flavor. – JNevill May 23 '17 at 13:25

2 Answers2

1

If you want commands in your script to be read by msfconsole, you need to direct them to its stdin; otherwise, they're only read by the shell itself (which is waiting for msfconsole to exit before it proceeds).

One way to redirect a set of text to a given command is with a heredoc:

#!/bin/bash

service postgresql start || exit

msfconsole <<EOF
sleep 15
quit
EOF
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • *shrug*. This is what they call a "fact-intensive investigation" (and is *why* I edited the question to add the [tag:metasploit] tag). The given syntax *definitely* sends the commands `sleep 15` and `quit` to the stdin of `msfconsole`; the question is whether msfconsole is actually expecting or reading them from stdin. I can speak with some authority as to shell syntax and semantics, but metasploit is outside my wheelhouse. – Charles Duffy May 24 '17 at 15:41
-1

I am not familiar with msfconsole but if you execute it then you get a prompt to enter commands then the following method I use to partition may ssd with gdisk, may work for you.

#!/bin/sh

(
# Delete current partition table
echo o                  # Delete partition table
echo Y                  # Confirm deletion

# Create Linux LVM partition
echo n                  # Create new partition
echo 3                  # Partition number
echo                    # First Sector
echo                    # Last Sector
echo 8E00               # Set partition type: Linux LVM

# Write out partition table
echo w                  # Write out new partition table
echo Y                  # Confirm write

) | gdisk /dev/sdx

If you have space in the commands "" needed like: echo "ls -ltr"

lw0v0wl
  • 664
  • 4
  • 12
  • That big whole `(echo ...)` block might also be written as one line: `printf '%s\n' o Y n 3 '' '' 8E00 w Y` – Charles Duffy May 23 '17 at 15:33
  • BTW, `a && b || c` is **not** equivalent to `if a; then b; else c; fi` -- the former can run *both* `b` and `c` (if `b` exits with a nonzero status), whereas the latter is guaranteed to do only one or the other. (Granted, not a big deal with two `echo`s going to the same file descriptor -- if one fails, the other likely will as well -- but the idiom can bite one if used in other circumstances). – Charles Duffy May 24 '17 at 16:54