Thursday 16 December 2010

Delete files older than a number of days

 If you want to delete all the files in D:\temp which are older than 30 days old then just run the following command:

forfiles /p "D:\temp" /S /M * /D -30 /C "cmd /c del /Q @path"

Tuesday 30 November 2010

Windows Server 2008 losing IP address after reboot and cannot change Primary IP address at all


There is a bug in Windows Server 2008 which causes the server to randomly loose IP addresses after a reboot. Also for some reason I cannot remove/replace the primary IP address with any other IP address either.

You can see all the IP addresses that are meant to be assigned to the NIC in the GUI but when you do ipconfig you only see some of the IP addresses. If you try to ping the missing IPs, you will not get a response either.

There is a Microsoft patch will fix the problem with missing IP addresses.


KB Article Number(s): 982931


Request the hotfix from MS, install and reboot. By the way, the exe MS provides suggests that it is for VISTA only, but it is for Windows Server 2008 also.

Sadly the primary IP issue continued post hotfix and needed the removal of IP's, disable the card, delete the reg key (HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Network   'Config' reg_binary), restart, enable the card, assign IP's and test

This did not work for me.

After reboot I still have missing primary IP and the only solution is to remove all IPs manually and the run a script which adds all the IPs back to the server with a number of the following:



netsh interface IP add address name="Local Area Connection" 192.168.1.20  255.255.255.0 skipassource=true

Tuesday 7 September 2010

Neevia Document Converter Pro 6 Windows Service Blocking/freezing not converting XL/XLS/XLSX files

If you are having problems converting certain MS Office documents while running the converter as a windows service then read the below:

If you run DC Pro as a windows service try this: on the machine where this happens manually open MsExcel click on the Office Button and go to Word Options and select Popular. In the section, Personalize your copy of Microsoft Office, fill in the values for User name and Initials and reboot.

Another thing you should consider in case you run DC Pro as a service and still have problems log in with the account used by dcStart and launch (open/close) all the applications (MsWord, Excel… etc) used as parsers by DC Pro to have their registry profiles properly created.

Also make sure that MsOffice parsers run in the same user account as DCStart.

To do that type dcomcnfg in the command prompt hit enter, expand the Component Services group, expand the Computers group, expand the My Computer group, expand the DCOM Config group, find and select Microsoft Excel Document (valid for other parsers too) -> right mouse click -> Properties.

- click the Identity tab. Check the This user checkbox, press Browse and specify the Administrator account or the account used by dcStart;

- enter and re-enter the password;

- click the Security tab. Check Use custom access permissions, press Edit and add the "Everyone" user account;

- Check the Use custom launch permissions checkbox, press

- Edit and add the "Everyone" user account;

- reboot the computer;

Friday 9 July 2010

MS SQL 2005 Debugging Stored Procedure Performance by printing current time

When you have a very large Stored Procedure which is made up of multiple queries and is slow or is taking a very long tim eto execute, you can place the following in before and after each section so that when you run it through the MS SQL management studio in the messages tab you can see exactly how long each section of the Stored Procedure takes to execute.

print 1
print convert(varchar(10),CURRENT_TIMESTAMP,114)

select ....

print 2
print convert(varchar(10),CURRENT_TIMESTAMP,114) 

select ....

print3
print convert(varchar(10),CURRENT_TIMESTAMP,114)

Tuesday 20 April 2010

converting certificate from IIS to Apache

If you have a pfx file for IIS web server but you need the relevant key and certificate files for Apache web server the following will be very usefull:


# Export the private key file from the pfx file
openssl pkcs12 -in filename.pfx -nocerts -out key.pem
# Export the certificate file from the pfx file
openssl pkcs12 -in filename.pfx -clcerts -nokeys -out cert.pem
# This removes the passphrase from the private key so Apache won't
# prompt you for your passphase when it starts
openssl rsa -in key.pem -out server.key

Wednesday 31 March 2010

samba jcifs single server/host multiple computer or host names

I was using jcifs in a java application to access files on a remote file server.

Samba and hence jcifs do not like it when you use a path which contains an alias rather than the actual computer or host name.

So for example if the hostname of your remote file server is fileserver1, but for some reason you want to access some files on this server using JCIFS or samba but want to refer to it as fileserver2 and in your local hosts file you have fileserver2 pointing to the same IP address as fileserver1, then when you try to access \\fileserver2\.... you get the following error:

jcifs.smb.SmbException: Failed to connect to server
java.net.UnknownHostException: fileserver2
at jcifs.UniAddress.getAllByName(UniAddress.java:315)
at jcifs.smb.SmbFile.getFirstAddress(SmbFile.java:852)
at jcifs.smb.SmbFile.connect(SmbFile.java:939)
at jcifs.smb.SmbFile.connect0(SmbFile.java:868)
at jcifs.smb.SmbFile.queryPath(SmbFile.java:1321)

The solution is the following:
  1. open registry editor on the fileserver. 
  2. go to HKEY_Local_Machine \ System \ CurrentControlSet \ Services \ LanmanServer \ Parameters 
  3. create a new multi-string value parameter with the name of "OptionalNames". 
  4. edit the data of this newly created field and add as many aliases as you want. One alias on a new line. So in the above case, you would just add fileserver2. 
  5. Now on both the fileserver and your local client add fileserver2 to your hosts file (C:\WINDOWS\system32\drivers\etc\hosts) with the same IP address of fileserver1. 
  6. reboot the fileserver. 

I recently found that an easier way is just to create a new value under HKEY_Local_Machine \ System \ CurrentControlSet \ Services \ LanmanServer \ Parameters on the fileserver and then reboot the fileserver:

Value name: DisableStrictNameChecking
Data type: REG_DWORD
Radix: Decimal
Value: 1

