-1
import subprocess
import ipaddress
import pygsheets
from subprocess import Popen, PIPE
from pprint import pprint
from googleapiclient import discovery

#def append_table(start='A1', end=None, values=None, dimension='ROWS', 
overwrite=False)

gc = pygsheets.authorize(service_file="testProj.json")
source_spreadsheet = 
gc.open_by_key("1Mc1RbfPZIbxmCiqlRJt_AWJVasrGqA7iLmUkuHHL3tw")
wks = source_spreadsheet.sheet1


network = ipaddress.ip_network('10.70.60.0/23')


for i in network.hosts():
i=str(i)
toping = Popen(['ping', '-c', '3', i], stdout=PIPE)
output=toping.communicate()[0]
hostalive = toping.returncode
if hostalive == 0:
    print (i, "Pings")
else:
    print (i, "Doesn't ping")

I was able to read from the file, I just want to figure out how I can write to it. For example column A will list all the IP's in the subnet and column B will print out if the IP pings or doesn't

tehhowch
  • 9,645
  • 4
  • 24
  • 42
  • I'm quite sure there is example code that shows how to write to a Google Sheet with both pygsheets and gspread - were you able to perform those examples? – tehhowch Jul 19 '18 at 20:42
  • Yeah I have went through many examples but I am struggling to get it to work I get many syntax errors, I am new to python – Brad John Hughes Jul 19 '18 at 20:44
  • Well, you need to include your effort at solving the issue in your code here. Being new to something doesn't mean you shouldn't still try to solve it. When you include your attempt at writing, make sure that the rest of the code is valid Python - you have at least one `IndentationError` in what you've posted here. – tehhowch Jul 19 '18 at 21:02
  • lol I have been trying for 8 hours today! – Brad John Hughes Jul 19 '18 at 21:18
  • 1
    What happens when you try writing to the file? Do you see any kind of error output? If so, including the error would be a good start to making this question more answerable. – Richie Thomas Jul 19 '18 at 23:05

1 Answers1

-1

I think you need to give permission to your program to write in sheets , In your sheet there must be sharing option with writing permision , you have to insert email id , you get that mail id in credentials JSON file which you get when you make your api key

Note :- i am new to Python , new to stackoverflow as well , not yet done much with google sheets , but while learning to ,connect to google sheets i saw a video that i need to put the mail id in sharing

  • At this point it's impossible to identify the issue OP is having writing to their desired Google Sheets file, because the code they have provided does not even attempt to write to a Google Sheets file. It does, however, appear to have a Service Account authorization step, and OP does indicate that the file is at least readable by the service account. – tehhowch Jul 19 '18 at 21:21
  • yes it is readable I created dummy text in the sheet and ran the script and it did output the result but I removed that line from the code because I am trying to write not read – Brad John Hughes Jul 19 '18 at 21:23
  • my issue basically is I don't know how to modify my code so when I run the script it dumps the output into the sheet – Brad John Hughes Jul 19 '18 at 21:23
  • Try checking out the google official docs for google sheets using python https://developers.google.com/sheets/api/quickstart/python – Chetan Rakheja Jul 19 '18 at 21:27