LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-18-2010, 12:39 PM   #1
geforce
LQ Newbie
 
Registered: Apr 2010
Posts: 5

Rep: Reputation: 1
Script to generate SSH Banner MOTD


Hi All

I want to automate my System-installation. So i try to make a Script that will generate the /etc/motd. Now i have the Problem that the script has some variables and after the "Hostname, OS, HW, IP" the "stars" are anyware, but not there they should. The most problem is the $OS this string can verry long or verry short be. Fedora release 12 (Constantine) or RedHat 5.4

Can anybody tell me what i can do that after the variables place the "stars" on the write position?


Script
Code:
#!/bin/bash


INSTDATE=$(date +%F)
HOSTNAME=$(hostname)
OSRELASE=$(cat /etc/*release | head -1)
OSDETECT="i-have-to-define"
IPADDRES=$(ifconfig eth0 | grep -o '[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*' | head -1)


cat << End-of-Message > /etc/motd
    * * * * * * * * * * * * *:  W A R N I N G  :* * * * * * * * * * * * * *
    *THIS SYSTEM IS RESTRICTED TO AUTHORIZED USERS FOR AUTHORIZED USE ONLY*
    *   UNAUTHORIZED ACCESS IS STRICTLY PROHIBITED AND MAY BE PUNISHED    *
    * * * * * * * * * * * * *:  W A R N I N G  :* * * * * * * * * * * * * *
    *                                                          $INSTDATE *
    *           Hostname:       $HOSTNAME                                 *
    *           OS:             $OSRELASE                                 *
    *           HW:             $OSDETECT                                 *
    *           IP:             $IPADDRES                                 *
    *                                                                     *
    *           Contact:        Company                                   *
    *                           Street                                    *
    *                           Country                                   *
    *           Administrator:  NAME                                      *
    *                                                                     *
    *           State:          Productive                                *
    ***********************************************************************
End-of-Message

Should be looks
Code:
oot@localhost's password: 
Last login: Sun Apr 18 19:22:52 2010 from localhost

    * * * * * * * * * * * * *:  W A R N I N G  :* * * * * * * * * * * * * *
    *THIS SYSTEM IS RESTRICTED TO AUTHORIZED USERS FOR AUTHORIZED USE ONLY*
    *   UNAUTHORIZED ACCESS IS STRICTLY PROHIBITED AND MAY BE PUNISHED    *
    * * * * * * * * * * * * *:  W A R N I N G  :* * * * * * * * * * * * * *
    *							       2010-04-18 *
    *		Hostname:	localhost.localdomain			  *
    *		OS:		Fedora release 12 (Constantine)		  *
    *		HW:		i-have-to-define	 		  *
    *		IP:		10.10.10.10				  *
    *									  *
    *		Contact:	Company          			  *
    *				Street          	                  *
    *				Country     				  *
    *		Administrator:	NAME					  *
    *									  *
    *		State:		Productive				  *
    ***********************************************************************
thx
 
Old 04-18-2010, 12:56 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Use awk, with printf and formatting.
 
Old 04-18-2010, 12:58 PM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Just a thought: instead of cat you may try printf and format every line of the message accordingly. For example, the OS line should be given by
Code:
printf "    *           %-16s%-42s*\n" "OS:" "$OSRELASE"
An additional note: to retrieve the OS field, the "cat /etc/*release" is not a strong method. For example on my OpenSuse I have two files:
Code:
$ ls /etc/*release
/etc/lsb-release  /etc/SuSE-release
where the first line of the first file is
Code:
LSB_VERSION="core-2.0-noarch:core-3.2-noarch:core-2.0-ia32:core-3.2-ia32"
maybe not what you want. You can try with the following command, which is available on most linux distributions nowadays:
Code:
lsb_release -sd
Moreover take in mind that on some systems the file is not called something-release, but something-version.
 
Old 04-18-2010, 01:14 PM   #4
geforce
LQ Newbie
 
Registered: Apr 2010
Posts: 5

Original Poster
Rep: Reputation: 1
Thx colucix, verry helpfull.

i try with awk and printf with variables but the solution to use printf line per line terrific!!. So simple that overlooked. Thx Thx


On my Fedora I have 3 Files but in all the Files are the same. So i make this pipe head -1... on my CentOS and RedHat's only one File.
I'm verry glad that you note me about this files.

Code:
[root@localhost ~]# ls /etc/*release
/etc/fedora-release  /etc/redhat-release  /etc/system-release

Code:
[root@localhost ~]# cat /etc/*release
Fedora release 12 (Constantine)
Fedora release 12 (Constantine)
Fedora release 12 (Constantine)
 
Old 04-18-2010, 01:21 PM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
So this is solved? Please mark it as such.
 
Old 04-18-2010, 01:24 PM   #6
geforce
LQ Newbie
 
Registered: Apr 2010
Posts: 5

Original Poster
Rep: Reputation: 1
Hi Tinkster..
i try and if there some problems i will ask the gurus again.
when i finished i will post the soultion and then will mark as solved.

thx
 
Old 04-18-2010, 04:26 PM   #7
geforce
LQ Newbie
 
Registered: Apr 2010
Posts: 5

Original Poster
Rep: Reputation: 1
special thanks to colucix

I have now my Script. Works very well.
First i want to use the /etc/*release. For my Company works.
But in future if I know which i have to use i will change.

the "lsb_release -sd" command is not implement on my system, so it is not the better solution.

THX ALL



Code:
#!/bin/bash

FILE="/home/user/tmp/motd"
INSTDATE=$(date +%F)
HOSTNAME=$(hostname)
OSRELASE=$(cat /etc/*release | head -1)
OSDETECT="i-have-to-define"
#IPADDRES=$(ifconfig eth0 | grep -o '[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*' | head -1)
ETHERADD=$(ifconfig |grep -e ^[a-z] |grep -v lo | awk '{ printf $1 FS}')


if [ -w $FILE ]
	then
		if [[ -n `egrep -o '[0-9]+++-[0-9]++-[0-9]++' $FILE` ]]
		then
			IDATE=$(egrep -o '[0-9]+++-[0-9]++-[0-9]++' $FILE)
			echo "$FILE was in past $IDATE altered" 
			echo "Will be saved as .rpmsave"
			FILE=$FILE.rpmsave
		fi
	else
	    	logger -p local0.warn -t ME "`id -un` is not allowed to edit $FILE"
	    	echo "File is not Writeable. You are Not allowed to chance Motd"
		echo "Will be saved as .rpmsave"
		FILE=$FILE.rpmsave
fi


cat << End-of-Message > $FILE
`echo -en "\033[1;31m"`
    * * * * * * * * * * * * *:  W A R N I N G  :* * * * * * * * * * * * * *
    *THIS SYSTEM IS RESTRICTED TO AUTHORIZED USERS FOR AUTHORIZED USE ONLY*
    *   UNAUTHORIZED ACCESS IS STRICTLY PROHIBITED AND MAY BE PUNISHED    *
    * * * * * * * * * * * * *:  W A R N I N G  :* * * * * * * * * * * * * *
    *							       $INSTDATE *
End-of-Message

printf "    *           %-16s%-42s*\n" "Hostname:" "$HOSTNAME" >>$FILE
printf "    *           %-16s%-42s*\n" "OS:"	   "$OSRELASE" >>$FILE
printf "    *           %-16s%-42s*\n" "HW:" 	   "$OSDETECT" >>$FILE
#printf "    *           %-16s%-42s*\n" "IP:" 	   "$IPADDRES" >>$FILE

for i in $ETHERADD
do
	IP=$(ifconfig $i | grep -o '[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*' | head -1)
	printf "    *           %-16s%-42s*\n" "IP $i"      "$IP" >>$FILE
done

cat << End-of-Message >> $FILE
    *									  *
    *		Contact:	Company		 			  *
    *				Street		 			  *
    *				Country		 			  *
    *		Administrator:	NAME					  *
    *									  *
    *		Status:		Productive				  *
    ***********************************************************************
`echo -en "\033[0m"`
End-of-Message
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
motd or login banner per user maginotjr Linux - Server 9 03-09-2012 01:08 PM
how to avoid motd banner after successfull ftp login sebasyan Linux - Server 2 11-19-2009 11:08 PM
Which script overwrites /ect/motd? Nerox Debian 6 12-29-2004 02:25 PM
motd script or something like that linuxtesting2 Linux - General 2 06-09-2004 02:13 PM
change the banner for ssh [cacheflow] Linux - Security 5 09-16-2002 03:03 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:57 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration