I am trying to run a python script from bash script to upload files to a website using crontab. The script runs from terminal but doesn't work from crontab and the error is
"Error: <urlopen error [Errno -2] Name or service not known>"
The python script is:
#!/usr/bin/env python
from ConfigParser import ConfigParser
from mechanize import Browser, RobustFactory
import base64
from os.path import basename
import sys
import os
print(os.environ)
if __name__ == "__main__":
if len(sys.argv) != 2:
print "Error: 1 argument needed"
sys.exit(1)
#DATA FROM CFG FILE
url = "http:website"
cfn = "absolute_path/upload.cfg"
cfg = ConfigParser()
try:
cfg.read(cfn)
usr = cfg.get('Auth', 'user')
pwd = cfg.get('Auth', 'pass')
except Exception, e:
print "Error:", e
sys.exit(1)
if not usr or not pwd:
print "Error: username or password not valid."
sys.exit(1)
filename = sys.argv[1]
try:
br = Browser(factory=RobustFactory())
br.addheaders.append(('Authorization', 'Basic %s' % base64.encodestring('%s:%s' % (usr, pwd))))
br.open(url)
br.select_form(nr=1)
br.form.add_file(open(filename, 'rb'), 'text/plain', basename(filename))
br.submit()
print "File '%s' successfully uploaded" % filename
sys.exit(0)
except Exception, e:
print "Error:", e
sys.exit(1)
When I try to "print(os.environ)" in python script in the case of manual run and using crontab:
with crontab :
"{'MAILTO': '/var/mail/envclim', 'LANG': 'en_US.UTF-8', 'SHELL': '/bin/sh', 'XDG_RUNTIME_DIR': '/run/user/1000', 'SHLVL': '2', 'PYTHONPATH': '/usr/lib/python2.7/site-packages:/usr/lib/python2.7/site-packages/', 'OLDPWD': '/home/envclim/upload_sdswas', 'PWD': '/home/envclim/upload_sdswas/upload_scripts', 'LOGNAME': 'envclim', 'USER': 'envclim', 'HOME': '/home/envclim', 'PATH': '/bin:/usr/bin:/usr/local/bin:/usr/local/CDO/bin', 'XDG_SESSION_ID': '6', '_': '/usr/bin/python'}"
but in the manual case , there is the proxy, as the following:
{'HTTP_PROXY': 'http://10.51.51.51:80/', 'KDE_IS_PRELINKED': '1', 'ALL_PROXY': 'socks://10.51.51.51:80/', 'NO_PROXY': 'localhost,127.0.0.0/8,::1', 'GJS_DEBUG_OUTPUT': 'stderr', 'http_proxy': 'http://10.51.51.51:80/', 'FTP_PROXY': 'http://10.51.51.51:80/',----------}
In crontab, I add :
SHELL=/bin/sh
PATH=/bin:/usr/bin
PYTHONPATH=/usr/lib/python2.7/site-packages
MAILTO=/var/mail/envclim
I am using fedora19. I tried to disabled proxy, but the script didn't work at all.
please, can anyone solve this problem?
Thanks in advance
Zeinab
Thanks alot for your comments, I solved this problem by adding these lines in crontab:
PYTHONPATH=/usr/lib/python2.7/site-packages
HTTP_PROXY=http://10.51.51.51:80/
and in the bash file that used to run the python script:
export PYTHONPATH=$PYTHONPATH:/usr/lib/python2.7/site-packages/
export LD_LIBRARY_PATH=/lib:/usr/lib:/usr/local/lib:/usr/local/usr/lib
Related
Paramiko Script is hanging, attempting to ssh through a list of IP address and execute a few commands to extract information that may or may not be there. For example if i was to implement a uname -a it should reveal the hostname, however if the next server in the list doesn't have a hostname then it seems the script is hanging.
import paramiko
import sys
import io
import getpass
import os
import time
# ***** Open Plain Text
f = open("file.txt")
# ***** Read & Store into Variable
hn=(f.read().splitlines())
f.close()
# ***** Credentials
username = raw_input("Please Enter Username: ")
password = getpass.getpass("Please Enter Passwod: ")
# ***** SSH
client=paramiko.SSHClient()
def connect_ssh(hn):
try:
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hn, 22, username, password, look_for_keys=False, allow_agent=False)
print 'Connection Attempting to: '+(hn)
channel = client.get_transport().open_session()
channel.invoke_shell()
#print "CMD
#channel.send("CMD"+"\n")
channel.send("command"+"\n")
CMD = (channel.recv(650000))
print (CMD)
except Exception, e:
print '*** Caught exception: %s: %s' % (e.__class__, e)
try:
channel.close()
except:
pass
# *****Create Loop through input.txt
for x in hn:
connect_ssh(x)
Based on the example multiprocess using pathos and dill I hit the wall of cPickle errors. I did as www3cam did; with a little addition.. by removing pathos, dill, multiprocess and pyreadline. Then hit pip install pathos --no-cache-dir after carefully removed those side-packages mentioned above. Fresh install, cold-reboot of PC and an Anacoda2 update later... the jar with cPickles... is still there and Frank Zappa keeps singing his "Titties and Beer" song.
The modified code from 1:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os, sys
import datetime, time
import multiprocessing
import pathos.pools as pp
import dill
class Multiprocess(object):
def __init__(self):
pass
def qmp_worker(self,(inputs, the_time)):
print " Processs %s\tWaiting %s seconds" % (inputs, the_time)
time.sleep(int(the_time))
print " Process %s\tDONE" % inputs
def qmp_handler(self): # Non tandem pair processing
pool = pp.ProcessPool(2)
pool.map(self.qmp_worker, data)
def mp_worker((inputs, the_time)):
print " Process %s\tWaiting %s seconds" % (inputs, the_time)
time.sleep(int(the_time))
print " Process %s\tDONE" % inputs
sys.stdout.flush()
def mp_handler(): # Non tandem pair processing
p = multiprocessing.Pool(2)
p.map(mp_worker, data)
def mp_handler_tandem():
subdata = zip(data[0::2], data[1::2])
# print subdata
for task1, task2 in subdata:
p = multiprocessing.Pool(2)
p.map(mp_worker, (task1, task2))
#data = (['a', '1'], ['b', '2'], ['c', '3'], ['d', '4'])
data = (['a', '2'], ['b', '3'], ['c', '1'], ['d', '4'],
['e', '1'], ['f', '2'], ['g', '3'], ['h', '4'])
if __name__ == '__main__':
sys.stdout.flush()
# print 'mp_handler():'
# mp_handler()
# print '---'
# time.sleep(2)
# print '\nmp_handler_tandem():'
# mp_handler_tandem()
print '---'
# time.sleep(2)
Multiprocess().qmp_handler()
Perhaps something should be set like with the example from here "catching and printing PyQt5 tracebacks?
Snippet code below that made me thinking and curious if something like that has to be done done for dill?
# Back up the reference to the exceptionhook
sys._excepthook = sys.excepthook
# Set the exception hook to our wrapping function
sys.excepthook = my_exception_hook
Latest error:
File "c:\python\anac2\lib\site-packages\pathos\multiprocessing.py", line 137, in map
return _pool.map(star(f), zip(*args)) # chunksize
File "c:\python\anac2\lib\site-packages\multiprocess\pool.py", line 251, in map
return self.map_async(func, iterable, chunksize).get()
File "c:\python\anac2\lib\site-packages\multiprocess\pool.py", line 567, in get
raise self._value
cPickle.PicklingError: Can't pickle : attribute lookup builtin.function failed
More info about this error is at: my cPickle dump.
Trying to read an .ini db file from python to test a connection to a PostGres database
Python Version 2.7.11
In Fedora, I installed with
sudo dnf install python-configparser
Install 1 Package
Total download size: 41 k
Installed size: 144 k
Is this ok [y/N]: y
Downloading Packages:
python-configparser-3.5.0b2-0.2.fc23.noarch.rpm 40 kB/s | 41 kB 00:01
Total 31 kB/s | 41 kB 00:01
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Installing : python-configparser-3.5.0b2-0.2.fc23.noarch 1/1
Verifying : python-configparser-3.5.0b2-0.2.fc23.noarch 1/1
Installed:
python-configparser.noarch 3.5.0b2-0.2.fc23
in my config.py I do a
import configparser
when I run my script I get 'module' object is not callable
db.ini
[test1]
host=IP address
database=db
port=5432
user=username
password=pswd
[test2]
host=localhost
database=postgres
port=7999
user=abc
password=abcd
config.py
#!/usr/bin/env python
#from configparser import ConfigParser
import configparser
def config(filename='database.ini', section='gr'):
# create a parser
parser = configparser()
# read config file
parser.read(filename)
# get section, default to gr
db = {}
if parser.has_section(section):
params = parser.items(section)
for param in params:
db[param[0]] = param[1]
else:
raise Exception('Section {0} not found in the {1} file'.format(section, filename))
return db
testing.py
#!/usr/bin/env python
import psycopg2
from config import config
def connect():
""" Connect to the PostgreSQL database server """
conn = None
try:
# read connection parameters
params = config()
# connect to the PostgreSQL server
print('Connecting to the PostgreSQL database...')
conn = psycopg2.connect(**params)
# create a cursor
cur = conn.cursor()
# execute a statement
print('PostgreSQL database version:')
cur.execute('SELECT version()')
# display the PostgreSQL database server version
db_version = cur.fetchone()
print(db_version)
# close the communication with the PostgreSQL
cur.close()
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
conn.close()
print('Database connection closed.')
if __name__ == '__main__':
connect()
[root#svr mytest]# ./testing.py
'module' object is not callable
any ideas ?
thank you.
You have an error in your parser creation, see the ConfigParser examples:
An example of reading the configuration file again:
import ConfigParser
config = ConfigParser.RawConfigParser()
config.read('example.cfg')
In Python, a module is basically just a file, so if you want to use anything from this module, you have to specify what from the module you want. In your case, change the lines to
# create a parser
parser = configparser.ConfigParser()
This way, you are using the class ConfigParser from the module.
You can also use the follow to import the parser:
from configparser import ConfigParser
instead of
import configparser
I had the same issue as you and this worked for me.
need some help making the below a callable object for another script e.g scrape.run()
I'm not sure where to start any pointers?
scrape.py
from clint.textui import puts, colored
import subprocess, sys
from datetime import datetime
from time import sleep as sleep
today = datetime.now().strftime("%H:%M:%S")
multimon_ng = subprocess.Popen("rtl_fm -A lut -s 22050 -f 148.81250M | multimon-ng -t raw -a FLEX -f alpha /dev/stdin",
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=True)
while True:
try:
nextline = multimon_ng.stdout.readline()
if not nextline:
pass
else:
try:
flex, mdate, mtime, bitrate, other, capcode, o2, msg = nextline.split(" ", 7)
except ValueError:
print "invalid line", nextline
else:
puts(colored.yellow(capcode), newline=False)
puts(" [", newline=False)
puts(colored.green(mdate + " " + str(datetime.now().strftime('%H:%M'))), newline=False)
puts(" ]", newline=False)
puts(msg)
except KeyboardInterrupt:
print "exit"
multimon_ng.poll()
Add this to the bottom of the script.
if __name__ == "__main__":
function_you_wish_to_call_here()
Anything under the if statement will run when you call the script directly.
I have the following code:
import paramiko
import time
import re
import sys
import random
import fileinput
ip_address = raw_input("Enter a valid WAP IP: ")
#Open SSHv2 connection to devices
def open_ssh_conn(ip):
try:
#Logging into device
session = paramiko.SSHClient()
#AutoAddPolicy
session.set_missing_host_key_policy(paramiko.AutoAddPolicy())
#Passing the necessary
session.connect(ip, username = 'myUsername', password = 'myPassword')
#Start an interactive shell session on the switch
connection = session.invoke_shell()
#Commands
connection.send("enable\n")
time.sleep(1)
connection.send("show version\n")
time.sleep(1)
#Checking command output for IOS syntax errors
output = connection.recv(65535)
#Checking command output for IOS Syntax errors
if re.search(r"% Invalid input detected at", output):
print "* There was at least one IOS syntax error on device %s" % ip
else:
print "\nDONE for device %s" % ip
#Test for reading command output
print output + "\n"
#Closing the connection
session.close()
except paramiko.AuthenticationException:
print "* Invalid username or password. \n* Please check
the username/password file or the device configuration!"
print "* Closing program...\n"
#Calling the SSH function
open_ssh_conn(ip_address)
How can I test multiple credential without getting kick out of the program when an exception is caught?
for example, try this new credentials:
session.connect(ip, username = 'myNewUsername', password = 'myNewPassword')
I figured it out! I created a nested list with the credentials:
list = [['username1', 'password1'], ['username2', 'password2'], \
['username3', 'password3']]
Then, created a for loop and put my code inside:
for elem in list:
my code...
# this is the connect line:
session.connect(ip, username = elem[0], password = elem[1])
That did it!!!!