ssh expect login

Hello
I use the following script to extract information from MikroTik. Unfortunately, after his performance did not get any feedback on my screen.

#!/usr/bin/perl

use Net::SSH::Expect;

$ssh = Net::SSH::Expect->new (
host => “xx.xx.xx.xx”,
port => “22”,
password => ‘pass’,
user => ‘admin’,
raw_pty => 0
);

$ssh->login() or die (“Dont work”);

($out) = $ssh->send(‘/queue simple print\r’);

($out =~ /(\d+)/) && (($out) = ($1));
print $out;

You might try:

($out) = $ssh->send('/queue simple print;/quit\r');

I do not use expect, I am just using something like:

ssh router.fqdn "/queue simple print; /quit;"

I have had trouble getting output without sending the quit command. It does not give me trouble every time. Perhaps SSH disconnects too quickly for me. I do get a stray “interrupted” when I send quit. I can simply ignore it in my situation.

Also, there are simple queues on the router? You get results when you run that command manually?

I have the following script working. I would like to result command: $nazwa send – “/system identity print\r” executed saved to a file. At the moment, I can only write path to the file tmp.

#!/bin/bash
HOSTNAME=“xx.xx.xx.xx”
PORT=“22422”
USER=“admin”
PASS=“pass”
TMP=$(mktemp)
PATH=“/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin”

create expect script

cat > $TMP << EOF
#exp_internal 1 # Uncomment for debug
set timeout -1
spawn ssh -p$PORT $USER@$HOSTNAME
match_max 100000
expect -exact “password:”
send – “$PASS\r”
expect " > "
$nazwa send – “/system identity print\r”
expect " > "
send – “quit\r”
expect eof
EOF

run expect script

#cat $TMP # Uncomment for debug
expect -f $TMP
echo $TMP >> log.log

remove expect script

rm $TMP