Wednesday, January 15, 2014

Create A Bunch Of Random Files for Testing Whatever Using Powershell



I need to test processing of files by a process that ingests text files into an Oracle Database.  For this I do not care what the data looks like once ingested. I care more about testing the ingestion process which is based on a JAVA application.  To give the ingestion process a good workout I generate a ton of files, the more the better. To do this I kludged together a powershell script that does the job.  The script generates as many text files as I want using any text source I want.  Then the script inserts a random date into each file so that they appear as new files to Oracle.  I could use powershell to randomly add anything but I choose dates since there is a date field in the Oracle database.  

I found the powershell snippet that generates random dates online a while back and not sure who gets credit, but it is not me.  A quick search this morning and I saw several hits in Google for the code.
Basically the script creates a new file appends a random date then cats a source text file and appends that to the new file.  Even though each file has the same information they are random because of the random date.

#declare some variables
$SourceText=”c:\path\to\mytext.txt”  (edit as needed)
$DestLoc=”c:\path\to\where\files\go  (edit as needed)
$myTxt= cat $SourceTxt    (I used this because I did not find a simple way to cat the source file right in the code when I need the text.  The code is “Add-content $DestLoc\$y.txt $myTxt”  I tried using backticks like used in bash to put the output of one command into another command like so: “Add-content $DestLoc\$y.txt `cat $SourceText`” but powershell does not like that.
#set how many files are needed
$Xfiles=10000 (edit as needed)

#set up a loop
for ($i=1; $i -lt $Xfiles; $i++)
{
$y = $i; (on each iteration set $y = current value of $i)
new-item $DestLoc\$y.txt -type file -force (create the new empty file using $y as the file name)

#Generate a random date
[[DateTime]$theMin = "1/1/2008" (start date edit as needed)
[DateTime]$theMax = [DateTime]::Now (end date, for this it is current, edit as needed)
$theRandomGen = new-object random
$theRandomTicks = [Convert]::ToInt64( ($theMax.ticks * 1.0 - $theMin.Ticks * 1.0 ) * $theRandomGen.NextDouble() + $theMin.Ticks * 1.0 )
$dateNow=(new-object DateTime($theRandomTicks))
#Append random date to new file
Add-content $DestLoc\$y.txt $dateNow
#Append target text to new file
Add-content $DestLoc\$y.txt $myTxt

}

Uncluttered Script_________________________________________________________

#declare some variables
$SourceText=”c:\path\to\mytext.txt”  
$DestLoc=”c:\path\to\where\files\go  
$myTxt= cat $SourceTxt  
#set how many files are needed
$Xfiles=10000
#set up a loop
for ($i=1; $i -lt $Xfiles; $i++)
{
$y = $i;
new-item $DestLoc\$y.txt -type file -force

#Generate a random date
[[DateTime]$theMin = "1/1/2008"
[DateTime]$theMax = [DateTime]::Now
$theRandomGen = new-object random
$theRandomTicks = [Convert]::ToInt64( ($theMax.ticks * 1.0 - $theMin.Ticks * 1.0 ) * $theRandomGen.NextDouble() + $theMin.Ticks * 1.0 )
$dateNow=(new-object DateTime($theRandomTicks))

#Append random date to new file
Add-content $DestLoc\$y.txt $dateNow

#Append target text to new file
Add-content $DestLoc\$y.txt $myTxt

}

Friday, January 10, 2014

Can't Start network on New RHEL6 VM Clone

After cloning a RHEL 6 VM I could not get the network running, got error that the nic was not present.
Problem turned out to be that when cloning the new VM gets a new MAC but RHEL UDEV still showed the configuration of the network before cloning so there was a conflict.  On the source VM I had an eth0 on the cloned VM I only had eth1 and no eth0.



You need to edit  /etc/udev/rules.d/70-persistent-net.rules
Find these lines:
# PCI device 0x15ad:0x07b0 (vmxnet3) (custom name provided by external tool)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:50:56:bc:00:45", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"


# PCI device 0x15ad:0x07b0 (vmxnet3)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:50:56:bc:00:46", ATTR{type}=="1", KERNEL=="eth*", NAME="eth1"


Look at the entries and find your virtual NIC MAC address, You can get that In VSphere client by editing the new cloned system clicking on the NIC and you will see the MAC  listed on the right side of the properties box.   


In the 70-persistent-net.rules file you should see that the entry for eth1 has the new MAC not eth0.


Delete entry referring to eth0 i.e., NAME=”eth0”


On the remaining entry edit NAME=”eth1” to NAME=”eth0”


Now fix  /etc/sysconfig/network-scripts/ifcfg-eth0 by making sure the HWADDR matches the correct MAC address.
 
Restart the network or reboot as necessary.

Thursday, January 9, 2014

Rescue data with Simple HTTP Server

Had Rhel system VM that crashed during a yum update then would not boot due to a kernel panic.  Had 3 kernels listed in Grub and the system would panic on each until oldest kernel let me login in as single user for maintenance. Before going on with troubleshooting and recovery etc I wanted to save my data just in case. I could not hook up an external drive so I took a shot at starting the network and it started right up.  With that done I tried starting ssh but it would not start, the sshd.config file was corrupted and the ssh service completely missing from init.d.  So I opted on recovering my data using the “Really Simple HTTP Server” in Python.  This was not only simple but it worked great.

Python and SimpleHTTPServer may already be installed by default on a RHEL6 system on RHEL6, they are on our build. To use use just do the following:



$ cd /home/somedir$ python -m SimpleHTTPServer

Now the http server will start on port 8000.
You should see:
Serving HTTP on 0.0.0.0 port 8000 ...

From a browser on another system connect with http and IP of the target system:
http://xxx.xxx.xxx.xxx:8000 and you will see you files and sub-directories in the directory you are running SimpleHTTP.

You can only download individual files not directories so if need be tar up the files you want and down load the tar.


This is also a great and easy way to get files in lieu of scp, just don't leave the Simple HTTP Server running, probably not very secure.


REF: http://www.linuxjournal.com/content/tech-tip-really-simple-http-server-python

Thursday, January 2, 2014

Simple Powershell That Builds A List From User Input



This script builds a list from user input.   For me this list is used as a source for configuring folders and xml config files with other Powershell scripts used in a custom process on my job.
The scripts keeps prompting for inputs until quit.


#Declare Variables (Optional)
$DestList=”c:\mypath\list.txt  The list file being built


clear (clears the screen)


echo “Start Building the List”


new-item $DestList -type file -force   Create the file and if it already exists force it to be recreated


$response = “”   
do
{
echo “”
echo “”
$response = Read-Host “Enter a list item or Q when done”   Can use Q or q
if ($response -ne “Q”)
{
echo $response >> $DestList
}
until ($response -eq “Q”)

# The next section is optional

clear
echo "Here is the list"
echo ""
cat $DestList
echo ""
echo ""
echo "If the list is not correct either re-run script or directly edit the new file"
start-sleep -s 10  (gives time for user to see list and message before script quits)


____________Full Script_____________________
#Declare Variables
$DestList=”c:\mypath\list.txt  
clear
echo “Start Building the List”
new-item $DestList -type file -force  
$response = “”   
do
{
echo “”
echo “”
$response = Read-Host “Enter a list item or Q q when done”  
if ($response -ne “Q”)
{
echo $response >> $DestList
}
until ($response -eq “Q”)

# The next section is optional

clear
echo "Here is the list"
echo ""
cat $DestList
echo ""
echo ""
echo "If the list is not correct either re-run script or directly edit the new file"
start-sleep -s 10