I am having a very specific problem. I use Putty for database management for my small business. We recently had an update and our normal command path to update our records is no longer in use.
We run Putty on all computers in store. Putty is used on a virtual machine with oracle. We have a main computer for the system in which the update occurred.
We normally input ~/Desktop/getdata.sh into putty using root user and it complies an updated list, creates a text file that we use. Unfortunately, the person who created this script no longer works with us.
I am trying to find a way to re execute this file again.
After the update, when we type in ~/Desktop/getdata.sh (after logging in as root) into Putty we get 'directory cannot be found.' I've searched everyday to find this file. However, I did find a getdata.py file and a getdata.bat files.
I can show both scripts if needed, I can update the question.
When I tried to run getdata.py I get
[root#RT_Store-01 /]# ~/Desktop/getdata.py
import: unable to open X server `'.
import: unable to open X server `'.
import: unable to open X server `'.
import: unable to open X server `'.
: command not foundta.py: line 5:
/root/Desktop/getdata.py: line 6: from: command not found
/root/Desktop/getdata.py: line 7: from: command not found
: command not foundta.py: line 8:
/root/Desktop/getdata.py: line 9: syntax error near unexpected token `('
/root/Desktop/getdata.py: line 9: `dir_path = os.path.dirname(os.path.realpath(_'file__))
Do I need to convert my files to .sh? How would I do that? Is this a bigger problem?
The script for getdata.py is
import os
import tempfile
import paramiko
import time
from pywinauto import Application
from subprocess import Popen, PIPE, STDOUT
dir_path = os.path.dirname(os.path.realpath(__file__))
class Connection(object):
"""Connects and logs into the specified hostname.
Arguments that are not given are guessed from the environment."""
def __init__(self,
host,
username=None,
private_key=None,
password=None,
port=22,
):
self._sftp_live = False
self._sftp = None
if not username:
username = os.environ['LOGNAME']
# Log to a temporary file.
templog = tempfile.mkstemp('.txt', 'ssh-')[1]
paramiko.util.log_to_file(templog)
# Begin the SSH transport.
self._transport = paramiko.Transport((host, port))
self._tranport_live = True
# Authenticate the transport.
if password:
# Using Password.
self._transport.connect(username=username, password=password)
else:
# Use Private Key.
if not private_key:
# Try to use default key.
if os.path.exists(os.path.expanduser('~/.ssh/id_rsa')):
private_key = '~/.ssh/id_rsa'
elif os.path.exists(os.path.expanduser('~/.ssh/id_dsa')):
private_key = '~/.ssh/id_dsa'
else:
raise TypeError(
"You have not specified a password or key.")
private_key_file = os.path.expanduser(private_key)
rsa_key = paramiko.RSAKey.from_private_key_file(private_key_file)
self._transport.connect(username=username, pkey=rsa_key)
def _sftp_connect(self):
"""Establish the SFTP connection."""
if not self._sftp_live:
self._sftp = paramiko.SFTPClient.from_transport(self._transport)
self._sftp_live = True
def get(self, remotepath, localpath=None):
"""Copies a file between the remote host and the local host."""
if not localpath:
localpath = os.path.split(remotepath)[1]
self._sftp_connect()
self._sftp.get(remotepath, localpath)
def put(self, localpath, remotepath=None):
"""Copies a file between the local host and the remote host."""
if not remotepath:
remotepath = os.path.split(localpath)[1]
self._sftp_connect()
self._sftp.put(localpath, remotepath)
def execute(self, command):
"""Execute the given commands on a remote machine."""
channel = self._transport.open_session()
channel.exec_command(command)
output = channel.makefile('rb', -1).readlines()
if output:
return output
else:
return channel.makefile_stderr('rb', -1).readlines()
def update(self):
"""Execute the given commands on a remote machine."""
channel = self._transport.invoke_shell(term='xterm')
channel.exec_command('~/Desktop/update.sh')
output = channel.makefile('rb', -1).readlines()
if output:
return output
else:
return channel.makefile_stderr('rb', -1).readlines()
def close(self):
"""Closes the connection and cleans up."""
# Close SFTP Connection.
if self._sftp_live:
self._sftp.close()
self._sftp_live = False
# Close the SSH Transport.
if self._tranport_live:
self._transport.close()
self._tranport_live = False
def __del__(self):
"""Attempt to clean up if not explicitly closed."""
self.close()
def getData():
"""Create, get, and put delim file when called directly."""
app = Application().start(r"c:\putty.exe trak#10.1.10.70 -pw trak")
app.window_(
title_re=".*trak.*").TypeKeys("/home/trak/Desktop/getdata.sh && exit{ENTER}")
app.window_(title_re=".*trak.*").WaitNot("exists", timeout=120)
trakfile = dir_path + '/storage/trakdelim.txt'
shell = Connection('10.1.10.70', "trak", password="trak")
shell.get('/trak/data/trakdelim.txt', trakfile)
shell.close()
if __name__ == "__main__":
getData()
I appreciate anyone who can help and I can clarify when needed!
A bit of googling showed me what this is:
Someone copied this code and wrote a simple script with it that takes a file (/trak/data/trakdelim.txt) on a remote computer with ip address 10.1.10.70 username trak and password trak and copies it to the storage/trakdelim.txt file. if this is not working for you now then take a tool that allows you to do this manually with such as winSCP and use that instead.
Good luck.
You need to execute it as a python script.
python ~/Desktop/getdata.py
Related
Because this question seems to aim somewhere else I am going to point my problem here:
In my python script I am using multiple requests to a remote server using ssh:
def ssh(command):
command = 'ssh SERVER "command"'
output = subprocess.check_output(
command,
stderr=subprocess.STDOUT,
shell=True,
universal_newlines=True
)
return output
here I will get the content of file1 as output.
I have now multiple methods which use this function:
def show_one():
ssh('cat file1')
def show_two():
ssh('cat file2')
def run():
one = show_one()
print(one)
two = show_two()
print(two)
Executing run() will open and close the ssh connection for each show_* method which makes it pretty slow.
Solutions:
I can put:
Host SERVER
ControlMaster auto
ControlPersist yes
ControlPath ~/.ssh/socket-%r#%h:%p
into my .ssh/config but I would like to solve this within python.
There is the ssh flag -T to keep a connection open, and in the before mentioned Question one answer was to use this with Popen() and p.communicate() but it is not possible to get the output between the communicates because it throws an error ValueError: Cannot send input after starting communication
I could somehow change my functions to execute a single ssh command like echo "--show1--"; cat file1; echo "--show2--"; cat file2 but this looks hacky to me and I hope there is a better method to just keep the ssh connection open and use it like normal.
What I would like to have: For example a pythonic/bashic to do the same as I can configure in the .ssh/config (see 1.) to declare a specific socket for the connection and explicitly open, use, close it
Try to create ssh object from class and pass it to the functions:
import paramiko
from pythonping import ping
from scp import SCPClient
class SSH():
def __init__(self, ip='192.168.1.1', username='user', password='pass',connect=True,Timeout=10):
self.ip = ip
self.username = username
self.password = password
self.Timeout=Timeout
self.ssh = paramiko.SSHClient()
self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
if connect:
self.OpenConnection()
self.scp = SCPClient(self.ssh.get_transport())
def OpenConnection(self):
try:
skip_ping = False
ping_res=False
log.info('Sending ping to host (timeout=3,count=3) :'+self.ip)
try:
PingRes = ping(target=self.ip,timeout=3,count=3, verbose=True)
log.info('Ping to host result :' + str(PingRes.success()))
ping_res=PingRes.success()
except:
skip_ping=True
if ping_res or skip_ping:
log.info('Starting to open connection....')
self.ssh.connect(hostname=self.ip, username=self.username, password=self.password, timeout=self.Timeout, auth_timeout=self.Timeout,banner_timeout=self.Timeout)
self.scp = SCPClient(self.ssh.get_transport())
log.info('Connection open')
return True
else:
log.error('ssh OpenConnection failed: No Ping to host')
return False
myssh = SSH(ip='192.168.1.1',password='mypass',username='myusername')
the ping result is wrapped in try catch because sometimes my machine return an error you can remove it and just verify a ping to the host.
The self.scp is for file transfer.
I have a directory containing some files and sub-directories on my local machine that is generated by a daily Python script running. And then I want to copy all those generated files in a directory to the server and then run some series of commands on it by ssh using python package paramiko.
I want to add little code to securely copy that entire directory and files and sub-directories inside it to to my server over SSH/SCP using the paramiko package of python. I can send the individual files using the same but unable to send the entire directory and its content using paramiko. It gives me IOError saying it's a directory.
IOError: [Errno 21] Is a directory: 'temp'
Here is the my code:
from paramiko import SSHClient, AutoAddPolicy
from scp import SCPClient
class SSh(object):
def __init__(self, address, username, password, port=22):
print "Connecting to server."
self._address = address
self._username = username
self._password = password
self._port = port
self.sshObj = None
self.connect()
self.scp = SCPClient(self.sshObj.get_transport())
def sendCommand(self, command):
if(self.sshObj):
stdin, stdout, stderr = self.sshObj.exec_command(command)
while not stdout.channel.exit_status_ready():
# Print data when available
if stdout.channel.recv_ready():
alldata = stdout.channel.recv(1024)
prevdata = b"1"
while prevdata:
prevdata = stdout.channel.recv(1024)
alldata += prevdata
print str(alldata)
else:
print "Connection not opened."
def connect(self):
try:
ssh = SSHClient()
ssh.set_missing_host_key_policy(AutoAddPolicy())
ssh.connect(self._address, port=self._port, username=self._username, password=self._password, timeout=20, look_for_keys=False)
print 'Connected to {} over SSh'.format(self._address)
return True
except Exception as e:
ssh = None
print "Unable to connect to {} over ssh: {}".format(self._address, e)
return False
finally:
self.sshObj = ssh
if __name__ == "__main__":
# Parse and check the arguments
ssh = SSh('10.24.143.190', 'username', 'password')
ssh.scp.put("Valigrind_BB.py") # This works perfectly fine
ssh.scp.put("temp") # IOError over here Is a directory
ssh.sendCommand('ls')
Thank you in advance.
Looking at the source code for SCP, it appears you can pass the parameter "recursive" as a bool to the put() method to specify transferring the contents of a directory recursively. Here is a link to the source code I am talking about. Try changing last part of your code to the following:
if __name__ == "__main__":
# Parse and check the arguments
ssh = SSh('10.24.143.190', 'username', 'password')
ssh.scp.put("Valigrind_BB.py") # This works perfectly fine
ssh.scp.put("temp", recursive=True) # IOError over here Is a directory
ssh.sendCommand('ls')
Also, if you just want to transfer files, you can try rsync as an alternative. The modification to your code above should work though. I hope this helps.
Fairly new to python. I'm able to ssh to a switch and get the data needed using the below script but I need to pull this information from 100+ switches. What do I need to add to this script to achieve that? Thanks
from paramiko import client
class ssh:
client = None
def __init__(self, address, username, password):
# Let the user know we're connecting to the server
print("Connecting to switch.")
# Create a new SSH client
self.client = client.SSHClient()
# The following line is required if you want the script to be able to access a server that's not yet in the known_hosts file
self.client.set_missing_host_key_policy(client.AutoAddPolicy())
# Make the connection
self.client.connect(address, username=username, password=password, look_for_keys=False)
def sendCommand(self, command):
if(self.client):
stdin, stdout, stderr = self.client.exec_command(command)
while not stdout.channel.exit_status_ready():
# Print data when available
if stdout.channel.recv_ready():
alldata = stdout.channel.recv(1024)
prevdata = b"1"
while prevdata:
prevdata = stdout.channel.recv(1024)
alldata += prevdata
print(str(alldata, "utf8"))
else:
print("Connection not opened.")
connection = ssh("x.x.x.x", "user", "pwd")
connection.sendCommand("show int status | e not")
swiches mean instance? so you want to run command on muti-machine? your can try fabric.
#roles("instance-group")
#task
def upload():
"""上传代码到测试服务器"""
local("tar -zc -f /tmp/btbu-spider.tar.gz ../BTBU-Spider")
put("/tmp/btbu-spider.tar.gz", "/tmp/")
local('rm /tmp/btbu-spider.tar.gz')
run("tar -zx -f /tmp/btbu-spider.tar.gz -C ~/test/")
then you can define "instance-group" ssh ip, you can call this function from local. but actually all this command are run at remote instance(not local function).
Add the information about all the switches into a configuration file, for example like:
address1,username1,password1
address2,username2,password2
Each switch can be on a new line and arguments can be separated by just using a comma for ease of use. Then, open the file and read lines one by one and parse them:
with open('switches.conf', 'r') as f:
for line in f: # Read line by line
params = line.split(',') # Split the three arguments by comma
address = params[0]
username = params[1]
password = params[2]
connection = ssh(address, username, password)
connection.sendCommand("show int status | e not")
with open(address, 'w+') as wf: # If you have to write any information about the switches, suggesting this method
wf.write(..information to write..)
That must be done in the cycle. You can just call two functions in that cycle, the function that initializes the SSH connection and the function that gets the information, while this could be your main function. Of course the example for configuration is just a guideline, it has a lot of flaws, especially security ones, so you can make it however you want. Anyway, the idea is that you just need a cycle to do that and by having a configuration file to read from makes it a lot easier :) And you can save the information to different text files that are named by the addresses of the instances for example, as #Santosh Kumar suggested in the comments.
EDIT: Edited my answer and added an example for the connection and sending the command, as I hadn't noticed it was a class.
You can use parallel-ssh for running ssh cmds on multiple machines. Get it by running `pip install parallel-ssh
from pssh.clients import ParallelSSHClient
hosts = ['switch1', 'switch2']
client = ParallelSSHClient(hosts, user='my_user', password='my_pass')
output = client.run_command('show int status | e not')
for host, host_output in output.items():
print host, "".join(host_output.stdout)
I'm not a programmer, but would like to use Python for automation of some Administrative purposes.
The first application after "Hello world" I tried to create is interactive ssh client.
I've read some documentation and articles and decided it would be the easiest way to use paramiko module, but unfortunately I'm facing a problem:
My applications asks you to enter some neccessary information such as server ip, username, password. After this it establishes connection with defined server and provide you with cli on your screen. For emulating the process of entering command I use while loop.
Unfortunately my application works well only with the first command you enter. While trying to type the second command an error appears:
Traceback (most recent call last):
File "C:\Python27\Tests\ssh_client.py", line 53, in <module>
client.execute_command(command)
File "C:\Python27\Tests\ssh_client.py", line 26, in execute_command
stdin,stdout,stderr = self.connection.exec_command(command)
File "C:\Python27\lib\site-packages\paramiko\client.py", line 343, in exec_command
chan.exec_command(command)
AttributeError: 'NoneType' object has no attribute 'exec_command'
Code of the programm (Windows 7):
import paramiko
SERVER = raw_input('Please enter an ip address of remote host: ')
USER = raw_input('Please enter your username: ')
PASSWORD = raw_input('Please enter your password: ')
class MYSSHClient():
def __init__(self, server=SERVER, username=USER, password=PASSWORD):
self.server = server
self.username = username
self.password = password
self.connection = None
self.result = ''
self.is_error = False
def do_connect(self):
self.connection = paramiko.SSHClient()
self.connection.set_missing_host_key_policy(paramiko.AutoAddPolicy())
self.connection.connect(self.server, username=self.username, password=self.password)
def execute_command(self, command):
if command:
print command
stdin,stdout,stderr = self.connection.exec_command(command)
stdin.close()
error = str(stderr.read())
if error:
self.is_error = True
self.result = error
print 'error'
else:
self.is_error = False
self.result = str(stdout.read())
print 'no error'
print self.result
else:
print "no command was entered"
def do_close(self):
self.connection.close()
if __name__ == '__main__':
client = MYSSHClient()
client.do_connect()
while 1:
command = raw_input('cli: ')
if command == 'q': break
client.execute_command(command)
client.do_close()
I tried to delete while loop and just call commands one by one right in the code, but have the same problem (when typing the second command see the same error).
It looks like I don't understand fully how paramiko module works. I tried to find information on web but unfortunately didn't find any solution.
I'd be very appreciated if somebody could tell me what I do wrong or give me a link on the similar issue where I can find a solution.
Thanks in advance for any help.
Please use for pxssh module this is very useful for your application if work for windows
Python: How can remote from my local pc to remoteA to remoteb to remote c using Paramiko
this example very helpful for you Python - Pxssh - Getting an password refused error when trying to login to a remote server
i think u check for your server settings in remote host machine
Not a real answer to your problem but more of a suggestion.
I would recommend you to have a look at fabric, which does exactly what you want: Automate tasks on local or remote hosts. Might be a bit easier since you don't have to implement the logic for the connection and execution of commands.
Fabric documentation: http://docs.fabfile.org/en/1.6/
Unfortunately I didn't find the way to resolve my issue using paramiko module, but I've found such a module as Exscript.
The simple code is below:
from Exscript.util.interact import read_login
from Exscript.protocols import SSH2
account = read_login()
conn = SSH2()
conn.connect('192.168.1.1')
conn.login(account)
while True:
command = raw_input('cli: ')
if command == 'q': break
conn.execute(command)
print conn.response
conn.send('quit\r')
conn.close()
I would like to write a script that performs the following Linux commands sequence:
rsh rr01 ( here the system will ask for password )
ssh rr02 ( here the system will ask for password again)
activate my app on some file
I was naively (and stupidly) tried to accomplish that with a simple os.system
bear in mind I using python 2.3 and would like to avoid adding modules (if possible).
Thanks in advance!
[EDIT] Check out also another example using paramiko on http://code.google.com/p/lexel-intern0t/source/browse/trunk/Python/ssh_client.py.
The script found on http://media.commandline.org.uk//code/ssh.txt may help you. It includes os module (to find the keys) and tempfile module for logging purposes, both found in python 2.3 global module index ( http://docs.python.org/release/2.3/modindex.html ) and paramiko ( http://www.lag.net/paramiko/docs/ ) for the SSH2 protocol implementation, which is supposed to work on python 2.3 and above. It's a simple piece of code:
"""Friendly Python SSH2 interface."""
import os
import tempfile
import paramiko
class Connection(object):
"""Connects and logs into the specified hostname.
Arguments that are not given are guessed from the environment."""
def __init__(self,
host,
username = None,
private_key = None,
password = None,
port = 22,
):
self._sftp_live = False
self._sftp = None
if not username:
username = os.environ['LOGNAME']
# Log to a temporary file.
templog = tempfile.mkstemp('.txt', 'ssh-')[1]
paramiko.util.log_to_file(templog)
# Begin the SSH transport.
self._transport = paramiko.Transport((host, port))
self._tranport_live = True
# Authenticate the transport.
if password:
# Using Password.
self._transport.connect(username = username, password = password)
else:
# Use Private Key.
if not private_key:
# Try to use default key.
if os.path.exists(os.path.expanduser('~/.ssh/id_rsa')):
private_key = '~/.ssh/id_rsa'
elif os.path.exists(os.path.expanduser('~/.ssh/id_dsa')):
private_key = '~/.ssh/id_dsa'
else:
raise TypeError, "You have not specified a password or key."
private_key_file = os.path.expanduser(private_key)
rsa_key = paramiko.RSAKey.from_private_key_file(private_key_file)
self._transport.connect(username = username, pkey = rsa_key)
def _sftp_connect(self):
"""Establish the SFTP connection."""
if not self._sftp_live:
self._sftp = paramiko.SFTPClient.from_transport(self._transport)
self._sftp_live = True
def get(self, remotepath, localpath = None):
"""Copies a file between the remote host and the local host."""
if not localpath:
localpath = os.path.split(remotepath)[1]
self._sftp_connect()
self._sftp.get(remotepath, localpath)
def put(self, localpath, remotepath = None):
"""Copies a file between the local host and the remote host."""
if not remotepath:
remotepath = os.path.split(localpath)[1]
self._sftp_connect()
self._sftp.put(localpath, remotepath)
def execute(self, command):
"""Execute the given commands on a remote machine."""
channel = self._transport.open_session()
channel.exec_command(command)
output = channel.makefile('rb', -1).readlines()
if output:
return output
else:
return channel.makefile_stderr('rb', -1).readlines()
def close(self):
"""Closes the connection and cleans up."""
# Close SFTP Connection.
if self._sftp_live:
self._sftp.close()
self._sftp_live = False
# Close the SSH Transport.
if self._tranport_live:
self._transport.close()
self._tranport_live = False
def __del__(self):
"""Attempt to clean up if not explicitly closed."""
self.close()
def main():
"""Little test when called directly."""
# Set these to your own details.
myssh = Connection('example.com')
myssh.put('ssh.py')
myssh.close()
# start the ball rolling.
if __name__ == "__main__":
main()