Connect to putty and enter some commands

I want to connect to the spackler and I want to take a few steps:

  • Login to Putty
  • enter some commands to bring down the server.
  • Go to a specific path
  • Delete file from directory
  • Start the server again

I need to write code in windows. But my server is on Linux. How do I proceed? thanks in advance

+4
source share
7 answers

You need Paramiko , but it can be a little tricky for beginners.

For simple repetitive tasks, you can use my script - it is located on GitHub (https://github.com/tadeck/ssh-matic) and was created to learn Python. It is based on the Python SSH friendly friendly interface to the Paramiko parameter ( available here ).

Using the mentioned SSH module connecting to the server and executing the command is quite simple:

import ssh server = ssh.Connection(host='host', username='user', private_key='key_path') result = server.execute('your command') 

Basically, you do not need PuTTy, but the SSH module for Python. This module should work on both Windows and Linux. Using my script, you will only need to work with the command you want to call, as well as customize the code for your needs.

Good luck. Tell me if that helped.

+8
source

you can use code similar to:

command = "plink.exe -ssh username@ " + hostname + " -pw password -batch \"export DISPLAY='" + hostname + "/unix:0.0' ; "

which will open ssh for the desired hostname using username and password

shutdown: command += "sudo /sbin/halt\""

reboot: command += "sudo /sbin/reboot\""

add other commands using the same method as above,

run the command with:

pid = subprocess.Popen(command).pid

As Tadek noted, this will only work on a Windows machine trying to connect to a Linux machine.

+3
source
 from pywinauto.application import Application import time app = Application ().Start (cmd_line=u'putty -ssh user_name@10.70.15.175 ') putty = app.PuTTY putty.Wait ('ready') time.sleep (1) putty.TypeKeys ("password") putty.TypeKeys ("{ENTER}") time.sleep (1) putty.TypeKeys ("ls") putty.TypeKeys ("{ENTER}") 

I am using python 2.7. The code runs on Windows and connects to remote Linux. he works in my environment.

+3
source

You can do it:

 # Use plink to open a connection to the remote shell command = "plink.exe -ssh %s -batch" % credentials sp = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE) # Send commands to the shell as if they were read from a shell script sp.stdin.write("command1\n") sp.stdin.write("command2\n") sp.stdin.close() # read out the answers, if needed ans = sp.stdout.read() sp.wait() 

For credentials it is best to specify the name of the PuTTY connection profile, ready using the username and SSH key.

+2
source

You can use Fabric to perform these actions.

0
source

I think you need a login for the SSH server, so why not use http://www.lag.net/paramiko/ ? Therefore you do not need putty

0
source

To connect to an ubuntu machine using putty and create and run a python script check this link

0
source

Source: https://habr.com/ru/post/1379436/


All Articles