subprocess run call is not pulling the value from a variable

Viewed 10

import subprocess import sys import csv

filename = sys.argv[1] with open(filename, 'r') as servers_list: servers = csv.DictReader(servers_list) for vm in servers_list: print("---- Trying to ping Server with IPAddress ----", vm)

    reply = subprocess.run(['ping', '-c', '3', '-n', vm])

    if reply.returncode == 0:
        print('Alive')
    else:
        print('Unreachable')

This code is not working as ping is not getting VM value though print statement is getting populated with IP address, ---- Trying to ping Server with IPAddress ---- 10.1.1.9

ping: 10.1.1.9 : Name or service not known

I checked the code on python command line it is working CompletedProcess(args=['ping', '-c', '1', '-n', '192.168.1.1'], returncode=1) PING 192.168.1.2 (192.168.1.2) 56(84) bytes of data.

--- 192.168.1.2 ping statistics --- 1 packets transmitted, 0 received, 100% packet loss, time 0ms

CompletedProcess(args=['ping', '-c', '1', '-n', '192.168.1.2'], returncode=1) PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data. 64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.058 ms

--- 127.0.0.1 ping statistics --- 1 packets transmitted, 1 received, 0% packet loss, time 0ms rtt min/avg/max/mdev = 0.058/0.058/0.058/0.000 ms CompletedProcess(args=['ping', '-c', '1', '-n', '127.0.0.1'], returncode=0)

0 Answers
Related