1#!/usr/bin/perl
2# Copyright 2003,2010 Hewlett Packard Development Company, L.P.
3# Modify files to allow iLO serial port redirection
4#
5#Using vspconfig.pl
6#   This script will modify the following two files:
7#  \etc\inittab
8#  \etc\securetty
9
10#     usage: vspconfig.pl [-h] -t [0|1|2|3]
11
12#          -h 	: This help message
13#          -t	: Specify terminal (ttyS)
14
15#     For information about the iLO Virtual Serial Port (VSP),
16#     refer to:
17#           Integrated Lights-Out Virtual Serial Port
18#           configuration and operation
19
20#     This document can be found on www.hp.com.
21
22use Getopt::Std;
23use vars qw/ %opt /;
24
25# VSP document name
26my $vspDoc = "\tIntegrated Lights-Out Virtual Serial\n\tPort configuration and operation";
27my $webAddr = "www.hp.com";
28# Serial port session string
29my $iStr = "";
30my $iStr0 = "S";
31my $iStr1 = ":2345:respawn:/sbin/agetty 115200 ttyS";
32my $iStr2 = " vt100";
33# Non standard serial port address
34my $spStr1 = "setserial /dev/";
35my $spStr2 = " uart 16550A port 0x0408 irq 4";
36# Generic terminal string
37my $cStr = "ttyS";
38
39my @cStr;
40my @iStr;
41
42sub cmdArgs {
43	getopts( 'ht:', \%opt ) or die &usage;
44	if ($opt{h} or !%opt) {
45		&usage;
46		exit;
47	}
48	if ($opt{t} =~ /[4-9]|\d{2,}/) {
49		print "Unsupported terminal $opt{t}.\n";
50		exit;
51	}
52	for($i=0;$i<4;$i++) {
53		push(@cStr,($cStr.$i));
54		push(@iStr,($iStr0.$i.$iStr1.$i.$iStr2));
55	}
56	# concat ttyS with terminal number
57	$cStr = $cStr . $opt{t};
58	$iStr = $iStr0 . $opt{t} . $iStr1 .$opt{t} . $iStr2;
59
60}
61
62
63sub printa {
64	print @cStr;
65	print "\n";
66	print "@cStr";
67	print "\n";
68	print @cStr."";
69}
70
71sub usage {
72	print STDERR << "EOF";
73
74     usage: $0 [-h] -t [0|1|2|3]
75
76          -h 	: This help message
77          -t	: Specify terminal (ttyS)
78
79EOF
80	&info;
81	exit;
82
83}
84
85sub info {
86	print STDERR << "EOF";
87
88     For information about the iLO Virtual Serial Port (VSP),
89     refer to:
90           Integrated Lights-Out Virtual Serial Port
91           configuration and operation
92
93     This document can be found on $webAddr.
94
95EOF
96}
97
98sub remind {
99	my $str = $spStr1 . $cStr . $spStr2;
100	print STDERR << "EOF";
101
102     Server must be rebooted before file changes take effect.
103
104     For servers with a non-standard UART address, append rc.serial
105     with the following string:
106         $str;
107
108EOF
109}
110sub prev_mod {
111
112    print STDERR << "EOF";
113
114 Warning:
115    A terminal setting was detected in $_[0] on line $_[2].
116    Line $_[2] contains the following terminal setting:
117       $_[1]
118    Modify line $_[2] to the desired setting with the following:
119       $_[3]
120    or remove line $_[2] and run this script again.
121
122EOF
123}
124sub files {
125
126   my $count = 0;
127   # Open specific file
128   my $efile = "/$_[0]/$_[1]";
129   open(EF,"$efile") || die "Cannot open $efile: $!";
130
131   while($line = <EF>) {
132
133      if( $line =~ m/$_[2]/) {
134      	print "File $efile is already appended with specified terminal.\n";
135	close(EF);
136	return;
137      }
138      foreach $next (@cStr) {
139	if($line =~ /$next/) {
140	   $count++;
141	   &prev_mod($efile,$line,$count,$_[2]);
142	   return;
143	   last;
144        }
145      }
146   }
147   open(EF,">>$efile") or die "Cannot write $efile: $!";
148   # Update file with predefined text
149   print EF "$_[2]\n";
150   print "File $efile has been appended with:  $_[2]\n";
151   # Note files have changed
152   $change = 'y';
153   close(EF);
154}
155
156sub main( ) {
157    	$change;
158	system("clear");
159	&cmdArgs;
160	&files("etc","inittab",$iStr);
161	&files("etc","securetty",$cStr);
162
163	# if changes were made, send a reminder message
164	if ($change =~ /[y]/) {
165	  &remind;
166	  &info;
167	}
168  	exit;
169}
170
171&main;
172
173