JumpSSh in Python get_cmd_output - python

I am trying to connect to a remote server from a jump server. It connected to the remote server perfectly, but when I try to run a python script on the remote server, it says no directory found. Please help
gateway_session = SSHSession('host',
'unman', password='password').open()
# from jump server, establish connection with a remote server
remote_session = gateway_session.get_remote_session('host',
'username',password='password')
print(gateway_session.get_cmd_output('python /Folder/test.py'))

Try first to check where you are:
print(gateway_session.get_cmd_output('pwd;ls -alrth; ls -alrth /'))
That way, you know if there is indeed a Folder at /
The OP adds in the comments:
all I needed was to was separate the commands with a semi colon and run them using the same get output command.
The OP adds:
I just sshpass to a jump server from their, I sshpass to the remote on the same command in a shell script.

Related

How to create directory in remote system using python script

I want to create a directory in my remote system using python script or by socket programming. I have remote system's Username, password and IP address. I am able to do this in my local machine but not in remote. Please help!
Download Putty then connect to remote system) and in terminal write mkdir foldername
To create a directory on a remote machine, you will have to first connect to it.Telnet and SSH and SSH is used to connect to remote machines. Obviously TELNET or SSH service should be running on the remote machine, otherwise you won't be able to connect.Since in case of Telnet,data is transfered in plain text, it's better to use SSH protocol.
Once connected to the remote machine using SSH, you will be able to execute commands on the remote machine.
Now since you want to do everything in Python, you will have to write a complete SSH client in Python. Which is greate for learning, because you will learn about socket programming and cryptography.
If you are in a hurry, you can use a good SSH library.
If you are getting network connection error, please check whether SSH is installed in the remote machine or not. If yes, then check firewall settings.

Script executed on remote server using Python Paramiko cannot read/access local files

I'm trying to run a local Python script from my laptop (which works fine) on remote server (VPS).
The script can't read local files from my laptop from VPS
Output:
My script on pycharm.
import sys
import time
import paramiko
# Connect to remote host
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect('myip', port=22, username='root', password='mypassword')
# Setup sftp connection and transmit this script
sftp = client.open_sftp()
sftp.put(r'/myscript.py', '/myscript.py')
sftp.close()
# till now everything's good. I check my VPS files i find my script uploaded there
# Run the transmitted script remotely without args and show its output.
# SSHClient.exec_command() returns the tuple (stdin,stdout,stderr)
stdout = client.exec_command('python3 /myscript.py')[1]
for line in stdout:
# Process each line in the remote output
print(line)
client.close()
sys.exit(0)
When I run script from VPS I got this issue
I can't run the script directly from VPS to check the issue because I use local files, check the screenshot:
When I remove local paths and run the script (both from pycharm and VPS), it works fine.
You cannot magically access local files from script run on a server.
I can't run the script directly from VPS to check the issue because i use local files, check img.
There's no difference between running the script in remote shell using your favourite SSH terminal client (I assume that's what you mean by "run the script directly from VPS") and running the script in remote shell using Paramiko. It still runs in the remote shell.
There's no easy way to make the client files accessible from the server. That would be a security nightmare.
Either your script has to upload the files to the server.
Or you need to run a (SFTP/FTP/whatever) server on your local machine to make your local file accessible to the world.
For an example how to run an SFTP server, see my guide:
Installing SFTP/SSH server on Windows using OpenSSH

Connect to localhost mongodb from python script started by torify

When I'm trying to connect to the mongodb from my python script I see this warning many times and error finally:
[Dec 13 11:58:56] WARNING torsocks[8133]: [connect] Connection to a local address are denied since it might be a TCP DNS query to a local DNS server. Rejecting it for safety reasons. (in tsocks_connect() at connect.c:177)
pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 1] Operation not permitted
I use torify command for starting my command - torify python myscript.py. Without torify it works.
What I'm doing wrong? The same situation is on another machines to.
I found a solution for this case. I have to create a Hidden Service first. To do that I have to add this lines to my tor config file (/etc/tor/torrc):
HiddenServiceDir /tmp/tormongo
HiddenServicePort 27017 127.0.0.1:27017
After that I need to restart my tor service. If everything was done right the folder has to appear in /tmp. In this folder will be the new file hostname with the string like sgwqrpepus3lwcke.onion. This is the host what I can use instead of localhost in mongodb connection settings.
Or I can add a new ENV variable for this setting and set it every script start by using command like that:
export DB_HOST=$(cat /tmp/tormongo/hostname) && torify python /var/www/myproject/myscript.py

Error when connecting to SFTP server using Python Pysftp

I am executing a Python script on my Linux server that uses pysftp to connect to another server in order to read files that are sitting in a directory of that remote server. When I run the script, it fails out while connecting to the remote server and creates a text file with the title: 'This service allows sftp connections only.'
This file is created inside my project directory. Below is the part of my code that is failing:
def sftp_get_file(sftp_host, sftp_username):
with pysftp.Connection(sftp_host, sftp_username) as sftp:
# transfer file from remote to local
sftp.get(remote_file, local_file)
Code is very simple and works when I've tested it using my local server as the remote server. When I tested it in the new environment by actually depending on SFTP, then it failed. Any suggestions? Is pysftp using SSH at some point when it should be using only SFTP?
Turns out the problem was due to me performing sftp.execute('ls') a couple lines down in the script. The server I was remoting onto only supported sftp commands and that command was forbidden.

Python execute remote application using server resource

I am trying to ran a matlab executable application from Python on a remote server.
I used following code:
os.system("\\Server-01\\D$\\matlab_t.exe 7.25 16") # 7.25 and 16 are input arguments of matlab_t.exe
The above code is running on my local machine. I noticed that it is using resources (CPU and memory) of my local machine, while I am trying to use resources on the remote server.
May I know how I can execute it using server resource?
Thanks.
That command will run on your computer, the path may be pointing to a remote server, but no one has told the remote server that it should execute code, only that they need to serve the matlab_t.exe file.
You have to use a mechanism to access the remote server. Normally ssh is used for this purpose, but the ssh daemon has to be running on the remote server and also you need to have access (ask you admin about that).
Then you can use python like this:
import paramiko
ssh = paramiko.SSHClient()
ssh.connect(server, username=username, password=password)
ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command(cmd_to_execute_on_remote_server)
In python, the os.system command only executes the command on the local machine. What you want is a local command that will get the server to execute it by itself.
If the server is Windows based then you can use PsExec to do this, if the server is Linux based then using ssh with a python library (like the other answer demonstrates) would probably be the way to go.
Using PsExec, your command in os.system would be something like:
psexec.exe \\Server-01 -u <username> -p <password> D:\matlab_t.exe 7.25 16
If you server needed no authentication, you could remove the username and password flags.

Categories

Resources