SSH /登录到网络并注销在buildserver [英] ssh/login to a buildserver on the network and logout

查看:207
本文介绍了SSH /登录到网络并注销在buildserver的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想ssh到网络上的buildserver并运行一些命令和服务器的注销,我已经看过其他职位
并写了下面的code,但它不工作?任何人都可以提出什么是错还是有更好的方式来做到这一点?在此先感谢

 导入OS
进口SYS
进口Pexpect的
#使用os.system(SSH用户名@ buildservername)
孩子= pexpect.spawn('SSH用户名@ buildservername',日志文件= sys.stderr即可)
#child.expect('你确定要继续连接(是/否)?)
#child.sendline('是')
#child.expect('密码:')
child.sendline('密码')
CMD ='主机'
使用os.system(CMD)
os.chdir('//本地的/ mnt /工作区')
os.mkdir('NEWDIR')
os.getcwd()


解决方案

您可能需要看起来的的的paramiko 库,特别是具有了的 SFTPClient

这是一个原生的Python SSHv2的协议库。

 进口的paramikoSSH = paramiko.SSHClient()
ssh.load_system_host_keys()
ssh.connect('buildservername'用户名='yadomi',密码='密码')SFTP = ssh.open_sftp()
sftp.chdir('/本地的/ mnt /工作区')
sftp.mkdir('NEWDIR')

I am trying to ssh into a buildserver on the network and run some commands and logout of the server,i have looked at other posts and written the following code but its not working?can anyone suggest what is wrong or is there a better way to accomplish this?thanks in advance

import os
import sys
import pexpect
#os.system(ssh username@buildservername)
child = pexpect.spawn('ssh username@buildservername', logfile=sys.stdout)
#child.expect('Are you sure you want to continue connecting (yes/no)?')
#child.sendline('yes')
#child.expect('password:')
child.sendline('password')
cmd = 'hostname'
os.system(cmd)
os.chdir('//local/mnt/workspace')
os.mkdir('newdir')
os.getcwd()

解决方案

You may take a look a the Paramiko library, especialy the SFTPClient

It's a native Python SSHv2 protocol library.

import paramiko

ssh = paramiko.SSHClient()
ssh.load_system_host_keys()
ssh.connect('buildservername', username='yadomi', password='password')

sftp = ssh.open_sftp()
sftp.chdir('/local/mnt/workspace')
sftp.mkdir('newdir')

这篇关于SSH /登录到网络并注销在buildserver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