how to give sftp proxy command in python script using paramiko? - python-2.7

I want to use proxy command in python using paramiko.
proxy command:
sftp -o "ProxyCommand /usr/bin/nc --proxy proxy.somegateway.com:8080 %h %p"
but when I use this directly, I am able to connect sftp server.
but if I want to use this proxy command in a Python script I get the issue below:
My script:
>>> import paramiko
>>> cmd = '/usr/bin/ssh proxy.somegateway.com:8080'
>>> proxy = paramiko.ProxyCommand(cmd)
>>> proxy
<paramiko.proxy.ProxyCommand object at 0x23b3bd0>
>>> client = paramiko.SSHClient()
>>> client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
>>> client.connect(hostname='some.host.com', username='myuser', password='secretpassword', sock=proxy)
No handlers could be found for logger "paramiko.transport"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/site-packages/paramiko/client.py", line 366, in connect
t.start_client(timeout=timeout)
File "/usr/lib/python2.7/site-packages/paramiko/transport.py", line 510, in start_client
raise e
paramiko.ssh_exception.ProxyCommandFailure: ('/usr/bin/ssh proxy.somegateway.com:8080', 'Broken pipe')
I am getting ProxyCommandFailure issue.

It should be like this:
target_host = 'sftp.foo.com'
target_port = 22
proxy = paramiko.proxy.ProxyCommand(
'/usr/bin/nc --proxy proxy.bar.com:8080 %s %d' \
% (target_host, target_port) )
client.connect(hostname=target_host, port=target_port, password='...', sock=proxy)
# ^^^^^^^^^^
See paramiko's doc for details.

Just do not forget to add hostname and port to the proxy command
target_host = 'foo'
target_port = 22
proxy = paramiko.proxy.ProxyCommand('/usr/bin/nc --proxy proxy.bar.com:8080 %s %d' % (target_hostname, target_port) )
more info here

Related

Missing environment variables in OpenShift

