unmanaged task in linux - linux

I trying to create a unmanaged script to manage my tp-link switch (change state, description, and speed,...). Tp-link uses a similar connection that cisco... for example: Deactivate a port:
enable
configure
interface gigabitEthernet 1/0/20
shutdown
If I connect to my switch (with ssh) I can execute all commands without problems but I use command line don't works:
For example:
ssh x.x.x.x "sh interface configuration"
This command should be show the status of my ports but not shows nothing. User and password is ok. SSH returns:
Authenticated with partial success.
Connection to 192.168.20.26 closed by remote host.
Can you says other options to execute this?
I try this execute this options with expect and telnet and results is ok but if I executed expect with ssh connection don't works neither.
My expect script:
#!/usr/bin/expect -f
spawn -noecho telnet 192.168.20.26
expect "User:"
send -- "admin\r"
expect "Password:"
send -- "mypass\r"
send -- "enable\r"
send -- "configure\r"
send -- "interface gigabitEthernet 1/0/20\r"
send -- "description hola\r"
send -- "exit\r"
send -- "exit\r"
send -- "exit\r"
send -- "exit\r"
send -- "exit\r"
interact
The script works fine but if I execute this script from remote host don't works.
Best regards.

The problem above was caused by the fact that ssh failed to allocate PTY automatically. ssh -t fixed the issue.

Related

Expect/Send Issue

Currently, I am working on a script to automatize a process, in this point my script is short and simple but I have had some Issues with expect/send.
code:
#!/usr/bin/expect -f
#!/bin/bash
set ip ***
set ip2 ***
set user ***
set usr2 ***
set OTP [lindex $argv 0]
spawn ssh "usr#$ip";
expect "OTP Password:"
send -- "$OTP"
interact
expect "prompt >"
send -- "ssh usr2#$ip2"
interact
For this point script works until the first ssh but... for the second ssh (expect "prompt >" / send -- "ssh $ip2") It doesn't work... I don't get the idea why. I have tried with some commands like expect eof, wait, timeout and nothing as well I checked to expect version is on latest (5.45).
Do you have any idea? thanks!
Your spawn looks fine to me, but in the send, you forgot to send the carriage return, which actually terminates the command:
send -- "$OTP\r"

Connect via ssh and run a program after a switch user

I want to create inside a bash script a funcion that connects via ssh to another server (using rsa keys), do a switch user (inserting the password), start a program and then exit with the exit code of the program started.
Below a test I'm doing:
#! /usr/bin/expect
set timeout 120
spawn ssh user1#10.211.55.24
expect ".*user1"
sleep 3
send "whoami\r"
send "/bin/su hdfs\r"
expect "*?assword:"
send "hdfs\r"
expect "$"
send "whomai\r"
send "exit\r"
It works until the switch user, I can switch to the hdfs user but the following commands are not sent (whomai). The $ prompt is correct. Furthermore I'm not able to get the exit code of the command (in this example echo command).

How to download file through linux expect?

I face a trouble about linux expect script.
I have a computer A which only have LAN connection, while another computer B in the same LAN which have Internet connection. What I want to do is writing a script which can login into computer B through ssh and download file to it and finally use scp to transfer the file to computer A.
And below is my script.
#!/bin/expect
set url [lindex $argv 0];
spawn ssh "user#computer-B"
expect "password:"
send "passwd\n"
expect "Last login:"
send "cd tmp\n"
send "wget $url\n"
expect "saved"
send "scp * user#computer-A:~/\n"
expect {
"yes/no" { send "yes\n"; exp_continue }
"password:" { send "passwd\n" }
}
expect "100%"
send "rm *\n"
send "exit\n"
But now the script will scp the file to A immediately not until wget finishing. Is my script close to the proper way to do it? If not what should I do? Thanks very much.
You don't need expect, and you could use key-based login to make your life easier.
SOCKS
You could use a socks proxy.
From computer A :
ssh -D 1080 address-of-B
followed by
export SOCKS_SERVER=127.0.0.1:1080
You can now use wget from computer A.
One liner
From computer A :
ssh computer-B 'wget -O - $url' >> filename_on_computer_a

expect script on crontab not works completely

I am trying to write an expect script to telnet a machine and reboot it. When I run the script manually it works as expected
But when I try to run it autoaticall on crontab it doesn't complete the process.
The script is :
#!/usr/local/bin/expect
spawn telnet 192.168.1.1
expect "login:"
send "root\r"
expect "Password:"
send "<password>\r"
send "bash\r"
send "cd /opt\r"
send "reboot\r"
interact
the output of the script ( when it is run by cron )
spawn telnet 192.168.1.1
Trying 192.168.1.1...
Connected to 192.168.1.1.
Escape character is '^]'.
DD-WRT v24-sp2 std (c) 2010 NewMedia-NET GmbH
Release: 08/07/10 (SVN revision: 14896)
RK-SWOT-2 login: root
Password:
and that's it. no more go forward on the cron.
Can you help me for this ?
I've fixed the issue.
I think the problem was with the interact command. I changed it to
expect eof
and the problem gone.

How to automate telnet session using Expect?

I'm trying to write an expect script to automate telnet. This is what I have so far.
#!/usr/bin/expect
# Test expect script to telnet.
spawn telnet 10.62.136.252
expect "foobox login:"
send "foo1\r"
expect "Password:"
send "foo2\r"
send "echo HELLO WORLD\r"
# end of expect script.
Basically, what I want to do is telnet to the following IP address and then echo HELLO WORLD. However, it seems that the script fails after attempting to telnet...I'm not sure if it's able to accept login and password input, but it is not echoing HELLO WORLD. Instead, I just get this output:
cheungj#sfgpws30:~/justin> ./hpuxrama
spawn telnet 10.62.136.252
Trying 10.62.136.252...
Connected to 10.62.136.252.
Escape character is '^]'.
Welcome to openSUSE 11.1 - Kernel 2.6.27.7-9-pae (7).
foobox login: foo1
Password: foo2~/justin>
It's hard to tell, but from the output you're pasting it looks like:
Your script isn't waiting for login to complete before sending the next command.
Your script is exiting and closing the process before you can see any output.
There are no guarantees in life, but I'd try this as a first step:
#!/usr/bin/expect -f
spawn telnet 10.62.136.252
expect "foobox login:"
send "foo1\r"
expect "Password:"
send "foo2\r"
# Wait for a prompt. Adjust as needed to match the expected prompt.
expect "justin>"
send "echo HELLO WORLD\r"
# Wait 5 seconds before exiting script and closing all processes.
sleep 5
Alternatives
If you can't get your script to work by manually programming it, try the autoexpect script that comes with Expect. You can perform your commands manually, and autoexpect will generate an Expect typescript based on those commands, which you can then edit as needed.
It's a good way to find out what Expect actually sees, especially in cases where the problem is hard to pin down. It's saves me a lot of debugging time over the years, and is definitely worth a try if the solution above doesn't work for you.
You're sending the echo command without first expecting the prompt. Try:
# after sending the password
expect -re "> ?$"
send "echo HELLO WORLD\r"
expect eof
Have you seen this StackOverflow Question?
He seems to have got things working by using curly braces.
Here is a simplified version
#!/usr/bin/expect
# just do a chmod 755 one the script
# ./YOUR_SCRIPT_NAME.sh $YOUHOST $PORT
# if you get "Escape character is '^]'" as the output it means got connected otherwise it has failed
set ip [lindex $argv 0]
set port [lindex $argv 1]
set timeout 5
spawn telnet $ip $port
expect "'^]'."

Resources