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

Ingo Brown
Ingo Brown used Ask the Experts™
on
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?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Technology Scientist
Commented:
try replacing this line
import sshtunnel #import SSHTunnelForwarder

Open in new window

with:
import sshtunnel 
from sshtunnel import SSHTunnelForwarder

Open in new window

OR, if that doesn't work try:
import paramiko
from sshtunnel import SSHTunnelForwarder

Open in new window


https://github.com/pahaz/sshtunnel/blob/master/README.rst
Ingo BrownOwner

Author

Commented:
Apologies for the delay in response. I will give this a try this afternoon and revert
NerdsOfTechTechnology Scientist

Commented:
SSH forwarder should do the trick
Ingo BrownOwner

Author

Commented:
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!!
NerdsOfTechTechnology Scientist

Commented:
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

Ingo BrownOwner

Author

Commented:
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)
NerdsOfTechTechnology Scientist

Commented:
Thanks for the update!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial