Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help manipulating and displaying text

Status
Not open for further replies.

dfezz1

Technical User
Jun 17, 2009
4
US
Hello All,

I have been working on a great script to remotely gather server info and store it in a .txt that can be imported to .xls

I have been reading the hostnames that are in the /.shh/known_hosts file so I don't have to mess with passing a password - via ssh (not easy to do , by the way, but posibble) anyway...

I have been stripping the known_hosts file of the following , trying to get a clean hosts.txt file to use as my CONFIRMED list of servers with exchanged keys.

I have removed the follow:
- ssh key
- extra charaters and extended hostnames
(e.g. myserver vs myserver_backup) So I would remove the "_backup"
- duplicates

So now I have something like the following

hostname, 192.168.1.1
hostname2, 192.168.1.2
192.168.1.3, hostname3, hostname3.yahoo.com
hostname4
hostname5, 192.168.1.5
192.168.1.6

So I am looking to manage the text. move all hostnames to the first column and ip's to the 2nd comma separated spot. And move all single column enties to the bottom hostname first then ip's, but that doesn't really matter, just need to know how to manipulate the text, I know your going to say... reg expressions, but I want the easiest way or at least a way I can repeat and reuse for separate tasks.(something I understand)

hostname, 192.168.1.1
hostname2, 192.168.1.2
hostname3, 192.168.1.3
hostname5, 192.168.1.5
hostname4
192.168.1.6

I will then create an array and print them out via the array.

Code:
"@lines = map({[split /,/]} @lines);"

Thanks
 
You haven't asked a question, just listed a bunch of requirements. Are you hoping that someone will write all the code to accomplish all the requirements or do you need help with a part you are stuck on, or what?

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
dfezz1 said:
I know your going to say... reg expressions, but I want the easiest way ..
What is the easiest way?
For me regex would be the easiest way, how to parse hostnames and ips out of the data lines - look:

hosts.pl
Code:
[COLOR=#804040][b]use strict[/b][/color];
[COLOR=#804040][b]use warnings[/b][/color];

[COLOR=#804040][b]my[/b][/color] [COLOR=#008080]@hostnames[/color];
[COLOR=#804040][b]my[/b][/color] [COLOR=#008080]@ips[/color];
[COLOR=#0000ff]# process _DATA_ lines[/color]
[COLOR=#804040][b]while[/b][/color]([COLOR=#008080]<DATA>[/color]){
 [COLOR=#804040][b]chomp[/b][/color];
 [COLOR=#0000ff]# extract hostname[/color]
 [COLOR=#804040][b]my[/b][/color] [COLOR=#008080]$hostname[/color] = [COLOR=#008080]$1[/color] [COLOR=#804040][b]if[/b][/color] ([COLOR=#804040][b]m/[/b][/color][COLOR=#6a5acd]([a-zA-Z][/color][COLOR=#6a5acd]\w[/color][COLOR=#6a5acd]*)[/color][COLOR=#804040][b]/[/b][/color]);
 [COLOR=#0000ff]# extract ip[/color]
 [COLOR=#804040][b]my[/b][/color] [COLOR=#008080]$ip[/color] = [COLOR=#008080]$1[/color] [COLOR=#804040][b]if[/b][/color] ([COLOR=#804040][b]m/[/b][/color][COLOR=#6a5acd]([/color][COLOR=#6a5acd]\d[/color][COLOR=#6a5acd]{1,3}[/color][COLOR=#6a5acd]\.\d[/color][COLOR=#6a5acd]{1,3}[/color][COLOR=#6a5acd]\.\d[/color][COLOR=#6a5acd]{1,3}[/color][COLOR=#6a5acd]\.\d[/color][COLOR=#6a5acd]{1,3})[/color][COLOR=#804040][b]/[/b][/color]);
 [COLOR=#804040][b]if[/b][/color] ([COLOR=#008080]$hostname[/color] && [COLOR=#008080]$ip[/color]){
   [COLOR=#0000ff]# if both $hostname and $ip exist then compose line and print[/color]
   [COLOR=#804040][b]print[/b][/color] [COLOR=#ff00ff]"[/color][COLOR=#008080]$hostname[/color][COLOR=#ff00ff], [/color][COLOR=#008080]$ip[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color]
 }
 [COLOR=#804040][b]elsif[/b][/color] ([COLOR=#008080]$hostname[/color]) {
   [COLOR=#0000ff]# if only $hostname exists then store it into @hostnames[/color]
   [COLOR=#804040][b]push[/b][/color] [COLOR=#008080]@hostnames[/color], [COLOR=#008080]$hostname[/color]
 }
 [COLOR=#804040][b]elsif[/b][/color] ([COLOR=#008080]$ip[/color]) {
   [COLOR=#0000ff]# if only $ip exists then store it into @ips [/color]
   [COLOR=#804040][b]push[/b][/color] [COLOR=#008080]@ips[/color], [COLOR=#008080]$ip[/color]
 }
}
[COLOR=#0000ff]# at end print the both lists @hostnames and @ips[/color]
[COLOR=#804040][b]foreach[/b][/color] [COLOR=#804040][b]my[/b][/color] [COLOR=#008080]$hostname[/color] ([COLOR=#008080]@hostnames[/color]){
  [COLOR=#804040][b]print[/b][/color] [COLOR=#ff00ff]"[/color][COLOR=#008080]$hostname[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color];
}
[COLOR=#804040][b]foreach[/b][/color] [COLOR=#804040][b]my[/b][/color] [COLOR=#008080]$ip[/color] ([COLOR=#008080]@ips[/color]){
  [COLOR=#804040][b]print[/b][/color] [COLOR=#ff00ff]"[/color][COLOR=#008080]$ip[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color];
}


[COLOR=#0000ff]__DATA__[/color]
[COLOR=#0000ff]hostname, 192.168.1.1[/color]
[COLOR=#0000ff]hostname2, 192.168.1.2[/color]
[COLOR=#0000ff]192.168.1.3, hostname3, hostname3.yahoo.com[/color]
[COLOR=#0000ff]hostname4[/color]
[COLOR=#0000ff]hostname5, 192.168.1.5[/color]
[COLOR=#0000ff]192.168.1.6[/color]
Output:
Code:
c:\Users\Roman\Work>perl hosts.pl
hostname, 192.168.1.1
hostname2, 192.168.1.2
hostname3, 192.168.1.3
hostname5, 192.168.1.5
hostname4
192.168.1.6
 
Not all the lines in the file have an IP address, and not all have a dns name. The format of known_hosts allows either. If all you are trying to do is get list of hostnames and the IP addresses they point to, wouldn't it be easier to just read them and look them up with gethostbyname or gethostbyaddr?

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
That worked like a charm! Thanks alot I am going to add in "SORT" and that should be complete
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top