前提
いつもお世話になっております。
ネットワーク機器(Fortigate)のCLIプロンプトをnetmikoのsend_multilineを使用して返答が出来るかの検証を行っています。FortinetSSHがsend_multilineという属性を持ち合わせてない為、出るエラーという事はわかってるのですがどこを修正すればいいのかわかりません。Python初心者のため、公式を参考にしてソースは作成しました。原因含めご教授頂けないでしょうか?send_multiline以外の他方法もありましたら情報提供よろしくお願い致します。
11/25 ソースのインデント、エラーコード修正しました。
11/25 13:55 エラーコード修正しました。
11/25 17:04 試した事修正しました。
11/25 18:26 試した事、エラーコード修正しました。
netmiko公式
https://pynet.twb-tech.com/blog/netmiko4-send-multiline.html
実現したいこと
ネットワーク機器(Fortigate)のCLIプロンプトにnetmikoを使って返答したい(他方法含め)
発生している問題・エラーメッセージ
raise SSHException("No authentication methods available")
paramiko.ssh_exception.SSHException: No authentication methods available
→本文のpasswordのつづりに間違いがありました。申しわけないです。
output = net_connect.send_multiline(cmd_list)
AttributeError: 'FortinetSSH' object has no attribute 'send_multiline'
→netmikoのVerUp試した所、上記エラーは出なくなりました。
(現在のエラーコード)
cmd, expect_string = cmd_item
ValueError: not enough values to unpack (expected 2, got 1)
恐らく2つの要素が必要なのに、一つしかないという意味?cmd_list内見直し中です。
該当のソースコード
from netmiko.fortinet import FortinetSSH from netmiko import ConnectHandler device = {'device_type': 'fortinet', 'ip': '192.168.1.2', 'username': 'admin', 'password': 'Password', 'port': 22, } with ConnectHandler(**device) as net_connect: cmd_list = [ ["execute federated-upgrade initialize"], ["\n", r"immediate | hh:mm YYYY/MM/DD"], ["immediate", "\n"], ["7.2.3", "\n"], ["y", "\n"], ["n", "\n"], ["n", "\n"], ["n", "\n"], ["\n", r"confirm | cancel"], ["confirm", ""], ] output = net_connect.send_multiline(cmd_list) print(output) #net_connect.disconnect()
試したこと
・netmikoのVerUP 3.4.2→4.0.0
・netmikoがfortinetをサポートしている事は確認済
・print(dir(FortinetSSH))で確認。クラスがサポートしている属性の中にsend_multiineが存在しない(これが出来ない原因なのかは不明です。)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__enter__', '__eq__', '__exit__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_autodetect_fs', '_build_ssh_client', '_connect_params_dict', '_first_line_handler', '_lock_netmiko_session', '_modify_connection_params', '_open', '_read_channel', '_read_channel_expect', '_read_channel_timing', '_retrieve_output_mode', '_sanitize_output', '_test_channel_read', '_timeout_exceeded', '_try_session_preparation', '_unlock_netmiko_session', '_use_ssh_config', '_write_channel', '_write_session_log', 'check_config_mode', 'check_enable_mode', 'cleanup', 'clear_buffer', 'close_session_log', 'commit', 'config_mode', 'disable_paging', 'disconnect', 'enable', 'establish_connection', 'exit_config_mode', 'exit_enable_mode', 'find_prompt', 'is_alive', 'normalize_cmd', 'normalize_linefeeds', 'open_session_log', 'paramiko_cleanup', 'read_channel', 'read_until_pattern', 'read_until_prompt', 'read_until_prompt_or_pattern', 'run_ttp', 'save_config', 'select_delay_factor', 'send_command', 'send_command_expect', 'send_command_timing', 'send_config_from_file', 'send_config_set', 'serial_login', 'session_preparation', 'set_base_prompt', 'set_terminal_width', 'special_login_handler', 'strip_ansi_escape_codes', 'strip_backspaces', 'strip_command', 'strip_prompt', 'telnet_login', 'write_channel']
・代替案pexpect使用→通らない
(簡単にstatusを表示するコマンドが通るか確認)
公式ページ
https://pexpect.readthedocs.io/en/stable/index.html
import pexpect connection = pexpect.spawn("ssh admin@192.168.1.2") connection.expect("password:", timeout=120) connection.sendline("Password") connection.expect("FortiGate-60E#") connection.sendline("get system status")
結果、timeout?になる
raise TIMEOUT(str(err) + '\n' + str(self)) pexpect.TIMEOUT: Timeout exceeded. <pexpect.spawn object at 0x7f4b9a0e12b0> version: 3.3 command: /usr/bin/ssh args: ['/usr/bin/ssh', 'admin@192.168.1.2] searcher: <pexpect.searcher_re object at 0x7f4b92667518> buffer (last 100 chars): b' \r\nFortiGate-60E # ' before (last 100 chars): b' \r\nFortiGate-60E # ' after: <class 'pexpect.TIMEOUT'> match: None match_index: None exitstatus: None flag_eof: False pid: 3939 child_fd: 3 closed: False timeout: 30 delimiter: <class 'pexpect.EOF'> logfile: None logfile_read: None logfile_send: None maxread: 2000 ignorecase: False searchwindowsize: None delaybeforesend: 0.05 delayafterclose: 0.1 delayafterterminate: 0.1

回答1件
あなたの回答
tips
プレビュー