Wednesday 17 March 2010

Rsync over ssh hanging / freezing in Cygwin problem

Sometimes when using rsync over ssh under cygwin in windows to synchronise files from one server to another, the rsync process copies a few files and then just hangs. Sometimes the rsync process utilises 100% cpu and sometimes 0% cpu, but in either case no more files are synchronised.

The solutions is to use rsync as a daemon on the remote server.

run the following in cygwin on the remote windows machine.

cygrunsrv --install "rsyncd" --path /usr/bin/rsync --args "--daemon --no-detach" --desc "Starts a rsync daemon for accepting incoming rsync connections" --disp "Rsync Daemon" --type auto

then create the following file /etc/rsyncd.conf

In this file you should have something like:

max connections = 10
log file = /var/log/rsync.log
timeout = 300
[pub]
comment = Random things available for download
path = /path/to/my/public/share
read only = no
list = yes
Note: in the above example the name of the module is pub.

Then start the service. You can use "net start rsyncd" to do this.

Now when you want to rsync use:

rsync ... src rsync://user@host/pub/datafolder

notice that you must use modules when using rsync daemon. In this case the module is pub.

The above rsync will copy the files to /path/to/my/public/share/datafolder

Friday 5 March 2010

Finding all hosts on a network subnet (Windows Cygwin Bash)

Just run the below in Cygwin bash command shell:

for ip in $(seq 1 254); do ping -w 20 -n 1 192.168.1.$ip>/dev/null; [ $? -eq 0 ] && echo "192.168.1.$ip UP" || : ; done

Friday 12 February 2010

What queries are currently running MS SQL 2005

This is the script which I always had in my archive. Following script find out which are the queries running currently on your server.

SELECT
sqltext.TEXT,
req.session_id,
req.status,
req.command,
req.cpu_time,
req.total_elapsed_time
FROM sys.dm_exec_requests req
CROSS APPLY sys.dm_exec_sql_text(sql_handle) AS sqltext

While running above query if you find any query which is running for long time it can be killed using following command.

KILL [session_id]

Monday 25 January 2010

Email Alert when number of established connections in Apache reach a limit on Windows

This method of alerting is actually relevant for any kind of connection checking.

You can see from the below how easily the script can be customised.

I have Apache 2.2.14 mod ssl installed on a Windows Server 2003.

On this server I have also installed cygwin.

We host a number of HTTPS web applications on this server.

Recently I noticed that suddenly Apache would become totally unresponsive.

After a lot of investigation I found that if I did "netstat -nt" on the server when Apache is unresponsive, then I would see a very large number of connections in ESTABLISHED state on port 443 all from the same IP address.

When this happens Apache become unresponsive to all new requests for 5 minutes. Then suddenly all these connections are released and Apache begins to server requests again.

Now I am still investigating as to why certain valid clients seem to suddenly make hundreds of HTTPS connections to our servers and hold the connections until the Apache timeout of 300 seconds kicks in.

In conf/extra/httpd-default.com the default value for Timeout is set to 300 seconds.

Now you should not really change this value but if you are having the same problem as me you do not really want the Apache server to be unresponsive for 5 miutes (300 seconds). I have set this value to 30 seconds and at least now the server will become unresponsive for 30 seconds then it recovers.

At the same time I wanted to be alerted by email when this situation occurred.

So I did the following on the Apache server:

Download bmail from this link, extract and copy bmail.exe in to d:\tools\bin\bmail.exe

In the same directory create a bash script and call it connectionCheck.sh

assuming
1. your email address is techsupport@abc.com
2. your server is called XYZ
3. you would like to be alerted as soon as the total number of concurrent established HTTPS connections (443) reaches 50
4. your email servers IP address is 192.168.1.10 and that you can connect to this server and send emails from your Apache sevrer.

then the contents of this script looks like:

#!/bin/bash

netstat -nt grep -i established grep -i :443 >netstat.log
X=`cat netstat.log wc -l`
if [ "$X" -gt "50" ];
then
Y=`cat netstat.log`
/cygdrive/d/tools/bin/bmail.exe -s 192.168.1.10 -t
techsupport@abc.com -f XYZ@abc.com -a "XYZ more than 50 https connections" -b "$Y"
fi

I also set
chmod +x connectionCheck.sh

and used vim to
:set ff=unix

To test the above so far you can change the value from 50 to 0 and then just execute the script from cygwin shell when you know there is atleast some activity on your HTTPS Apache server:

./connectionCheck.sh

This should send you an email which contains the netstat details at the time of execution.

Then create a windows scheduled task which starts from 00:00 and ends 23:59 and runs every minute and runs:

c:\cygwin\bin\run.exe bash -wait --login -c "/cygdrive/d/tools/bin/connectionCheck.sh"

Now every minute, this script will run silently.

It will send you an email everytime the established number of https connections to your apapche server goes above 50. You can change this number to whatever you want, but I think apache has a limit of 150 and if you are even hitting 100 concurrent established connections, you probably have a very busy site, unless your apache server takes a very long time to respond to each client request.

Monday 18 January 2010

CYGWIN SSHD service not starting on Windows server 2008 due to Netlogon service not starting

I installed Cygwin with Open ssh on a Windows server 2008.

I ran the ssh-host-config as normal and configured sshd as a service.

When I tried to start the service it game me an error 1068. Something abot not being able to start sshd because it depends on the netlogon service.

I tried to start the netlogon service manually to see if that would help, but I could not start it as it started then stopped immediately saying that because the computer is part of a workgroup and not a domain then the netlogon service does not need to run!!!!

I found the solution was to change the startup type of the netlogon service from manual to automatic. Then reboot the server and everything starts to work!!!
I do not know why this works but it does for me.