Shell scripts

Displaying the host name of a UNIX server on login

September 27th, 2007    2 Comments

To show the host name of a server when you log in to it. Add the following at the end of your shell startup file (e.g. .bashrc or .cshrc in your home directory): perl banner.pl Then create the following Perl script in your home directory and call it banner.pl. # Perl script for printing banner showing name of local host use Text::Banner; $a = Text::Banner->new; $a->set(`hostname`); $a->size(1); $a->fill(''); $a->rotate('h'); print "\n"; drawlines(); print $a->get; drawlines(); print "\n"; print "\n"; sub drawlines() {     for ($loop=1; $loop<4; $loop++) {         for ($n=1; $n<60; $n++) {             print "=";         }         print "\n";     } } Finally, install the Text::Banner perl module  from CPAN. Note: if you don't want to (or can't) install the Perl module in the normal way on the server, you can  download the zip file from the CPAN Web site (http://search.cpan.org/~lory/Text-Banner-1.00/Banner.pm), gunzip and tar xvf it  in your home directory and then change the 'use' statement to:use Banner::Banner; Now, when you log in to the server you get something like this (in this case the server is called pegasus): pegasusbanner

Comments

  1. User Gravatar figjam said:

    April 18th, 2009 at 8:56 am (#)

    If you do not want to have it generated everytime, you can use a program called figlet (http://www.figlet.org)  on the linux/unix system. It does not usually come installed as standard. 

    You can install it in ubuntu (for example) by apt-get install figlet 
    The result is that you can generate text using different fonts. Once this has been generated, you can add it into the banner for the ssh/telnet/ftp server.

    -Cary

  2. User Gravatar Alistair said:

    April 19th, 2009 at 9:29 pm (#)

    Thanks for the tip Cary.
    I hadn't heard of figlet before.

Leave a comment

 

Using a shell script to replace text in multiple files

January 22nd, 2005

The problem I usually work at the office, but occasionally work from home. At the office I can browse to any networked server in Windows (e.g. in Windows Explorer) by entering an address like \\networkedserver. When I'm working from home, I log on to my work network via an SSH session. This works fine, but one drawback is that, because it's not a VPN session my home PC is not part of the work network, so although I can access machines from a command line, using SSH, or upload/download files using WinSCP, addresses like \\networkedserver don't work because (unless "networkedserver" happens to be one of my own machines on my own home network). How this affects my work The only hassle this causes is when I work on structured FrameMaker files from home. I need to copy the source XML files from the server at work to my own machine (using WinSCP) and then modify them the path to the DTD, using a text editor. Once I've finished working on them in FrameMaker and I want to copy them back to the work server, I need to change the DTD reference back to one that works on my work network. For example, at work the FrameMaker XML files I work with reference a DTD called mxDocBook.dtd. Let's say the path to this is //networkserver/mxDocBook/mxDocBook-DTD/mxDocBook.dtd. So the start of each XML file looks like this: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE chapter SYSTEM "//networkserver/mxDocBook/mxDocBook-DTD/mxDocBook.dtd" [ ... If I tried to open this file in FrameMaker on a computer on my home network I'd get an error message, because FrameMaker wouldn't be able to find the DTD file. So I have to change the identifier to something like: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE chapter SYSTEM "file:///D:/work/mxDocBook/DTDfiles/mxDocBook.dtd" [ ... Changing each file back and forwards could get very tedious, but there's a solution. The solution: the changedtd shell script I wrote a shell script that you can use to automate this process. The script (called changedtd) contains variables called FINDTHIS, CHANGETOTHIS and FNAMEPATTERN. It looks for files in the current working directory that match the FNAMEPATTERN (typically *.xml) and performs a search/replace, searching for FINDTHIS and replacing it will CHANGETOTHIS. If you want to use the script, copy it and edit the FINDTHIS and CHANGETHIS values to appropriate paths. To run the shell script you obviously need a shell. I'd recommend installing Windows Services for UNIX 3.5 as the easiest way of getting a UNIX-like shell on a Windows machine. You can download SFU 3.5 free of charge from: www.microsoft.com/windows/sfu Once you've got a shell, all you need to do is put the changedtd file in a directory that's in your PATH, and then, in a UNIX/SFU shell, browse to the directory containing the files you want to change and run the command: changedtd View/download the script Notes: You might think it's a very long-winded script to do such a simple operation, but most of the verbosity of it comes from: a) The parsing of the command line arguments. This allows you to run the script on a directory other than the current working directory and to recurse into child directories (if you dare). b) The usage instructions, which you can read by running changedtd -h. c) You could of course very easily change this script to do a search/replace with any text you like on any type of file, just by changing the values of FINDTHIS, CHANGETOTHIS and FNAMEPATTERN.

Leave a comment



^ back to top ^

Page 1 of 11