Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4995594
  • 博文数量: 921
  • 博客积分: 16037
  • 博客等级: 上将
  • 技术积分: 8469
  • 用 户 组: 普通用户
  • 注册时间: 2006-04-05 02:08
文章分类

全部博文(921)

文章存档

2020年(1)

2019年(3)

2018年(3)

2017年(6)

2016年(47)

2015年(72)

2014年(25)

2013年(72)

2012年(125)

2011年(182)

2010年(42)

2009年(14)

2008年(85)

2007年(89)

2006年(155)

分类: Python/Ruby

2013-08-16 23:09:16

What I've tried:

  1. invoke_shell() then channel.send su then send the password resulted in not being root
  2. invoke_shell() then channel.exec_command resulted in Channel Closed error
  3. _transport.open_session() then channel.exec_command resulted in not being in root
  4. invoke_shell() then write to the stdin and flush it resulted in not being in root


To my mind it would be much easier and secure to create a script which has sudoer rights.

For example, add this to sudoers:

myuser  ALL=NOPASSWD:/home/myuser/somescript.sh
 
Python Remote SSH with Paramiko

I am using Paramiko to do some remote ssh work and could not figure out how to change directories and execute a script with the SSHClient.execute_command() function. I finally figured out that .execute_command() is basically a single session, so doing a .execute_command('cd scripts') and then executing the script with another .execute_command() reverts back to your default directory. The alternatives are to send all the commands at once separated by a ; .execute_command('cd scripts; ./myscript.sh'), or to use the .interactive() shell support. Since I only needed to fire off this script I used the first solution.
 意思就是execute_command() 他是a single session,每次执行完后都要回到缺省目录。所以可以 .execute_command('cd  /var; pwd')

或者使用aa = ssh.invoke_shell()
aa.send('cd /var\n')

 

阅读(2142) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~