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.
Related
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))
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.
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 facing a problem that as I ssh to another machine, my paramiko ssh session does not see the same system PATH as when I manually ssh to the machine.
Here is my python code:
cmd = "echo $PATH"
try:
ssh.connect(ip, username=username, password=password)
except Exception as ex:
raise Exception("Failed to connect to %s with credentials username='%s' password='%s' %s" \
% (ip, username, password, ex.message) )
ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command(cmd)
output = ssh_stdout.read()
The output show /usr/bin:/bin
but when I manually ssh to the machine, there are several other paths on the system PATH.
Please help.
I don't think any bashrc or profile scripts are being sourced when you use exec_command(). Maybe try the following:
stdin, stdout, stderr = ssh.exec_command("bash -lc 'echo $PATH'")
my_path = stdout.read().rstrip()
If the problem is that you are trying to run a command that's normally in your PATH, but isn't when you use exec_command(), you're probably better off calling the command by its absolute path (run "which [command]" when you're logged into the other machine normally to find out what that is).
You better to load the bash_profile before you run your command. Otherwise you may get a 'command not found' exception.
For example,I write the command command = 'mysqldump -uu -pp -h1.1.1.1 -P999 table > table.sql' in the purpose of dumping a Mysql table
Then I have to load the bash_profile manually before that dumping command by typing . ~/.profile; .~/.bash_profile;.
Example
my_command = 'mysqldump -uu -pp -h1.1.1.1 -P999 table > table.sql;'
pre_command = """
. ~/.profile;
. ~/.bash_profile;
"""
command = pre_command + my_command
stdin, stdout, stderr = ssh.exec_command(command)
I am creating one user and want to set the ssh key for that user
My script is
import paramiko
ssh_conn = paramiko.SSHClient()
ssh_conn.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_conn.load_system_host_keys()
ssh_conn.connect('localhost', username='test', password='test')
cmd = 'ssh-keygen -t dsa'
stdin, stdout, stderr = ssh_conn.exec_command(cmd)
stdin.write('\n')
stdin.flush()
stdin.write('\n')
stdin.flush()
stdin.write('\n')
stdin.flush()
print "Output: ", stdout.read()
But its seems not working
When i run it as single statement copy past on python console then it works but when i run it as single python script it hang at last line print "Output: ", stdout.read().
Thx in advance for your help :)
Before attempting to read the stdout issue:
stdin.channel.shutdown_write()
See also this question for reference.