expect script on crontab not works completely - cron

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.

Related

unmanaged task in 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.

open telnet using shell and passing commands

I am new to linux and shell scripting. I want to connect to localhost and interact it.
#! /bin/bash
(exec /opt/scripts/run_server.sh)
when i execute this bash script, it starts listening on a port.
Listening on port xxxxx
Now i want to issue this command "telnet localhost xxxxx"
I tried something like this:
#! /bin/bash
(exec /opt/opencog/scripts/run_server.sh)&&
telnet localhost xxxxx
It is still listening on the port. But i think second command is not running. I expect another window showing that it is being connected like this.
vishnu#xps15:~$ telnet localhost xxxx
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
server>
The reason why i executing these as a script is that, automatically in the server i need to carry out some process by issuing certain commands like this "scm" "parse" etc.....
vishnu#xps15:~$ telnet localhost xxxx
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
server>scm
Entering scheme shell; use ^D or a single . on a line by itself to exit.
guile> (parse "i eat apple")
I have lots of text coming. Manually i cant issue this parse command for each and every sentence. so i want to automate. So i need to write a script for connecting to the server and interacting.
Any guidelines. Finally How to interact/send commands to this guile shell?
One way to login to the linux server as a same or different user and run some command or .sh script (very useful for post-commit hooks or cron jobs) is to use program called sshpass, for example a cron job command or svn post-commit hook would look like this:
/usr/bin/sshpass -p 'password' /usr/bin/ssh
-o StrictHostKeyChecking=no -q user#localhost 'any command'
Just replace password with your password, and user with your user, and put command that you need to run as that particular user...
To install sshpass it on ubuntu just type
apt-get install sshpass
Or on CentOs
yum install sshpass
I solved this with the netcat (nc) command.
$ echo "command1\ncommand2\n" | nc localhost xxxxx
I could manually connect to localhost using telnet localhost xxxx and then i can pass commands from shell to localhost like this.
If you need to use telnet, this solution may help you. Otherwise, use ssh, as other answer suggests.
You can use anything that produces output to write lines one by one, followed by "\r\n", and pipe these lines to ncat, e.g.:
echo -e "command1\r\ncommand2\r\n" | ncat localhost 5000
-e option makes echo interpret "\r\n" as special symbols.

Run remote command from script

I need to run a shell script from within jenkins, to commit changes after making the build. Deploying build to remote server is not a problem, so the new build is there. All I need to do is just commit it.
For that I need to login with ssh to that remote server using shell script, and so far it is okay:
#!/user/bin/expect -f
spawn ssh myusername#url
expect "password:"
send "mypassword\r"
interact
So now when I am logged in, I want to run a few commands: cd /path/to/repository; svn commit -m "Some change log"
I tried something like:
#!/user/bin/expect -f
spawn ssh -o "LocalCommand cd /path/to/repository" myusername#url
expect "password:"
send "mypassword\r"
But it just don't work, as I have no idea how to do it.
If anyone know how to do it, please let me know.
The remote server is running on linux, and the jenkins on osx.
I found solution just with expect:
#!/user/bin/expect -f
spawn ssh myusername#url
expect "password:"
send "mypassword\r"
expect "some server prompt"
send "cd /path/to/repository\r"
send "svn commit -m 'Some change log'\r"
EDIT:
This solution seems to work from time to time only. I mean commiting changes.

How can I easily check if a POP3 or SMTP connection is valid with perl / a shell script?

I have setup an SSH tunnel to my mail server as follows:
ssh -o ServerAliveInterval=60 -f me\#mydomain.com -L 63110:mail.mydomain.com:110 -N
ssh -o ServerAliveInterval=60 -f me\#mydomain.com -L 63325:mail.mydomain.com:25 -N
I can send/receive mail for a while, but after a period of inactivity, my mail client reports that it doesn't get a valid greeting from the mail server.
I have a perl script that checks every minute to make sure that the ssh tunnel is running (via ps) and that the port is open (using IO::Socket::PortState qw(check_ports)), but I would like to check whether or not I get a valid greeting as well.
What would be the best way to do this either in perl or a shell script (running Ubuntu 12.04)?
To answer the question, notwithstanding the setup of the OP, THE tool to use is swaks aka the Swiss Army Knife of SMTP. You can get it from here: http://jetmore.org/john/code/swaks/
Typically to test your smtp you would use a command like this: swaks --server mail.example.com --from ben.holness#example.com --to ben.holness#somewhere.example.com
It will then show you all the dialogue between the smtp client and the server, making it really easy to pinpoint the source of possible problems.

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