Monthly Archives: August 2009

Unattended FTP script to download files

Looks like I also found a nice quick way to download files off my webserver using a ftp script.  It’s so simple that I don’t know why I didn’t even think of it before!  Before I had this script, I had to manually download my sysadmin-tools from my website onto the remote servers I was doing maintenance on that particular day.  That took about 3-5 minutes each time I had to download the tools I had in a zip file on my webserver, which as you can imagine get really repetitive and annoying after a while.  So after about the 50th time downloading the sysadmin-tools, I decided to automate it!

This is how I approached creating the script:

1.  It has to work out of the box with Windows (I administer and maintain Windows Server 2003 boxes)

2.  It had to connect to my webserver using my username and password

3.  It had to change directories on the server to the download directory

4.  K.I.S.S (keep it simple stupid!)

Decisions:

After reviewing the script requirements, I decided to write a simple batch script using the built in FTP command in windows.  I didn’t know windows had a built in FTP command to tell you the truth until I starting writing the script.

Unattended FTP Script:

Below are the contents of the batch file:

@echo off

ftp -s:ftpscript.txt

Below are the contents of the ftpscript.txt file:

open
yourserver.com
username
password
cd “/public_html/yourdownloadfolder”
binary
get “sysadmin-tools.zip” “C:\Users\username\Desktop\sysadmin-tools.zip”
quit

Explanation:

It’s pretty straightforward, the batch file using the ftp command to start the built-in ftp program in windows.  After that the batch script calls the “ftpscript.txt” file to supply the ftp command to execute.  In the ftpscript.txt file it basically says to connect to a server, enters a username and password, then changes directories to the directory that contains the file you wish to download, switches the FTP mode to binary so it will download the .zip file correctly, downloads the file to somewhere on my Desktop, and finally quits FTP.

FTP Modes:

Make sure you are downloading in the right FTP mode!! I was getting corrupted files at first when I was making the script because I was downloading the .zip file in the wrong mode.  Once I changed to binary it downloaded successfully without any corruption issues.

Text files (html, php, css, java) — use text mode
Multimedia files (images, audio, video) — use binary mode
Programs (.exe, .class, .jar, .swf) — use binary mode

References:
http://www.robvanderwoude.com/ftp.php
http://www.bluehostforum.com/showthread.php?t=9311

  • Share/Bookmark

Using scp with spaces in the file path

I just recently learned how to upload a file from my linux box to my webserver using scp the other day. Normally its pretty straightforward to use scp to upload or download a file to a server, however I was having the hardest time uploading this one file that had spaces in the file path.

Here is what it looked like:

scp -r /home/username/Desktop/Sysadmin\ Tools/sysadmin-tools.zip user@website.com:public_html/somefolder/

When i would run this command, the following error would be displayed:

zip warning: name not matched: /home/username/Desktop/Sysadmin
zip warning: name not matched: Tools/*

As you can see, it was breaking up the file path at the space in “Sysadmin Tools” folder, and breaking it up into different arguments. The solution to this problem is to do the following:

Solution:

scp -r /home/username/Desktop/Sysadmin\\ Tools/sysadmin-tools.zip user@website.com:public_html/somefolder/

All I needed to add was an extra “\” backspace character after “Sysadmin”. This is because when the file is uploaded to the remote server, the “\” escape character will disappear (I am not sure why), and will cause this kind of error. However if you double escape the space “\\”, the first escape character will be removed but the second will remain, thus keeping the correct file path.

References:

http://www.thingy-ma-jig.co.uk/blog/14-05-2007/how-to-scp-a-path-with-spaces

  • Share/Bookmark

Shutdown script did not shutdown the sever! Exit Code 0x4f7

Well recently a shutdown script that I wrote did not shutdown a server successfully for some odd reason.  The script looks like the following:

shutdown -r -t 20 -c “Shutting Down to Install Windows Updates”

So until now this script has been working flawlessly on multiple servers.  I’ve been able to schedule automatic reboots of remote servers easily using this script in combination with windows scheduled tasks as well.  It comes in handy when you need to reboot the server at a specific time, such as 5:25PM or 1:26AM, which is great for me since I do a lot of remote server maintenance and do not have physical access to these servers.

Anyway, after trying to reboot this one server it gave me a exit code of (0x47f) which showed up in the scheduled tasks log.

scheduled tasks 0x4f7

After Looking up the exit code, I found a website that directed me to convert 47f to decimal and enter the decimal value “net helmsg” in the command line.  The command I entered was “net helpmsg 1271″, which gave me the following answer: “The machine is locked and can not be shut down without the force option”.

net helpmsg 1271

Well isn’t that great!  A error code that is easily understandable from Microsoft!

The solution to this problem was to edit my script to include the force option.  Here is the updated script below:

shutdown -r -f -t 20 -c “Shutting Down to Install Windows Updates”

And that should do the trick.

References:

http://www.tech-archive.net/Archive/Windows/microsoft.public.windows.terminal_services/2006-08/msg00331.html

  • Share/Bookmark

Windows Updates restart now or later option appears to all users on the Terminal Server

Just as the title suggests, Windows Update pops up notifications to restart the server now or later to all users on a Terminal Server.  I ran into this yesterday when I had installed a security update and a few miscellaneous ones as well.  I figured that the restart notifications would only be shown on the administrator accounts and not all users on the Terminal Server.  That proved to be a bad assumption on my part, but a logical one.  Why would regular users need to be notified that the server needed to be rebooted to finish installing updates when they don’t even have the security permissions to do so?

Anyway the Chief Financial Officer e-mailed me wondering what was going on later in the afternoon.  She and other users were getting really annoyed by the constant pop up to restart the server ( shoot I would be too!).  So in the end I told her to restart the server and that was the end of the problem.  Why couldn’t Microsoft restrict these updates notifications and restart pop up’s to only the administrator accounts?  Maybe there is a registry setting that I just don’t know about that could do all this for me, but for now I’ll just remember to schedule these updates during off hours.

  • Share/Bookmark

NTBackup error (1f)

I just ran into a problem where a daily exchange backup was not completing successfully. The error NTBACKUP gave me was (1f) which is because it could not find the backup file (.bkf) that it created for the backup set.

I went to the shared drive where the backup file should have been saved, and sure enough it was gone. I watched it for a day just to see if it would create the file by itself, but that proved to be false. It looks like NTBACKUP needs to already have the backup file in the right location for it to run the backup job successfully.

To solve the problem, I recreated the backup file that it was looking for in the shared folder. I did this by doing the following:

filenew

1. Right click
2. Choose “New”
3. Select “Text Document”
4. Type the exact name of the file that ntbackup needs.
5. Make sure you change the file extension to .bkf or it ntbackup will not use it.
6. Done.

After that the backup jobs started to run successfully again without a problem. I am still not sure why the original file disappeared but that’s not really important at this point.

  • Share/Bookmark