Solved

I need a little python script to do what this bash script does

Posted on 2014-12-09
5
167 Views
Last Modified: 2016-11-23
IFS=,
while read h i m ; do
  if [ `hostname` == $h ] ; then
    ip=$i
    make=$m
 fi
done << here
fred,10.10.10.10,dell
barny,10.10.10.11,toshiba
wilma,10.10.10.12,hp
here

This works great on my linux systems

problem is that windows, aix and solaris, and linux  have only python as a common scripting language.

can someone try to do this in python ?  I am really new at python
0
Comment
Question by:TIMFOX123
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
5 Comments
 
LVL 16

Accepted Solution

by:
gelonida earned 500 total points
ID: 40490786
#!/usr/bin/env python

# following import line must be first python command in script
# it ensures python 2 / 3 compatibility
from __future__ import print_function

import socket


def get_hostname():
    return socket.gethostname()

info_by_hostname = {
'fred' : ('dell', '10.10.10.10'),
'barny': ('toshiba', '10.10.10.11'),
'wilma' : ('hp', '10.10.10.12'),
}

hostname = get_hostname()
ip, make = info_by_hostname.get(hostname, ('unknown', 'unknown'))

print(hostname, ip, make)

Open in new window

0
 
LVL 16

Expert Comment

by:gelonida
ID: 40490804
In case you have any problems on one of your platforms with the function
socket.gethostname(), you can try  one of the following alternative implementations of
get_hostname()

import platform

def get_hostname():
    return platform.node()

Open in new window

import platform

def get_hostname():
    return platform.uname()[1]

Open in new window

0
 

Author Comment

by:TIMFOX123
ID: 40491078
Hello

This is a good answer however there is another point I did not make.

info_by_hostname = {
'fred' : ('dell', '10.10.10.10'),
'barny': ('toshiba', '10.10.10.11'),
'wilma' : ('hp', '10.10.10.12'),
}


this information gets pasted in from a CSV file and creating all this formatting with the single quotes and all is not that practical. I was really hoping for it to read the data as a,b,c  

if there is not another solution a script to read the csv and format it like this will work :)  it adds another step I was hoping to avoid
0
 
LVL 16

Expert Comment

by:gelonida
ID: 40491113
Well you didn't mention that ;-)

Do you want to perform only one look up in the script or multiple ones.
Is the csv file huge (does it fit into memory)

This will impact the suggested solution.

For the time being I assume you want to perform only one lookup and that there is thus no need to keep the csv table in memory
0
 
LVL 16

Expert Comment

by:gelonida
ID: 40491125
Following code should work then.

However it is not optimized for multiple lookups as it will open the csv file for each look up.
Whether this is what you want depends on the context.

#!/usr/bin/env python

# following import line must be first python command in script
# it ensures python 2 / 3 compatibility
from __future__ import print_function

import socket
import csv

def get_hostname():
    return socket.gethostname()

def get_info_for_host(hostname, csv_fname):
    with open(csv_fname, 'rb') as fin:
        reader = csv.reader(fin, delimiter=',')
        for row in reader:
            if len(row) != 3:
                #print("ignoring row", repr(row))
                continue
            name, ip, make = row
            if hostname == name:
                return ip, make
        return ("unknown", "unknown")

hostname = get_hostname()
ip, make = get_info_for_host(hostname, 'data.csv')
print(hostname, ip, make)

Open in new window


if the script is too slow, then it could be accelerated by NOT using the csv module and doing a 'grep' like search.
On the other hand:
In most cases I prefer robust code over fastest code and the csv module can handle many particular cases of csv files, though probably not needed in your current example
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Sequence is something that used to store data in it in very simple words. Let us just create a list first. To create a list first of all we need to give a name to our list which I have taken as “COURSE” followed by equals sign and finally enclosed …
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question