Using xmlrpclib from Python2 it give back OpaqueRef but I would like to get more informations like the status of all VMs and their names, bandwidth, VCPU
#!/usr/bin/env python
import xmlrpclib
xen = xmlrpclib.Server('http://xen.server.address')
session = xen.session.login_with_password('user', '*****')['Value']
all_vms = xen.VM.get_all(session)['Value']
print(all_vms)
I have tried to works on the status part but do not find the way to print the status of all.
For one VM :
power_state = (records['OpaqueRef:***']['power_state'])
print 'KO' if power_state == 'Halted' else 'OK'
But get stuck on doing that for all VMs of the Host
Related
I made a script that accepts an IP address as input and checks all of the ARP tables across all our layer three networking devices and searches for a match, and returns the mac address if one is found. Works great, but I've seen a bunch of different scripts here, github and reddit that can do this on multiple devices at once, significantly cutting the time. I've tried to copy one by making an updated script with limited success: it seems to execute the commands perfectly on some device types (IOS/XE) and not on others (ASA/HP ProCurve). Specifically, the HP devices seem to be getting stuck running the terminal width command that Netmiko customarily runs, while the ASA devices appear to run the arp command but have an issue with TextFSM parsing the output. Even still, I figure it's an issue with something I did because it works fine on all of them in my original script that features the same block of netmiko code. Here is the first function:
'''
Use Netmiko to execute show arp.
Return the result as a dictionary.
'''
global address
global addressFound
ios_device = {
'device_type': 'cisco_ios',
'ip': ip_add, #function argument
'username': 'redadmin',
'password': password,
'secret': password,
}
#more dictionaries for each device type follow
output_dict = {}
for ip_add in ios_device_list:
if addressFound == True:
break
remote_conn = ConnectHandler(**ios_device)
remote_conn.enable()
hostname = remote_conn.find_prompt()[:-1]
output = remote_conn.send_command("show ip arp", use_textfsm=True)
remote_conn.disconnect()
output_dict[hostname] = pd.DataFrame(output)
if address in output_dict[hostname].values:
print('A result was found on ' + hostname + ' (' + ip_add + '):')
print(output_dict[hostname].loc[output_dict[hostname]['address'] == address, 'mac'].item())
addressFound = True
break
#more if statements for each device type follow
Here is the main function:
global address
address = input('Enter the address you would like to search for: ')
global addressFound
'''
Use processes and Netmiko to connect to each of the devices. Execute
'show version' on each device. Use concurrent futures built-in queue
to pass the output back to the parent process. Record the amount of
time required to do this.
'''
start_time = datetime.now()
# Start a ThreadPool (or ProcessPool if you change Thread to Process)
# Using 5 workers (threads/processes) simultaneously
with cf.ThreadPoolExecutor(max_workers=20) as executor:
# Start the Netmiko operation and mark each future with its device dict
future_to_ios_device = {
executor.submit(show_version, ip_add): ip_add
for ip_add in ios_device_list
}
#more dictionaries for each device type follow
future_to_devices = [future_to_ios_device, future_to_asa_device, future_to_hp_device, future_to_xe_device]
# Do something with the results as they complete. Could be a print,
# a database write, or write to a CSV to store for later use
for futures in future_to_devices:
for future in cf.as_completed(futures):
device = futures[future]
try:
data = future.result()
except Exception as exc:
print("{} generated an exception: {}".format(device, exc))
The biggest difference I gather is that my script features a list of ip addresses vs a list of devices as in the case of the original (and many others I've seen). I'm pretty new to Python, so any assistance is greatly appreciated.
On Google IT automation with python specialization "Using python interact with Operating System"'s week 1 Qwiklabs Assessment: Working with Python 3rd module is not properly work
on ~/scripts directory :
network.py code
#!usr/bin/env python3
import requests
import socket
def check_localhost():
localhost = socket.gethostbyname('localhost')
print(localhost)
if localhost == '127.0.0.1':
return True
return False
def check_connectivity():
request = requests.get("http://www.google.com")
responses = request.status_code
print(responses)
if responses == 200:
return True
return False
By using this code I make "Create a new Python module
"
but Qwiklab show me that I cannot properly write code.!!!
now what is the problem?
I am responding with this piece here because I noticed a lot of folks taking this course "Using Python to interact with the Operating System" on Coursera do have similar issues with writing the Python function check_localhost and check_connectivity. Please, copy these functions to your VM and try again.
To ping the web and also check whether the local host is correctly configured, we will import requests module and socket module.
Next, write a function check_localhost, which checks whether the local host is correctly configured. And we do this by calling the gethostbyname within the function.
localhost = socket.gethostbyname('localhost')
The above function translates a hostname to IPv4 address format. Pass the parameter localhost to the function gethostbyname. The result for this function should be 127.0.0.1.
Edit the function check_localhost so that it returns true if the function returns 127.0.0.1.
import requests
import socket
#Function to check localhost
def check_localhost():
localhost = socket.gethostbyname('localhost')
if localhost == "127.0.0.1":
return True
else:
return False
Now, we will write another function called check_connectivity. This checks whether the computer can make successful calls to the internet.
A request is when you ping a website for information. The Requests library is designed for this task. You will use the request module for this, and call the GET method by passing a http://www.google.com as the parameter.
request = requests.get("http://www.google.com")
This returns the website's status code. This status code is an integer value. Now, assign the result to a response variable and check the status_code attribute of that variable. It should return 200.
Edit the function check_connectivity so that it returns true if the function returns 200 status_code.
#Function to check connectivity
def check_connectivity():
request = requests.get("http://www.google.com")
if request.status_code == 200:
return True
else:
return False
Once you have finished editing the file, press Ctrl-o, Enter, and Ctrl-x to exit.
When you're done, Click Check my progress to verify the objective.
I was also using a similar code, although It was getting executed fine in the lab terminal, however, it was not getting successfully verified. I contacted the support team using support chat and they provided a similar but relatively efficient code that worked ;
#!/usr/bin/env python3
import requests
import socket
localhost = socket.gethostbyname('localhost')
request = requests.get("http://www.google.com")
def check_localhost():
if localhost == "127.0.0.1":
return True
def check_connectivity():
if request.status_code == 200:
return True
I use the same code of yours to check what's the problem in the code, but your code successfully passed the qwiklabs check.
I think there is something wrong, Did you retry again by end this lab session and create another session just to check if this is something wrong with their end.
It's so easy, in this case, the shebang line would be /usr/bin/env python3.
you type a wrong shebang line:
#!usr/bin/env python3
But you should type:
#!/usr/bin/env python3
Add a shebang line to define where the interpreter is located.
It can be written like this
#!/usr/bin/env python3
import requests
import socket
def check_localhost():
localhost = socket.gethostbyname('localhost')
return localhost == "127.0.0.1"
def check_connectivity():
request = requests.get("http://www.google.com")
return request.status_code() == 200
This script does work even if you are facing the issue in the post:
import requests
import socket
def check_localhost():
localhost = socket.gethostbyname('localhost')
return True # or return localhost == "127.0.0.1"
def check_connectivity():
request = requests.get("http://www.google.com")
return True #or return request==200
What could be wrong with your code? The verification system is faulty and doesn't accept:
-tab instead of 4 spaces as right identation
-spaces between lines
#!usr/bin/env python3
import requests
import socket
def check_localhost():
localhost = socket.gethostbyname('localhost')
print(localhost)
if localhost == '127.0.0.1':
return True
def check_connectivity():
request = requests.get("http://www.google.com")
responses = request.status_code()
print(responses)
if responses == '200':
return True
#!/usr/bin/env python3
import requests
import socket
def check_localhost():
localhost = socket.gethostbyname('localhost')
if localhost == "127.0.0.1":
return True
def check_connectivity():
request = requests.get("http://www.google.com")
if request.status_code() == 200:
return True
the following script is working perfectly fine for IPv4 but however my job is to do same for IPv6..
#!/usr/bin/python
import pyping
response = pyping.ping('8.8.8.8')
if response.ret_code == 0:
print("reachable")
else:
print("unreachable")
is there any way.. i tried to install aioping or aio_ping.. but didnt work,.. is there any workaround to run the same as above for IPv6 in linux machines
Using the example from the multi-ping (pip install multiping) documentation:
from multiping import MultiPing
# Create a MultiPing object
mp = MultiPing(["2a00:1450:4005:803::200e"])
# Send the pings to those addresses
mp.send()
# With a 1 second timout, wait for responses (may return sooner if all
# results are received).
responses, no_responses = mp.receive(1)
if responses:
print("reachable: %s" % responses)
if no_responses:
print("unreachable: %s" % no_responses)
Have a look at the documentation to see how responses/no_responses are structured and how to ping multiple addresses simultaneously.
I have python script which run during the start of the linux VM:
I have added it in the chkconfig 345
script is supposed to check the hostname and if it is localhost.localdom then it should exit
#!/usr/bin/python
import subprocess,platform,os,sys,logging,shlex
system_name = os.getenv('HOSTNAME')
if system_name == 'localhost.localdom':
logging.info('Please correct host name for proxies, it is showing localhost')
sys.exit()"
if I run it manually it works fine. But during the startup process, even though the hostname is localhost.localdom. It does not exit.
so it look like during the boot process,
os.getenv('HOSTNAME')
is not returning the localshot.localdom what I have set in condition.
Please help getting this to work during reboot.
Thanks,
Jitendra Singh
posting an answer using the info in Isedev's comment...
you could try getting the hostname by:
import os
system_name = os.popen('/bin/hostname')
if system_name.read().rstrip() == 'localhost.localdom':
logging.info('Please correct host name for proxies, it is showing localhost')
sys.exit()
I'm trying to write a program that outputs data that can be served over a network with avahi. The documentation I've looked at seems to say I have to register the service with dbus and then connect it to avahi, but the documentation to do this is pretty sparse. Does anyone know of good documentation for it? I've been looking at these:
python-dbus:
http://dbus.freedesktop.org/doc/dbus-python/doc/tutorial.html#exporting-objects
python-avahi:
http://www.amk.ca/diary/2007/04/rough_notes_python_and_dbus.html
I'm really unfamiliar with how avahi works at all, so any pointers would be helpful.
I realise this answer is pretty late, considering your question was asked four years ago. However, it might help others.
The following announces a service using avahi/dbus:
import avahi
import dbus
from time import sleep
class ServiceAnnouncer:
def __init__(self, name, service, port, txt):
bus = dbus.SystemBus()
server = dbus.Interface(bus.get_object(avahi.DBUS_NAME, avahi.DBUS_PATH_SERVER), avahi.DBUS_INTERFACE_SERVER)
group = dbus.Interface(bus.get_object(avahi.DBUS_NAME, server.EntryGroupNew()),
avahi.DBUS_INTERFACE_ENTRY_GROUP)
self._service_name = name
index = 1
while True:
try:
group.AddService(avahi.IF_UNSPEC, avahi.PROTO_INET, 0, self._service_name, service, '', '', port, avahi.string_array_to_txt_array(txt))
except dbus.DBusException: # name collision -> rename
index += 1
self._service_name = '%s #%s' % (name, str(index))
else:
break
group.Commit()
def get_service_name(self):
return self._service_name
if __name__ == '__main__':
announcer = ServiceAnnouncer('Test Service', '_test._tcp', 12345, ['foo=bar', '42=true'])
print announcer.get_service_name()
sleep(42)
Using avahi-browse to verify it is indeed published:
micke#els-mifr-03:~$ avahi-browse -a -v -t -r
Server version: avahi 0.6.30; Host name: els-mifr-03.local
E Ifce Prot Name Type Domain
+ eth0 IPv4 Test Service _test._tcp local
= eth0 IPv4 Test Service _test._tcp local
hostname = [els-mifr-03.local]
address = [10.9.0.153]
port = [12345]
txt = ["42=true" "foo=bar"]
Avahi is "just" a Client implementation of ZeroConfig which basically is a "Multicast based DNS" protocol. You can use Avahi to publish the availability of your "data" through end-points. The actual data must be retrieved through some other means but you would normally register an end-point that can be "invoked" through a method of your liking.