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.
Related
My situation is that I have set up a container in a remote server, and inside the container, there is a virtual environment. I'm using the python interpreter inside this virtual environment in this container, not the one on the host.
From my local machine, I can open up PyCharm, and use Tools->Deployment->Configuration to easily set up a remote connection. And For a specific project, I can set up the interpreter by clicking Files->Settings->Project Interpreter. However, it seems that I can only select the host Python interpreter(/usr/bin/python) on the remote server, not the one inside the virtual environment in the container. How could I set up using this interpreter?
I googled but can't find exact solution. I don't think I need to install Docker locally because my Docker is on the remote server side, right?
In similar way you are connecting to remote host - you would need to setup container with same capabilities e.g. set ssh server running on there. Then you should expose the port into public world or use nested ssh tunnel, which would be better alternative.
Another interesting approach (maybe recommended) is to forward Docker socket from the remote machine so, that you local Docker CLI uses this socket for sending commands to remote host. Theoretically, then you could add this container directly in PyCharm, when you set correct Docker host address there.
Further, virtual environments on other than local host systems are not supported natively by PyCharm. However, you could try to add path of python and see if it works e.g. venv/bin/python from project directory.
I am trying to connect to remote machine by python script. Both the machines can be linux/windows.
I have to connect to various remote machines and it's not feasible for me to install or write some code on the remote side. I know the ip, username and password of the machine and i tried various options but was unsuccessful.
How shall I proceed.
Maybe you can use SSH to connect to a remote server.
paramiko will be good idea, it can use to connect linux/windows.
http://pxnet2768.pixnet.net/blog/post/157228756-%E7%B3%BB%E7%B5%B1%E9%81%8B%E7%B6%AD%E5%B7%A5%E7%A8%8B%E5%B8%AB%E7%9A%84%E6%B3%95%E5%AF%B6%EF%BC%9Apython-paramiko
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.
I'm connected to a VM on a private network at address 'abc.def.com' using ssh, and on that VM there's an application that hosts a Python web app (IPython Notebook) that I can access by pointing my local browser to 'abc.def.com:7777'.
From that web app I can call shell commands by preceding them with '!', for example !ls -lt will list the files in the VM current working directory. But since I'm using my own laptop's browser, I think I should be able to run shell commands on my local files as well. How would I do that?
If that's not possible, what Python/shell command can I run from within the web app to automatically get my laptop's IP address to use things like scp? I know how to get my IP address, but I'd like to create a program that will automatically enable scp for whoever uses it.
You have ssh access so you could possibly write a python function that would let you transfer files via scp the secure copy command which uses ssh to communicate. If you exchange keys with the server you wouldn't have to put in a password so I see no problem from that standpoint. The issue is if you have an address for your local machine to be accessed from the server.
I work on various remotes from my laptop all day and from my laptop to the sever I could have this function:
def scp_to_server(address, local_file, remote_file):
subprocess.call(['scp',local_file,"myusername#{}:{}".format(address, remote_file)])
that would copy a file from my local machine to the remote provided the paths were correct, I have permissions to copy the files, and my local machine's id_rsa.pub key is in the ~/.ssh/authorized_keys file on the remote.
I have no way to initiate a secure copy from the remote to my local machine however because I don't have an address to access the local machine from that I can "see" on the remote.
If I open the terminal on my laptop and run hostname I see mylaptop.local and on the remote I see remoteserver#where.i.work.edu but the first is a local address I can see it from other machines on my LAN at home, (because I have configured that) but I can't see mylaptop.local from the remote. I know there is a way to configure that so I could find my laptop at home from anywhere, but I never had the need to do that (since I bring the laptop with me) so I can't help you there. I think there are a few more hurdels to go-over than you would like.
You could implement the function above on your local machine and transfer the files that way though.
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.