我想编写一个程序(在Windows 7上的Python 3.x中),它通过ssh在远程shell上执行多个命令.在查看了paramikos的exec_command()函数之后,我意识到它不适合我的用例(因为在执行命令后通道被关闭),因为命令依赖于环境变量(由先前的命令设置)而不能连接到一个exec_command()调用,因为它们将在程序中的不同时间执行.
因此,我想在同一个通道中执行命令.我研究的下一个选项是使用paramikos的invoke_shell()函数实现交互式shell:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host, username=user, password=psw, port=22)
channel = ssh.invoke_shell()
out = channel.recv(9999)
channel.send('cd mivne_final\n')
channel.send('ls\n')
while not channel.recv_ready():
time.sleep(3)
out = channel.recv(9999)
print(out.decode("ascii"))
channel.send('cd ..\n')
channel.send('cd or_fail\n')
channel.send('ls\n')
while not channel.recv_ready():
time.sleep(3)
out = channel.recv(9999)
print(out.decode("ascii"))
channel.send('cd ..\n')
channel.send('cd simulator\n')
channel.send('ls\n')
while not channel.recv_ready():
time.sleep(3)
out = channel.recv(9999)
print(out.decode("ascii"))
ssh.close()
Run Code Online (Sandbox Code Playgroud)
这段代码存在一些问题:
我对这种"非决定论"感到困惑,非常感谢你的帮助.
我正在尝试使用python-paramiko在远程计算机上执行sudo命令,当我执行命令时,我用3个流绑定它,我使用输入流来传递密码,但它不起作用,这是追溯结果:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/paramiko/file.py", line 314, in write
self._write_all(data)
File "/usr/local/lib/python2.7/dist-packages/paramiko/file.py", line 439, in _write_all
count = self._write(data)
File "/usr/local/lib/python2.7/dist-packages/paramiko/channel.py", line 1263,in _write
self.channel.sendall(data)
File "/usr/local/lib/python2.7/dist-packages/paramiko/channel.py", line 796, in sendall
raise socket.error('Socket is closed')
error: Socket is closed
Run Code Online (Sandbox Code Playgroud)
这是我的python代码:
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('192.168.0.104', username='cdc',password='123456')
stdin, stdout, stderr = ssh.exec_command("sudo dmesg")
stdin.write("123456\n")
stdin.flush()
print stdout.readlines()
ssh.close()
Run Code Online (Sandbox Code Playgroud)
有帮助吗?提前致谢
我使用paramiko
f.ex sudo apt-get update的sudo 命令遇到了一些问题
这是我的代码:
try:
import paramiko
except:
try:
import paramiko
except:
print "There was an error with the paramiko module"
cmd = "sudo apt-get update"
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
ssh.connect("ip",username="lexel",password="password")
print "succesfully conected"
except:
print "There was an Error conecting"
stdin, stdout, stderr = ssh.exec_command(cmd)
stdin.write('password\n')
stdin.flush()
print stderr.readlines()
print stdout.readlines()
Run Code Online (Sandbox Code Playgroud)
这是一个快速代码.我知道我需要添加sys.exit(1)以及所有这些,但这仅仅是为了演示
我用这个作为参考: Jessenoller.com
问题:
我可以做这样的事情:
self.sftp.put(sourceFilePath, final_destination, use_sudo=True)
我可以创建文件夹,但不能创建文件?我是否需要明确调用 sudo 或在 paramiko 中设置一些东西?我应该将文件复制到允许的空间并进行 chowning 吗?有没有办法让 paramikko sudoer 不使用钥匙或不必乱搞ssh.exec_command("sudo mv")?我错过了什么?
编码:
class Ssh(object):
def __init__(self):
super(Ssh, self).__init__()
def setup(self):
'''Setup connection'''
try:
# DEBUG
paramiko.common.logging.basicConfig(level=paramiko.common.DEBUG)
#set username & password
username = 'sgdevbox'
password = MainFrame.ssh_pass
host = '192.168.0.170'
port = 22
self.transport = paramiko.Transport((host, port))
self.transport.connect(username = username, password = password)
self.sftp = paramiko.SFTPClient.from_transport(self.transport)
print(self.sftp.sock)
except Exception, e:
print(traceback.format_exc())
def putFiles(self, sources, listingSku):
'''
Upload images to server along with all currentItemInfo, …Run Code Online (Sandbox Code Playgroud) 我使用的朋友服务器只允许一个用户从 SSH 登录,所以通常我只是以该用户身份登录,然后su -l myuser更改帐户。我想使用 Python 自动化一些无聊的东西,但我遇到了问题。显然,我首先尝试的 Paramiko 模块为每个命令调用一个 shell,所以这是不可能的。后来我尝试使用invoke_shell()来克服这个问题,但它仍然失败(我认为这是因为更改用户也会更改外壳)。
在那之后,我发现了 Fabric 模块,但我能做的最好的事情是在正确的用户登录的情况下打开 SSH shell,但没有选择从代码运行任何命令。
有没有办法做到这一点?最终目标可能是这样的:
ssh.login(temp_user, pass)
ssh.command("su -l myuser")
expect("Password: ", ssh.send("mypass\n")
ssh.command("somescript.sh > datadump.txt")
Run Code Online (Sandbox Code Playgroud)
使用sudo是不可能的,以及添加无密码登录。
正如这里所建议的那样,我尝试使用 Paramiko 的代码:
import paramiko
host = "hostip"
user = "user"
user_to_log = "myuser"
password = "pass"
password_to_log = "mypass"
login_command = "su -l " + user_to_log
ssh = paramiko.SSHClient()
ssh.load_system_host_keys()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostip, username=user,
password=password)
transport = ssh.get_transport()
session = transport.open_session()
session.set_combine_stderr(True)
session.get_pty()
session.exec_command("su -l " …Run Code Online (Sandbox Code Playgroud)