How to do it...

We can use the paramiko module to create a remote session to a Unix machine. Then, from this session, we can read the remote machine's /proc/cpuinfo file to extract the CPU information.

Listing 6.3 gives the code for printing a remote machine's CPU information, as follows:

#!/usr/bin/env python 
# Python Network Programming Cookbook, Second Edition -- Chapter - 6 
# This program is optimized for Python 3.5.2. 
# It may run on any other version with/without modifications. 
# To make it run on Python 2.7.x, needs some changes due to API differences. 
# Follow the comments inline to make the program work with Python 2. 
 
 
import argparse 
import getpass 
import paramiko 
 
RECV_BYTES = 4096 
COMMAND = 'cat /proc/cpuinfo' 
 
def print_remote_cpu_info(hostname, port, username, password): 
    client = paramiko.Transport((hostname, port)) 
    client.connect(username=username, password=password) 
     
    stdout_data = [] 
    stderr_data = [] 
    session = client.open_channel(kind='session') 
    session.exec_command(COMMAND) 
    while True: 
        if session.recv_ready(): 
            stdout_data.append(session.recv(RECV_BYTES)) 
        if session.recv_stderr_ready(): 
            stderr_data.append(session.recv_stderr(RECV_BYTES)) 
        if session.exit_status_ready(): 
            break 
     
    print ('exit status: ', session.recv_exit_status()) 
    print (b''.join(stdout_data)) 
    print (b''.join(stderr_data)) 
     
    session.close() 
    client.close() 
 
if __name__ == '__main__': 
    parser = argparse.ArgumentParser(description='Remote file copy') 
    parser.add_argument('--host', action="store",
dest="host", default='localhost') parser.add_argument('--port', action="store",
dest="port", default=22, type=int) given_args = parser.parse_args() hostname, port = given_args.host, given_args.port user = input("Enter your remote account: ") # Comment out the above line and uncomment the
below line for Python 2.7. # user = raw_input("Enter your remote account: ") password = getpass.getpass("Enter password for %s: " %user) print_remote_cpu_info(hostname, port, user, password)

Running this script will show the CPU information of a given host, in this case, the local machine. Since my computer is 8 core, it shows the information of all the eight processors as follows (some of the processors are omitted in the following output for brevity):

$ python2 16_3_print_remote_cpu_info.py 
Enter your remote account: pradeeban
Enter password for pradeeban: 
('exit status: ', 0)
processor   : 0
vendor_id   : GenuineIntel
cpu family  : 6
model       : 60
model name  : Intel(R) Core(TM) i7-4700MQ CPU @ 2.40GHz
stepping    : 3
microcode   : 0x17
cpu MHz           : 2401.171
cache size  : 6144 KB
physical id : 0
siblings    : 8
core id           : 0
cpu cores   : 4
apicid            : 0
initial apicid    : 0
fpu         : yes
fpu_exception     : yes
cpuid level : 13
wp          : yes
flags       : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf eagerfpu pni pclmulqdq dtes64 monitor ds_cpl vmx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm epb tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid xsaveopt dtherm ida arat pln pts
bugs        :
bogomips    : 4789.08
clflush size      : 64
cache_alignment   : 64
address sizes     : 39 bits physical, 48 bits virtual
power management:
    
processor   : 1
vendor_id   : GenuineIntel
cpu family  : 6
model       : 60
model name  : Intel(R) Core(TM) i7-4700MQ CPU @ 2.40GHz
stepping    : 3
microcode   : 0x17
cpu MHz           : 2384.033
cache size  : 6144 KB
physical id : 0
siblings    : 8
core id           : 0
cpu cores   : 4
apicid            : 1
initial apicid    : 1
fpu         : yes
fpu_exception     : yes
cpuid level : 13
wp          : yes
flags       : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf eagerfpu pni pclmulqdq dtes64 monitor ds_cpl vmx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm epb tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid xsaveopt dtherm ida arat pln pts
bugs        :
bogomips    : 4789.08
clflush size      : 64
cache_alignment   : 64
address sizes     : 39 bits physical, 48 bits virtual
power management:
    
....
...
...
...
...
    
processor   : 7
vendor_id   : GenuineIntel
cpu family  : 6
model       : 60
model name  : Intel(R) Core(TM) i7-4700MQ CPU @ 2.40GHz
stepping    : 3
microcode   : 0x17
cpu MHz           : 2439.843
cache size  : 6144 KB
physical id : 0
siblings    : 8
core id           : 3
cpu cores   : 4
apicid            : 7
initial apicid    : 7
fpu         : yes
fpu_exception     : yes
cpuid level : 13
wp          : yes
flags       : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf eagerfpu pni pclmulqdq dtes64 monitor ds_cpl vmx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm epb tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid xsaveopt dtherm ida arat pln pts
bugs        :
bogomips    : 4789.08
clflush size      : 64
cache_alignment   : 64
address sizes     : 39 bits physical, 48 bits virtual
power management:
  
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.72.70