Link to home
Start Free TrialLog in
Avatar of Ingo Brown
Ingo BrownFlag for Spain

asked on

To enter strings into a keyboard, press carriage return, and have the string append a remote mySQL database

Issue:
Python Script will not run because sshtunnel import to Python 3 does not seem import properly to the Pi.
I am thinking we are using the wrong version of ssh module python script for the Pi but I am struggling.  

 Hardware :
Raspberry Pi 3 Model B -  V 1.2  2015

OS Software :
pi@raspberrypi:~ $ cat /etc/*-release
PRETTY_NAME="Raspbian GNU/Linux 8 (jessie)"
NAME="Raspbian GNU/Linux"
VERSION_ID="8"
VERSION="8 (jessie)"
ID=raspbian
ID_LIKE=debian

Script:
import sshtunnel #import SSHTunnelForwarder
import mysql.connector

ID_String_Last = ["0"]

sshtunnel.SSH_TIMEOUT = 5.0
sshtunnel.TUNNEL_TIMEOUT = 5.0

with sshtunnel.SSHTunnelForwarder(
    ('**********this is the host in here **********'),
    ssh_username='', ssh_password='**********************',
    remote_bind_address=('127.0.0.1,', 3306)
) as tunnel:
    connection = mysql.connector.connect(
        user='*********username**********', password='*****************',
        host='127.0.0.1', port=tunnel.local_bind_port,
        database='northgm7_489.log_1001',
    )


Output:
Python 3.4.2 (/usr/bin/python3)
>>> %Run '17-10- 15 V6.1 Pi mySQL to Ash server.py'
Traceback (most recent call last):
  File "/home/pi/Desktop/17-10- 15 V6.1 Pi mySQL to Ash server.py", line 3, in <module>
    import sshtunnel #import SSHTunnelForwarder
ImportError: No module named 'sshtunnel'


Question:
Does anybody know what version of module is required for this platform so that it can  run?
ASKER CERTIFIED SOLUTION
Avatar of NerdsOfTech
NerdsOfTech
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Ingo Brown

ASKER

Apologies for the delay in response. I will give this a try this afternoon and revert
SSH forwarder should do the trick
We could not get the module to work so we assumed we had a OS problem. We downloaded the latest version of  UBUNTU 16.04 LTS to the rasberry pi. When I run sudo -H pip install sshtunnel , I get the following error:
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-wmbw0/pynacl/

We are struggling with the module itself it seems
we are such noobs!!
try:

sudo apt-get install python-setuptools
sudo easy_install -U setuptools
sudo pip install --upgrade setuptools
python setup.py egg_info

Open in new window

We got the modules installed & below is the script that works.

from sshtunnel import SSHTunnelForwarder
import MySQLdb

user_entry = input("Please enter user: ")

with SSHTunnelForwarder(('bo************.com', 22),
                        ssh_password='*****************!', ssh_username='***********',
                        remote_bind_address=('localhost', 3306)) as server:
    conn = MySQLdb.connect('**********t.com', '*****************c', '*****************', '**************')
    cursor = conn.cursor()
    cursor.execute("INSERT INTO log_1001(user) VALUES('%s')" % user_entry)
    cursor.execute("SELECT * FROM log_1001")
    for row in cursor.fetchall():
        print(row)
Thanks for the update!