嗨我正在使用python并尝试连接到sftp并想从那里检索xml文件并需要放在我的本地系统中,下面是代码
import paramiko
sftpURL = 'sftp.somewebsite.com'
sftpUser = 'user_name'
sftpPass = 'password'
ssh = paramiko.SSHClient()
# automatically add keys without requiring human intervention
ssh.set_missing_host_key_policy( paramiko.AutoAddPolicy() )
ssh.connect(sftpURL, username=sftpUser, password=sftpPass)
ftp = ssh.open_sftp()
files = ftp.listdir()
print files
Run Code Online (Sandbox Code Playgroud)
这里连接已成功完成,现在我想查看所有文件夹和所有文件,并需要输入所需的文件夹以从那里检索xml文件.
最后我的目的是在连接到sftp服务器后查看所有文件夹和文件.在上面的代码中我使用了ftp.listdir()通过它获得输出,如下所示
['.bash_logout', '.bash_profile', '.bashrc', '.mozilla', 'testfile_248.xml']
Run Code Online (Sandbox Code Playgroud)
我想知道这些是否是唯一存在的文件?
我上面使用的命令也是查看文件夹的权利吗?
查看所有文件夹和文件的命令是什么
我想编写一个程序(在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)
这段代码存在一些问题:
我对这种"非决定论"感到困惑,非常感谢你的帮助.
Paramiko的SFTPClient显然没有exists方法.这是我目前的实施:
def rexists(sftp, path):
"""os.path.exists for paramiko's SCP object
"""
try:
sftp.stat(path)
except IOError, e:
if 'No such file' in str(e):
return False
raise
else:
return True
Run Code Online (Sandbox Code Playgroud)
有一个更好的方法吗?在异常消息中检查子字符串非常难看,并且可能不可靠.
我尝试过的:
invoke_shell()然后channel.send su发送密码导致不是rootinvoke_shell()然后channel.exec_command导致"频道关闭"错误_transport.open_session()然后channel.exec_command导致不是根invoke_shell() 然后写入stdin并冲洗它导致不是root我正在使用Paramiko的SFTPClient在主机之间传输文件.我希望我的脚本打印文件传输进度类似于使用scp看到的输出.
$ scp my_file user@host
user@host password:
my_file 100% 816KB 815.8KB/s 00:00
$
Run Code Online (Sandbox Code Playgroud)
任何的想法?
提前致谢
我使用Python的paramiko数据包来保持与服务器的ssh连接:
s = paramiko.SSHClient()
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
s.connect("xxx.xxx.xxx.xxx",22,username=xxx,password='',timeout=4)
Run Code Online (Sandbox Code Playgroud)
我想用这个ssh-connection将文件传输到ssh服务器,我该怎么办?
就像使用
scp a-file xxx@xxx.xxx.xxx.xxx:filepath
命令一样?
def download():
if os.path.exists( dst_dir_path ) == False:
logger.error( "Cannot access destination folder %s. Please check path and permissions. " % ( dst_dir_path ))
return 1
elif os.path.isdir( dst_dir_path ) == False:
logger.error( "%s is not a folder. Please check path. " % ( dst_dir_path ))
return 1
file_list = None
#transport = paramiko.Transport(( hostname, port))
paramiko.util.log_to_file('paramiko.log')
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
#transport
try:
ssh.connect( hostname, username=username, password=password, timeout=5.0)
#transport.connect(username=username, password=password )
except Exception, err:
logger.error( "Failed to connect to the …Run Code Online (Sandbox Code Playgroud) 我需要发送一些特殊的击键,并且不确定如何做到这一点.
我需要发送Ctrl+ Q后跟Ctrl+ A到终端(我正在使用Paramiko).
我试过了
shell = client.invoke_shell()
shell.send(chr(10))
time.sleep(5)
shell.send(chr(13))
shell.send('\x11')
shell.send('\x01')
print 'i tried'
Run Code Online (Sandbox Code Playgroud)
我可以看到两个返回成功,但没有,它没有退出picocom(也注意我有错误的方式,它期望ctrl + a,然后ctrl + q)
正如你在第2步看到的那样
Step 2 Exit the session from the switch, press Ctrl-a and Ctrl-q from your keyboard:
Switch# <type ^a^q>
Thanks for using picocom
Router#
Run Code Online (Sandbox Code Playgroud)
更新:
我试过\ x01\x16\x11 \n但是这会返回
Switch#
Switch#
*** baud: 9600
*** flow: none
*** parity: none
*** databits: 8
*** dtr: down
Switch#
Run Code Online (Sandbox Code Playgroud)
看起来这可能是另一个特殊命令?
我正在将Paramiko用于tail -f远程服务器上的文件.
以前,我们正在运行此通道ssh -t,但事实证明这种情况很脆弱,并且-t导致我们的远程调度系统出现问题.
我的问题是当脚本捕获SIGINT时如何杀死尾巴?
我的脚本(基于python paramiko模块中长期运行的ssh命令(以及如何结束它们))
#!/usr/bin/env python2
import paramiko
import select
client = paramiko.SSHClient()
client.load_system_host_keys()
client.connect('someserver', username='victorhooi', password='blahblah')
transport = client.get_transport()
channel = transport.open_session()
channel.exec_command("tail -f /home/victorhooi/macbeth.txt")
while True:
try:
rl, wl, xl = select.select([channel],[],[],0.0)
if len(rl) > 0:
# Must be stdout
print channel.recv(1024)
except KeyboardInterrupt:
print("Caught control-C")
client.close()
channel.close()
exit(0)
Run Code Online (Sandbox Code Playgroud)
该脚本Ctrl-C成功捕获了我的结尾.但是,它使tail -f进程在远程系统上运行.
client.close()和channel.close()似乎都没有终止它.
我可以在except块中发出什么命令来杀死它?
远程服务器正在运行Solaris 10.
我在python中使用'paramiko'在一个盒子上运行命令SSH.如何使用paramiko日志记录?我的意思是强制它生成日志(在文件或终端中)并设置日志级别.