Announcement

Collapse
No announcement yet.

My new shell scripting fun.

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    My new shell scripting fun.

    I never been much of a scripter before. Well I decided to change that. I know most of you are not unix people but, I just had to share my latest creation. First a bit of info. So I work testing network equipment. We have racks of computer in a lab. We also use vmware. I wanted a way to slap a unix OS on these machines. A while ago I found away to have the machines boot over the network and start installing a OS. I can do this both for windows and linux. The only thing I need to do is change some entries in the MS DHCP server we have and then tell the client to PXE boot. Well we use VMware as well. Usually I
    1. Ghost a linux image that has VMware already running
    2. Modify the image
    3. Then create the VMs
    4. Boot the VMs to give them they're MAC address
    5. Add the MAC address to the MS DHCP server with the right options
    6. Reboot the VMs

    So I wanted to script as much as this as possible. So my script requires a file with a lit of IPs for the machine. What the script does
    1. Create 16 Debian VMs
    2. Power the VMs on (to generate the MAC address)
    3. Power off the VMs
    4. Upgrade the VM config file
    5. Get all of the MAC addresses and create a batch file that has the proper netsh.exe command to update the DHCP Server (I have to copy this to the DHCP Server and run it)
    6.Get a list of the VMIDs (internal reference number for the VM)
    7.Make it so all the VMs start on a reboot of the vmware server
    8.Create a power on/off all vm script

    So I would then copy the bat file to the MS DHCP server and run it. Then run the poweron all script. The Linux OS installs over the network from a local mirror in about 15 minutes for all VMs. I then run the poweroff script andthen the poweron script again. Presto I have 16 Debian Linux Virtual Machines. I'll post the script in a follow up post
    3.

    #2
    Heres the script


    #!/bin/bash
    #Delete old dhcpconf.bat file and add scope option to allow FAI PXE
    echo "netsh.exe Dhcp Server 192.168.41.2 Scope 192.168.32.0 delete optionvalue 60" > /var/lib/vmware/dhcpconf.bat
    #set number of VMS
    VMCOUNT=16
    #Start loop
    while [ $VMCOUNT -ne 0 ]
    do
    #Check VMCOUNT var and lookup ip addy for this vm in the iplist file
    IP=$(grep -n "$VMCOUNT" /var/lib/vmware/iplist |grep "$VMCOUNT:" | cut -d: -f 2)
    #create vmdisk and vmx files
    ./createvm.sh debian4 -n $HOSTNAME-deb32-$VMCOUNT -q
    #Register vm with server
    vmrun -h https://localhost:8333/sdk -u root -p xxxxx -T server register "[standard] $HOSTNAME-deb32-$VMCOUNT/debian4.vmx"
    #Upgrade VMs
    vmrun -h https://localhost:8333/sdk -u root -p xxxxx -T server upgradevm "[standard] $HOSTNAME-deb32-$VMCOUNT/debian4.vmx"
    #Start VM to generate MAC addy
    vmrun -h https://localhost:8333/sdk -u root -p xxxxx -T server start "[standard] $HOSTNAME-deb32-$VMCOUNT/debian4.vmx"
    #Stop VM
    vmrun -h https://localhost:8333/sdk -u root -p xxxxx -T server stop "[standard] $HOSTNAME-deb32-$VMCOUNT/debian4.vmx"
    #Grep vmx file for vm mac addy, vm name
    MAC=$(grep /var/lib/vmware/Virtual\ Machines/$HOSTNAME-deb32-$VMCOUNT/debian4.vmx -i -e "00:0c:" | awk '{print $3}' | sed s/\"//g | awk '{print $0}' | sed s/\://g)
    VMNAME=$(grep /var/lib/vmware/Virtual\ Machines/$HOSTNAME-deb32-$VMCOUNT/debian4.vmx -i -e "deb32" | awk '{print $3}' | sed s/\"//g)
    #Create bat file to run on gandalf to delete exsisting lease, then add reservation to pxe boot debian install
    echo "netsh.exe Dhcp Server 192.168.41.2 Scope 192.168.32.0 Add reservedip $IP $MAC "\"$VMNAME.shire.bluearc.com"\" "\""\" "\"BOTH"\"" >> dhcpconf.bat
    echo "netsh.exe Dhcp Server 192.168.41.2 Scope 192.168.32.0 set reservedoptionvalue $IP 12 STRING "\"$VMNAME"\"" >> dhcpconf.bat
    echo "netsh.exe Dhcp Server 192.168.41.2 Scope 192.168.32.0 set reservedoptionvalue $IP 66 STRING "\"192.168.41.201"\"" >> dhcpconf.bat
    echo "netsh.exe Dhcp Server 192.168.41.2 Scope 192.168.32.0 set reservedoptionvalue $IP 67 STRING "\"pxelinux.0"\"" >> dhcpconf.bat
    #decrament vm number
    VMCOUNT=$(( $VMCOUNT - 1 ))
    done
    #get vms ID number and put into file
    vmware-vim-cmd vmsvc/getallvms |cut -d " " -f 1 |grep [0-9] > vmids
    #paste together three files creating command to autostart vms
    paste /var/lib/vmware/vmware-vim-cmd /var/lib/vmware/vmids /var/lib/vmware/autostart > /var/lib/vmware/poweron
    chmod 777 /var/lib/vmware/poweron
    /var/lib/vmware/poweron
    #Create file to power on all vms
    paste /var/lib/vmware/vmware-vim-cmd-power-on /var/lib/vmware/vmids > /var/lib/vmware/powernow
    chmod 777 /var/lib/vmware/powernow
    echo "RDP into Gandalf and make sure there are no DHCP leases for the IP's you used"
    echo "Copy the dhcpconf.bat to Gandalf and run as administrator"
    echo "This will add the right entries into the DHCP Server to pxeboot and install Debian"
    Last edited by mapes; 13 Jul 2010, 06:37 PM. Reason: not that it matters but left my root login pw in there

    Comment


      #3
      It's all Chinese to me.
      [img]https://farm5.staticflickr.com/4373/35734799443_53cb20ef13_z.jpg[/img]


      Killed by CLRs since 2004. WOOT!
      Support Cainslair. Donate here! [url]http://www.cainslair.org/billspaypal.php?[/url]

      Comment


        #4

        Actually Japanese Kanji.
        Apache

        Where do you put the Bayonet?
        Chesty Puller (upon seeing a flamethrower for the first time)
        I am all in favor of keeping dangerous weapons out of the hands of fools. Lets start with typewriters.
        Frank Lloyd Wright

        Comment


          #5
          That's awesome, mapes. Shell scripting rocks.
          [SIZE=1][B]"Now more than ever the people are responsible for the character of their Congress. If that body be ignorant, reckless, and corrupt, it is because the people tolerate ignorance, recklessness, and corruption." ~President James Garfield[/B][/SIZE]
          <<< Please [URL="http://www.cainslair.com/misc.php?do=donate"]donate[/URL] >>>

          Comment


            #6
            Ugh, believe me mapes. All i work with is unix.
            Delivering bacon goodness one round at a time.

            Comment


              #7
              Originally posted by baconoclock View Post
              Ugh, believe me mapes. All i work with is unix.
              What do you do?

              Comment


                #8
                Looks good! We're working on some scripting for a project at school for a Live USB/CD.

                Comment


                  #9
                  Im more lost than the Easter Bunny in July

                  its a bunc of gibberish to me

                  Comment


                    #10
                    I dunno why you're only printing 3 dollars...

                    Did you do this just to see if you could, or is this one of those "OMFG, I have do this AGAIN!" sanity saver deals?

                    Comment


                      #11
                      Wut
                      [COLOR="#008080"][/COLOR][SIZE="5"][COLOR="LightBlue"][B]Not everything that counts on the battlefield is countable.[/B][/COLOR][/SIZE]

                      Comment


                        #12
                        Originally posted by Apache Warrior View Post

                        Actually Japanese Kanji.
                        Apache
                        I dated a Japanese lady who showed me some Kanji. When she said she knows around 6,000 characters, and some folks know 8-10,000, etc., I pretty much lost interest in the subject, changed the subject. Memorization is not a Skudlike quality...I probably wouldn't be able to speak or walk upright if I had to make room for all those symbols in my head. And, yes, Mapes' script I'll leave untouched as well. But, hey, Mapes, nice work, I think.

                        Comment


                          #13
                          You used to have to know 6500 kanji to have a 6th grade education. They lowered the requirements later (I do not remember the date) There are over 50,000 Kanji but quite a few are rarely used. You would probably have to know 2000 Kanji plus Hiragana and Katakana (both are phonetic languages) to be able to read Japanese Newspapers, signs and labels. When you write Kanji, stroke order and direction are extremely important.
                          And to think, we only have to learn 26 letters.
                          Apache

                          Where do you put the Bayonet?
                          Chesty Puller (upon seeing a flamethrower for the first time)
                          I am all in favor of keeping dangerous weapons out of the hands of fools. Lets start with typewriters.
                          Frank Lloyd Wright

                          Comment


                            #14
                            Originally posted by Apache Warrior View Post
                            You used to have to know 6500 kanji to have a 6th grade education. They lowered the requirements later (I do not remember the date) There are over 50,000 Kanji but quite a few are rarely used. You would probably have to know 2000 Kanji plus Hiragana and Katakana (both are phonetic languages) to be able to read Japanese Newspapers, signs and labels. When you write Kanji, stroke order and direction are extremely important.
                            And to think, we only have to learn 26 letters.
                            Apache
                            And Apache slam dunks the competition with historical facts FTW.

                            In 1987 I had a chance to learn unix, but didnt think I had the time. I wish I would have taken the time now. I always was intrigued by programming old school. Got around in DOS ok but thats were I left off.

                            Good work though.
                            [COLOR="#008080"][/COLOR][SIZE="5"][COLOR="LightBlue"][B]Not everything that counts on the battlefield is countable.[/B][/COLOR][/SIZE]

                            Comment


                              #15
                              Originally posted by mapes View Post
                              What do you do?
                              I'm a systems engineer in the remote hosting section of the company i work for. Clients will pay for us to host their systems and everything related to it. So part of my role is to make sure they stay in 99.999 uptime. We mainly use hpux and AIX.
                              Delivering bacon goodness one round at a time.

                              Comment

                              Cain's Lair Forums Statistics

                              Collapse

                              Topics: 26,187   Posts: 269,850   Members: 6,183   Active Members: 7
                              Welcome to our newest member, Fermin13Q.

                              Today's Birthdays

                              Collapse

                              Top Active Users

                              Collapse

                              There are no top active users.

                              More Posts

                              Collapse

                              • Reply to Hi guys!
                                by Evil_T0NY {CLR}
                                I've been Alpha and will be Beta testing the Delta Force game. It's been really getting good reviews! Definitely a good Battlefield feel to it like the...
                                14 Nov 2024, 08:50 PM
                              • Reply to Hope your all OK over there
                                by Apache Warrior
                                We had 17 inches of rain from the storm on November 7, 2024.
                                Apache
                                11 Nov 2024, 07:55 AM
                              • Reply to Hope your all OK over there
                                by Sirex
                                Aye, I'm inclined to agree with that lmao
                                Gone are the days of warm summers and snow filled winters here, nothing but rain and wind for 8mths of...
                                10 Nov 2024, 08:53 PM
                              • Reply to Hope your all OK over there
                                by Apache Warrior
                                Now we have had a lot of flooding in this area and there are still a lot of houses that have not been repaired. Must be the apocalypse.
                                ...
                                8 Nov 2024, 09:23 AM
                              Working...
                              X