我想實作的是:
使用服務器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
然后在服務器A中執行Python腳本:
def test_paramiko_interact():
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname='10.46.169.66', port=22, username='user', password='pwd')
shell_line = 'sh /home/study_shell.sh\n'
channel = ssh.invoke_shell()
channel.send(shell_line)
while True:
rst = str(channel.recv(1024))
print(rst)
if 'yes/no' in rst:
channel.send('yes\n')
time.sleep(3)
break
# with SSHClientInteraction(ssh, timeout=2, display=True) as interact:
# stdin, stdout, stderr = ssh.exec_command(shell_line)
# rst = stdout.read()
# ssh.close()
# print(rst)
ssh.close()
但是結果沒有作用呀?請問如何處理呢?
非常感謝!
uj5u.com熱心網友回復:
這種情況需呼叫expect命令uj5u.com熱心網友回復:
您好,能給出一個使用的例子嗎?
channel.expect()
嗎?
還有我這個情況是有時會出現 yes/no,有時不會出現(就不需要channel.send('yes')了),請問這種分支如何處理呢?
uj5u.com熱心網友回復:
stdin, stdout, stderr = ssh.exec_command('sh /home/study_shell.sh\n';yes | reload')轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/169937.html
上一篇:MILP問題,R語言
