Community discussions

MikroTik App
 
angelo18
just joined
Topic Author
Posts: 13
Joined: Fri May 11, 2012 11:55 am

ssh expect login

Tue Jan 22, 2013 2:33 pm

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;
 
lambert
Long time Member
Long time Member
Posts: 548
Joined: Fri Jul 23, 2010 1:09 am

Re: ssh expect login

Tue Jan 22, 2013 10:38 pm

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?
 
angelo18
just joined
Topic Author
Posts: 13
Joined: Fri May 11, 2012 11:55 am

Re: ssh expect login

Wed Jan 23, 2013 10:46 am

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

Who is online

Users browsing this forum: No registered users and 16 guests