I am trying to set up a Python 2.7 project in Openshift. The goal is to use Tornado Websockets. I have tried a number of examples, but I keep getting stuck on a number of missing environment variables that are shown in the examples.
My current app.py:
import imp
import os
try:
zvirtenv = os.path.join(os.environ['OPENSHIFT_PYTHON_DIR'],
'virtenv', 'bin', 'activate_this.py')
execfile(zvirtenv, dict(__file__ = zvirtenv) )
except IOError:
pass
if __name__ == '__main__':
ip = os.environ['OPENSHIFT_PYTHON_IP']
port = int(os.environ['OPENSHIFT_PYTHON_PORT'])
app = imp.load_source('application', 'main.py')
app.application.listen(port , ip)
app.ioloop.IOLoop.instance().start()
My install requires:
install_requires=['tornado', 'requests', 'beautifulsoup4']
This results in the following error:
---> Running application from Python script (app.py) ...
Traceback (most recent call last):
File "app.py", line 14, in <module>
zvirtenv = os.path.join(os.environ['OPENSHIFT_PYTHON_DIR'],
File "/opt/app-root/lib64/python2.7/UserDict.py", line 40, in __getitem__
raise KeyError(key)
KeyError: 'OPENSHIFT_PYTHON_DIR'
Can anybody help me out?
You are looking for environment variables set when using OpenShift 2, but are using OpenShift 3. Under OpenShift 3 you do not need to activate the Python virtual environment, it is done for you. Your app.py should listen on all interfaces and on port 8080. That address doesn't change so long as using the default Python S2I builder, so environment variables not used to pass it in.

telnetlib and python subprocess in telnet

I am telnet into Keysight N1914A power meter and python subprocess.check_out[("Measure:Power?)] is not working. So I am trying to use the python telnetlib. I do not need username or password to log in. IP address and port number is all it needs.
There are lots of examples showing how to log in the device. My question is that how to obtain the results from the device after input commands.
For example: in the device, I type *IDN? it will result its device information; and when I type Measure:Power? it will result the power in decibel format.
import time
import csv
from string import split
import getpass
import sys
import telnetlib
import subprocess
Host = "192.168.1.10"
PORT = 5024
TIMEOUT =10
i = open('practice1.csv', 'wb')
tn = telnetlib.Telnet(Host,PORT)
print "You log in"
time.sleep(5)
while True:
#Powertemp1 = subprocess.check_output(["Measure:Power?"])
#tn.write("Measure:Power?")
tn.write("*IDN?")
Powertemp1 = tn.read_all()
print type(Powertemp1)
print Powertemp1
#Powertemp = float(Powertemp1)
#print '&s' % Powertemp
#wr = csv.writer(i, dialet = 'excel')
#wr.writerow([Powertemp])
time.sleep(5)
type(tn.read_all()) is str, but in the actual screen it is around 40 empty lines, and nothing is stored in the output text file.
Here is the result:
You log in
Traceback (most recent call last):
File "sunlook.py", line 25, in <module>
tn.write("*IDN?")
File "/usr/lib/python2.7/telnetlib.py", line 282, in write
self.sock.sendall(buffer)
File "/usr/lib/python2.7/socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 32] Broken pipe

Python script to stop Tomcat service error The specified service does not exist as an installed service

I am using a Python script to stop the Tomcat service. I am getting the error:
error: (1060, 'GetServiceKeyName', 'The specified service does not exist as an installed service.')
The method to stop the service is:
def service_info(action, machine, service):
if action == 'stop':
win32serviceutil.StopService(service, machine)
print '%s stopped successfully' % service
elif action == 'start':
win32serviceutil.StartService(service, machine)
print '%s started successfully' % service
elif action == 'restart':
win32serviceutil.RestartService(service, machine)
print '%s restarted successfully' % service
elif action == 'status':
if win32serviceutil.QueryServiceStatus(service, machine)[1] == 4:
print "%s is running normally" % service
else:
print "%s is *not* running" % service
From my base class unittest i call the method like this:
from Utilities.HelperMethods import recursive_overwrite, rename_file, delete_a_file, stop_a_service, service_info
class BaseTestCase(unittest.TestCase):
#classmethod
def setUpClass(cls):
machine = 'riaz-pc'
service_tomcat = 'Tomcat7.exe'
action_stop = 'stop'
action_start = 'start'
service_info(action_stop, machine, service_tomcat)
service_info(action_start, machine, service_tomcat)
I think I am giving the correct service name of Tomcat. In the command prompt CMD i enter tasklist and I can see Tomcat7.exe
I give Tomcat7.exe as the service name.
Why is it failing?
The full error log is:
Traceback (most recent call last):
File "C:\Webdriver\ClearCore 501\Base\BaseTestCase.py", line 35, in setUpClass
service_info(action_stop, machine, service_tomcat)
File "C:\Webdriver\ClearCore 501\Utilities\HelperMethods.py", line 134, in service_info
win32serviceutil.StopService(service, machine)
File "C:\Python27\lib\site-packages\win32\lib\win32serviceutil.py", line 409, in StopService
return ControlService(serviceName, win32service.SERVICE_CONTROL_STOP, machine)
File "C:\Python27\lib\site-packages\win32\lib\win32serviceutil.py", line 318, in ControlService
hs = SmartOpenService(hscm, serviceName, win32service.SERVICE_ALL_ACCESS)
File "C:\Python27\lib\site-packages\win32\lib\win32serviceutil.py", line 80, in SmartOpenService
name = win32service.GetServiceKeyName(hscm, name)
error: (1060, 'GetServiceKeyName', 'The specified service does not exist as an installed service.')
Thanks,
Riaz
I sense the service name is wrong. Try tomcat7 instead of Tomcat7.exe:
win32serviceutil.StopService('tomcat7', 'riaz-pc')

Error using api.update_status method in tweepy using Oauth2

Here is my code:-
I have double checked all the auth parameters.
import tweepy
CONSUMER_KEY ='#Omitted - you should not publish your actual key'
CONSUMER_SECRET ='#Omitted - you should not publish your actual secret'
ACCESS_KEY='#Omitted - you should not publish your access key'
ACCESS_SECRET = '#Omitted - you should not publish your access secret'
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)
api.update_status('Tweeting from command line')
Saved the file in home folder as status.py
after running python status.py follwing error comes:-
Traceback (most recent call last):
File "status.py", line 14, in <module>
api.update_status('Tweeting from command line')
File "/usr/local/lib/python2.7/dist-packages/tweepy-1.10-py2.7.egg/tweepy/binder.py", line 185, in _call
return method.execute()
File "/usr/local/lib/python2.7/dist-packages/tweepy-1.10-py2.7.egg/tweepy/binder.py", line 168, in execute
raise TweepError(error_msg, resp)
tweepy.error.TweepError: Could not authenticate with OAuth.
Please, help me out
I received this error under the same conditions - using tweepy, all of my keys/secrete were copy and pasted correctly. The problem was the time on my server. After running ntpdate -b pool.ntp.org I was to use tweepy just fine.
I am able to authenticate using tweepy, I have an extra line in my code though, it might help for you to change your code to this:
import tweepy
from tweepy import OAuthHandler
then proceede with the rest of your code. Also add a line in your code to print out to the shell to show your connect as follows:
print api.me().name
Make sure the line you see above this is right after api = tweepy.API(auth)
Try api.update_status(status='Tweeting from command line'). It helped me.

Using paramiko to tunnel an MySql port when django starts

I am trying to connect to a remote MySql server from my local machine.
I want to run it whenever the DEBUG constant is set to true.
Here's the script:
import select
import SocketServer
import sys
import threading
import paramiko
SSH_PORT = 22
DEFAULT_PORT = 4000
g_verbose = True
class ForwardServer (SocketServer.ThreadingTCPServer):
daemon_threads = True
allow_reuse_address = True
class Handler (SocketServer.BaseRequestHandler):
def handle(self):
try:
chan = self.ssh_transport.open_channel('direct-tcpip',
(self.chain_host, self.chain_port),
self.request.getpeername())
except Exception, e:
verbose('Incoming request to %s:%d failed: %s' % (self.chain_host,
self.chain_port,
repr(e)))
return
if chan is None:
verbose('Incoming request to %s:%d was rejected by the SSH server.' %
(self.chain_host, self.chain_port))
return
verbose('Connected! Tunnel open %r -> %r -> %r' % (self.request.getpeername(),
chan.getpeername(), (self.chain_host, self.chain_port)))
while True:
r, w, x = select.select([self.request, chan], [], [])
if self.request in r:
data = self.request.recv(1024)
if len(data) == 0:
break
chan.send(data)
if chan in r:
data = chan.recv(1024)
if len(data) == 0:
break
self.request.send(data)
chan.close()
self.request.close()
verbose('Tunnel closed from %r' % (self.request.getpeername(),))
def forward_tunnel(local_port, remote_host, remote_port, transport):
# this is a little convoluted, but lets me configure things for the Handler
# object. (SocketServer doesn't give Handlers any way to access the outer
# server normally.)
class SubHander (Handler):
chain_host = remote_host
chain_port = remote_port
ssh_transport = transport
ForwardServer(('', local_port), SubHander).serve_forever()
def verbose(s):
if g_verbose:
print s
HELP = """\
Set up a forward tunnel across an SSH server, using paramiko. A local port
(given with -p) is forwarded across an SSH session to an address:port from
the SSH server. This is similar to the openssh -L option.
"""
def forward():
client = paramiko.SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.WarningPolicy())
try:
print 'connecting'
client.connect('*******', username='***', password='****!')
print 'connected'
except Exception, e:
print '*** Failed to connect to %s:%d: %r' % ('*****', 22, e)
sys.exit(1)
try:
forward_tunnel(3306, '127.0.0.1', 3306, client.get_transport())
except SystemExit:
print 'C-c: Port forwarding stopped.'
sys.exit(0)
I have two problems here:
1) I don't know how and when to call my forward function when django raises.
2) When I access django locally and run the script from the console I get the following exception:
exception happened during
processing of request from
('127.0.0.1', 41872) Traceback (most
recent call last): File
"/usr/lib/python2.6/SocketServer.py",
line 558, in process_request_thread
self.finish_request(request, client_address) File
"/usr/lib/python2.6/SocketServer.py",
line 320, in finish_request
self.RequestHandlerClass(request, client_address, self) File
"/usr/lib/python2.6/SocketServer.py",
line 615, in init
self.handle() File "/home/omer/Aptana Studio 3
Workspace/Website/src/ssh_tunnel/tunnel.py",
line 51, in handle
verbose('Tunnel closed from %r' % (self.request.getpeername(),)) File
"", line 1, in getpeername
File "/usr/lib/python2.6/socket.py",
line 165, in _dummy
raise error(EBADF, 'Bad file descriptor') error: [Errno 9] Bad file
descriptor
Was this a bad idea to begin with?
Should I do this manually every time?
I don't think it's a bad idea.
I don't think you need to do it manually.
The exception is a bug in paramiko's forward code sample. This has been addressed by jhalcrow in the pull request here:
https://github.com/paramiko/paramiko/pull/36
This post has some code to do it in a more event driven way, i.e if you wanted to call it via some web event hooks in your django code or the like:
Paramiko SSH Tunnel Shutdown Issue
humm, i didn't try this, but if you are on linux, could you run
ssh -L 3306:localhost:3306 remote.host.ip
through python system call when DEBUG is set?
also if you are on Windows, try putty with port forwarding