如何在Python 3中利用paramiko库进行实用的交互操作(简单实例与全面解析)
最编程
2024-02-10 10:14:03
...
我想实现的是:
使用服务器A 登录 服务器B,把服务器B的文件用SCP 发到服务器C中,因为有yes/no 的输入,不知道用paramiko模块实现?
我在服务器B中写了一个.sh用于测试:
study_shell.sh
#! /bin/bash
echo "hello, shell! 0130"
choice="null"
echo "${choice}"
read -p "Do you want to build dir?(yes/no): " choice
echo "${choice}"
if [ ${choice} == 'yes' ]
then
echo "OK, we choice yes"
mkdir we_choice_YES
else
echo "We not choice yes..."
mkdir we_not_choice_yes
fi
显然我们想实现选择 yes
我们在另外一台服务器执行:
import paramiko
import time
__author__ = 'weiran'
def test_paramiko_interact():
trans = paramiko.Transport(('10.46.169.111', 22)) # 【坑1】 如果你使用 paramiko.SSHClient() cd后会回到连接的初始状态
trans.start_client()
# 用户名密码方式
trans.auth_password(username='user', password='pwd')
# 打开一个通道
channel = trans.open_session()
channel.settimeout(7200)
# 获取一个终端
channel.get_pty()
# 激活器
channel.invoke_shell()
cmd = 'cd /home/shell_study\r'
# 发送要执行的命令
channel.send(cmd)
cmd = 'bash ./study_shell.sh\r' # 【坑2】 如果你使用 sh ./study_shell.sh\r 可能会出现 [: 11: y: unexpected operator 错误
channel.send(cmd)
# 回显很长的命令可能执行较久,通过循环分批次取回回显
while True:
time.sleep(0.2)
rst = channel.recv(1024)
rst = rst.decode('utf-8')
print(rst)
# 通过命令执行提示符来判断命令是否执行完成
if 'yes/no' in rst:
channel.send('yes\r') # 【坑3】 如果你使用绝对路径,则会在home路径建立文件夹导致与预期不符
time.sleep(0.5)
ret = channel.recv(1024)
ret = ret.decode('utf-8')
print(ret)
break
channel.close()
trans.close()
# channel.invoke_shell()
if __name__ == '__main__':
test_paramiko_interact()
参考:
https://blog.****.net/qq_29778641/article/details/82186438
https://blog.****.net/songfreeman/article/details/50920767
这些链接作者都很牛,但有时我们往往只需要简单操作实现就行,有深入了解的小伙伴推荐看看~