Logging into a server and parsing files on that server with Python - python

I would like to be able to use a Python script that I wrote to search files to login to an Ubuntu server that's password protected (which I have credentials ), and search files on that server.. Is there a straight forward way to accomplish this?

To login and run remote terminal commands through python, you should use either paramiko or pexpect. Pexpect is not touched very much by noah these days... I'm starting to wonder whether he is abandoning it.
The other way is to sftp the files from the remote server to your local machine... paramiko is useful for that as well.

Related

SSH to Remote server using Python in-built modules

I want to ssh to remote servers and execute a script which is already on the remote servers.
Is it possible by python script using in-built modules? Unfortunately I can't use 3rd party modules due organization restrictions.
I believe its easier using SSH key based authentication but can't do that either. SSH login is by providing username and password.
Is there a way?
Thanks in advance!
Probably look into Python exec() and use sshpass for inline ssh login with username and password and the command you want to execute. Will require some experimentation on your end.

Is there a way run a python script on another server?

I have a Djnago web application on IIS 6 on one server. Is there a way that from this website I call another python script that is on another server in such a way that that script just run itself there?
Calling or Runnig that script in the usual way as internet says, is not working.
I always get the error of os.getcwd() and it also doesn't allow to change that directory.
I just want to run that python script there on that server from this server.
Can anyone help?
Normally, I would recommend using a framework like fabric or winrm if you want to run a python script on another server. Those frameworks use ssh or windows remoting functionality, respectively, to allow a python program to execute other commands (including python scripts) on other systems. If the target machine is a windows machine, be forewarned that you can run into all sorts of UAC issues doing normal operations.

PYTHON: cat-ing a remote file via SSH, WHILE SSH tunnel is open

I must say that I haven't coded seriously since university, latest in '92...
Since then I've just done some simple VB, so no real coding :)
I am trying to get some python coding done so that I am sending a remote command over SSH and I retrieve the output. Basically I am catting a text file and I am retrieving the contents of the file.
I have seen various methods to do this via SSH, using an ip address, port, key, etc, while opening the tunnel.
Yet, in my case, I have to manipulate the tunnel via some special server control, with web access, invoking Ajax commands, etc.
Relevant here is to just say that I have the python code to open and close the tunnels. What I need is to insert a simple snippet of code to execute a cat and to retrieve the output.
Can someone enlighten me on how that can be done? I don't need elevated access for the command, of course.
Any guidance would be much appreciated.
I am using python 3.6.4.
TIA

How to connect unix server with credentials and execute commands from python script

i am new to python scripting can someone please assist belo
1.read server, username and password
2.connect unix server
3.change directory and execute command
need some examples for above.
You can use the excellent paramiko package for this. It is an SSH (and SFTP) client library, which will let you easily connect to a Unix server running an ssh service, run commands, transfer files, etc.
You can also use fabric. Fabric is a Python (2.5-2.7) library and command-line tool for streamlining the use of SSH for application deployment or systems administration tasks.

How do I access a remote filesystem using Python on Windows?

I'm writing a Python script to access all computers on the network, log in to them and read some log files. I don't want to use something as low-level as socket, but I can if I must. I realize that my problem is similar to this question, but not the same.
Are there any modules for accessing external Windows machines?
Has anyone done anything like this before?
I'm specifically looking to log into Windows 7 machines, not unix.
Let's also assume that each computer I want to log into has Remote Desktop installed and enabled. I'm also not worried about network security or encryption because these files are not confidential. Windows machines don't have SSH installed on the by default do they?
There has to be something on the other side for you to talk to. This limits you to either setting up a "server" on each machine, installing a real server (i.e. sshd), building a "server" yourself and installing it, or using a built in and active feature of the OS.
Based upon this, what kind of system do you want to set up on these machines? What does it need to do? Just read the contents of a prespecified file list? Will that list change?
One solution is to turn on telnet, and use paramiko or twisted to
talk across it. This isn't very secure of course
Next up, set up a samba share, and access the folder remotely. This
is also insecure, though less so than telnet
You could find a ssh daemon port and run that, if you are so inclined
Psexec from sysinternals might work
Use twisted to build a server app with the features you need
Use ncat to listen on a port and spawn a cmd prompt
Be aware that most of the solutions for accessing windows remotely are... poor. The best solution is probably to roll your own, but that is hard work and you will probably make mistakes.
Also, Windows 7 is not exactly multi-user friendly. Individual processes can run as separate users, but the OS does not support having multiple users logged in at the same time. Someone is going to be the "user" and everyone else is just a process with a different credential set.
This is more an artificial limitation on M$'s part than anything technical. To see this in action, try to log in with RDP while a user is logged in locally. Fun times.
Per your edit, the easiest thing to do is just set up a samba share on the box.
After this share is set up:
with open(r'\\myCompNameOrIP\C\windows\logs\logfile.txt','rb') as logfile:
loglines = logfile.readlines()
Or you can use the gencat sample found here. Just give it r'\\myCompNameOrIP\C\windows\logs\*.txt' as the search path and watch the magic.
From Ubuntu I use samba:
In Bash:
gvfs-mount smb://them/folder
Here I give name, domain and password
Then in python:
folder = '/home/me/.gvfs/folder on them'
using the os module I read folders and files inside.
I am working in a small business environment.
Why not have each of the computers send the log file to the central computer?

Categories