some one know, how can i use variable to execute on remote host with paramiko?
For example i try to put multiple files and execute/make executable it.
SRCFILE='/root/test1.py'
DSTDIR="/tmp/"
dstf= 'runremote.py'
stdin, stdout, stderr = ssh.exec_command("chmod +x" + '' + DSTDIR + dstf)
This lines are only for example not the Script.
My issue, i get nothing from stdout and the file are untouched.
if i run stdin, stdout, stderr = ssh.exec_command("chmod +x /tmp/runremote.py") get output on stdout and my file is changed.
Related
this is 3rd hour of me knowing the existence of something which is called paramiko.
my requirement: run a py script on windows that connects to remote linux server, executes a shell script there and take output of whatever shell script prints and return to python script and print it on python windows terminal.
I am able to connect through ssh. Opening channel and session both working.
Issue: SshObj.exec_command doesn't work for some commands or scripts.
However, I tried with normal "ls" command. same SshObj.exec_command call is working fine.
below is snippet:
>>> ssh = paramiko.SSHClient()
>>> ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
>>> ssh.connect(IP,22,usname,pswd)
>>> stdin, stdout, stderr = ssh.exec_command('/home/scripts/a.sh')
>>> stdout.channel.recv_ready()
False
>>> stdout.channel.recv_ready()
False
>>> stdin, stdout, stderr = ssh.exec_command('/home/scripts/a.sh')
>>> stdin, stdout, stderr = ssh.exec_command('ls')
>>> stdout.channel.recv_ready()
True
After logging in to a remote machine i want to "cd" to a particular directory and execute a command in the "exec_command" using the paramico module . But unable to do that. Am using ";" in between two commands but still unable to get the expected output.
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect("10.73.73.242", username="root", password="dangerous")
print 'running remote command'
stdin, stdout, stderr = ssh.exec_command("cd /usr/ABC/PGMS;pwd")
x = stdout.readlines()
print "VALUES",x
z=stderr.readlines()
print "err are",z
stdin, stdout, stderr = ssh.exec_command("ls")
x = stdout.readlines()
print "VALUES ARE AGAIN",x
ssh.close()
Here i want to change dir to a directory "/usr/ABC/PGMS" execute "pwd" but its not working. Am clueless about what am doing wrong.
The output is as below
running remote command
VALUES ['/root\n']
err are ['bash: line 0: cd: /usr/ABC/PGMS/: No such file or directory\n']
VALUES ARE AGAIN ['Desktop\n', 'Documents\n', 'autoinst.xml\n', 'bin\n', >'dup_rm_ordr.py\n', 'fullwrite.log\n', 'inst-sys\n', 'pexpect-2.3\n', >'pexpect-2.3.tar.gz\n', 'read_print_matrix.py\n', 'remove_dup.py\n', >'runlog\n']
Still "pwd" shows the /root dir and lists the files and directories in /root.
I am Using Python paramiko and
My website server has folder structure like this-
1]dir1
--dirP
--dirQ
2]dir2
--dirA
--file.sh
--dirB
3]dir3
where i want to access file.sh from dirA inside dir2 folder
I tried this-
import paramiko
client.connect('mysite.com', username='something', password='something')
stdin, stdout, stderr = client.exec_command('cd dir2')
stdin, stdout, stderr = client.exec_command('ls')
for line in stdout:
print('... ' + line.strip('\n'))
but i get output-
...dir1
...dir2
...dir3
expected output is-
...dirA
...dirB
and also suggest how can i execute file.sh too?
client.exec_command("cmd ...") is just like the command ssh user#host "cmd ..." so
client.exec_command('cd dir2')
client.exec_command('ls')
are just like
ssh user#host 'cd dir2' # this would not affect the following `ls'
ssh user#host 'ls'
. So you need to do like this:
client.exec_command('cd dir2; ls')
which is just like
ssh user#host 'cd dir2; ls'
Or if you use variables need to add + for example
client.exec_command('str(var1)+str(var2))
I am using paramiko to start a process in remote server.
With below code even if process is getting started or not ,it's printing 'not able to start'.
Not able to figure out issue here.
stdin, stdout, stderr = ssh.exec_command("{0}/minidiameterd -f {0}/BasicDiam1".format(minidiam_path))
stdin, stdout, stderr = ssh.exec_command("pgrep minidiameterd")
output = stdout.readlines()
if not output:
print "Not able to start minidiameterd"
Can you try
output = stdout.read.splitlines()
I'm trying to use Paramiko to connect to a remote host and execute a number of text file substitutions.
i, o, e = client.exec_command("perl -p -i -e 's/" + initial + "/"
+ replaced + "/g'" + conf);
Some of these commands need to be run as sudo, which results in:
sudo: sorry, you must have a tty to
run sudo
I can force pseudo-tty allocation with the -t switch and ssh.
Is it possible to do the same thing using paramiko?
Actually it's quite simple. Just:
stdin, stdout, stderr = client.exec_command(command, get_pty=True)
The following code works for me:
#!/usr/bin/env python
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('localhost',username='root',password='secret')
chan = ssh.get_transport().open_session()
chan.get_pty()
chan.exec_command('tty')
print(chan.recv(1024))
This was just assembled from looking at a few examples online... not sure if its the "right" way.
I think you want the invoke_shell method of the SSHClient object (I'd love to give a URL but the paramiko docs at lag.net are frame-heavy and just won't show me a specific URL for a given spot in the docs) -- it gives you a Channel, on which you can do exec_command and the like, but does that through a pseudo-terminal (complete with terminal type and numbers of rows and columns!-) which seems to be what you're asking for.
According to the sudo manpage:
The -S (stdin) option causes sudo to read the password from the standard input instead of the terminal device. The
password must be followed by a newline character.
You can write to the stdin because it is a file object with write():
import paramiko
client = paramiko.client.SSHClient()
client.set_missing_host_key_policy(paramiko.client.AutoAddPolicy())
client.connect(hostname='localhost', port=22, username='user', password='password')
stdin, stdout, stderr = client.exec_command('sudo -S aptitude update')
stdin.write('password\n')
stdin.flush()
# print the results
print stdout.read()
client.close()