Shell script successful telnet login, how to issue commands after that? - shell

#!/usr/bin/expect -f
spawn telnet 10.21.0.17
expect -re "login"
send "admin\n"
expect -re "Password"
send "supersecurepassword\n"
interact
works as expected. Upon running the script I am logged in to whatever telent IP I used in the line spawn telnet 10.21.0.17
Then it drops me to the Shell of the AP
WAP->
How do I issue further commands? I'd like to issue reboot and then maybe a sleep 20 and finally exit.
I have tried using echo and expect with no success. I've also tried with removing the interact with no success. Any ideas?

This was resolved by simply adding a sleep before the expect, and of course not including interact, the following works well:
#!/usr/bin/expect -f
spawn telnet 10.21.0.17
expect -re "login"
send "admin\n"
expect -re "Password"
send "supersecurepassword\n"
sleep 5
expect "WAP"
send "reboot\n"
send "exit\n"
For reference, this was used to automate a reboot on a D-Link DAP-2590 wireless access point. Now that I know this though, I may use it for other things: changing passwords, etc. Hope it helps someone else in the future.

Related

linux expect in background

I use the following bash script to connect to pbx using telnet:
expect.sh:
#!/usr/bin/expect
spawn telnet [ip] 2300
expect -exact "-"
send "SMDR\r";
expect "Enter Password:"
send "PASSWORD\r";
interact
and created another script to redirect the result to a file:
#!/bin/bash
./expect.sh | tee pbx.log
I'm trying to run expect.sh at boot time so I added it to systemd. When I add it as service in /etc/systemd/system it runs but I can't get the results in the log file as if I run both scripts manually
any idea about how can I run it at boot time?
TIA
If you just want to permanently output everything received after providing your password, simply replace your interactive with expect eof, i.e. wait for end-of file which will happen when the connection is closed by the other end. You will probably also want to change the default timeout of 10 seconds with no data that will stop the command:
set timeout -1
expect eof

How do I redirect stdout from a program continuously into a spawned process using expect?

Need to open telnet, send a few commands and then send stdout from pocketsphinx.
Currently expect will wait until the program is finished and then output everything to the telnet process.
I need pocketsphix to continuously feed the spawned telnet process.
This is what I have so far:
#!/usr/bin/expect -d
set send_human {.1 .3 1 .05 2}
spawn telnet 192.168.1.104 23
expect “*”
send "\x01"; send "2\r"
expect “:”
send -h "hello world\r"
send -h "goodbye world\r"
send -h "Test Test Test\r"
send -- [exec pocketsphinx_continuous -infile speech.wav 2> /dev/null ]\n
You can use expect command interact for connecting together two spawned processes.
By default, interact expects the user to be writing stdin and reading stdout of the Expect process
itself. The -u flag (for "user") makes interact look for the user as the process named by its argument
(which must be a spawned id).
This allows two unrelated processes to be joined together without using an explicit loop. To aid in
debugging, Expect diagnostics always go to stderr (or stdout for certain logging and debugging information).
For the same reason, the interpreter command will read interactively from stdin.
For example
set send_human {.1 .3 1 .05 2}
spawn telnet 192.168.1.104 23
expect “*”
send "\x01"; send "2\r"
expect “:”
send -h "hello world\r"
send -h "goodbye world\r"
send -h "Test Test Test\r"
set sid_telnet $spawn_id
spawn pocketsphinx_continuous -infile speech.wav 2> /dev/null
interact -u $sid_telnet

Why can't tranfer file into the remote vps with expect?

The expect has been installed, it_a_test is the vps password.
scp /home/wpdatabase_backup.sql root#vps_ip:/tmp
The command can transfer file /home/wpdatabase_backup.sql into my vps_ip:/tmp.
Now i rewrite the process into the following code:
#!/usr/bin/expect -f
spawn scp /home/wpdatabase_backup.sql root#vps_ip:/tmp
expect "password:"
send it_is_a_test\r
Why can't transfer my file into remote vps_ip with expect?
Basically, expect will work with two feasible commands such as send and expect. In this case, if send is used, then it is mandatory to have expect (in most of the cases) afterwards. (while the vice-versa is not required to be mandatory)
This is because without that we will be missing out what is happening in the spawned process as expect will assume that you simply need to send one string value and not expecting anything else from the session, making the script exits and causing the failure.
So, you just have to add one expect to wait for the closure of the scp command which can be performed by waiting for eof (End Of File).
#!/usr/bin/expect -f
spawn scp /home/wpdatabase_backup.sql root#vps_ip:/tmp
expect "password:"
send "it_is_a_test\r"
expect eof; # Will wait till the 'scp' completes.
Note :
The default timeout in expect is 10 seconds. So, if the scp completes, within 10 seconds, then no problem. Suppose, if the operation takes more than that, then expect will timeout and quit, which makes failure in scp transfer. So, you can set increase timeout if you want which can be modified as
set timeout 60; # Timeout is 1 min

Execute remote commands from Debian to Windows via Telnet

I am working on remotely executing a command line in Windows from Debian. For that, I tried to use the bash script below. Using the expect tool, it consists in connecting via telnet to the remote server, entering username and password values and sending the command line desired.
#!/usr/bin/expect
set timeout 20
set name 192.168.1.46
set user Administrateur
set password MSapp/*2013
set cmd "TASKKILL /F /IM Tomcat6.exe"
spawn telnet 192.168.1.46
expect "login:"
send "$user\r"
expect "password:"
send "$password\r"
expect "C:\Users\Administrateur>"
send "$cmd\r"
The telnet connection is well established. But, the command line is not executed.
Could someone tell me what is wrong with my script?
Just add one more expect statement at the end, like as follows,
send "$cmd\r"
expect "C:\Users\Administrateur>"
Basically, expect will work with two feasible commands such as send and expect. If send is used, then it is mandatory to have expect (in most of the cases) afterwards. (while the vice-versa is not required to be mandatory)
This is because without that we will be missing out what is happening in the spawned process as expect will assume that you simply need to send one string value and not expecting anything else from the session.

Expect script sending command prematurely

I have an expect script that logs into several computers through ssh and starts programs. It has been working fine for a while from but now suddenly a problem has appeared.
It happens at the same time every run; after logging out of a certain computer it attempts to log into the next one before the prompt is ready. That is, the lines
#!/usr/bin/expect
set multiPrompt {[#>$] }
(...)
expect -re $multiPrompt
send "exit\r"
expect -re $multiPrompt
spawn ssh name#computer4.place.com
which should (and normally does) give the result
name#computer3:~$ exit
logout
Connection to computer3.place.com closed.
name#computer1:~$ ssh name#computer4.place.com
instead gives
name#computer3:~$ exit
logout
ssh name#computer4.place.com
Connection to computer3.thphys.nuim.ie closed.
name#computer1:~$
and then it goes bananas. In other words the ssh ... doesn't wait for the prompt to appear.

Resources