Exporting data from expect script - bash

so i've written a short expect script which logs into a APC Power Distribution Unit interface via telnet and polls the current ampage.
#!/usr/bin/expect
set ip "192.168.0.1"
set username "myusername"
set password "mypassword"
spawn "/bin/bash"
send "telnet $ip\r"
expect "*User Name*"
send "$username\r"
expect "*Password*"
send "$password\r"
expect "*APC*"
send -- "phReading all current\r"
expect "*Success*"
send "quit\r"
expect eof
The script does its job and I see the amps on screen, displayed like this:
apc>phReading all current
E000: Success
1: 7.5 A
apc>quit
What i need to do is 'export' that 7.5 figure, either to a text file or pass it to a bash script as a variable.
Any ideas on how i can do this?
Thank you!

Expect is an extension of TCL, so you have access to all of TCL's constructs.
If I were you I would have the expect script write directly to a file.
See the section "Writing a file" here: http://wiki.tcl.tk/367. It has a simple example for just that. In your case, you will want to open the file for append (a) instead of write (w).
open command documentation at: http://www.tcl.tk/man/tcl8.6/TclCmd/open.htm
Let me know how that works for you.

Related

How to send commands stored in a file when using Expect

I'm writing a script that will eventually execute a list of commands on a switch (via SSH). These commands are stored in a file and the number of commands will vary
However, I'm not sure how this can be done using Expect. I know Expect can use a while loop, but I can't find a clear example. can someone here help?
/usr/bin/expect <<EOD
spawn ssh -o StrictHostKeyChecking=no admin#$switch
expect "*Enter password for admin\:"
send "password\r"
expect "*#"
send "????"
there should be a while loop that reads line by line from a file called "commands" that looks like this
command 1
command 2
command 3
...
Extreme Networks XOS has an XML API. You can use this for executing arbitrary commands. See the ExtremeXOS XML API Developer Guide which is listed on the support documentation page.
Managing switches by expect-scripting their CLIs is often erratic and error prone, I'd recommend that you avoid doing so if possible.

Unix-Expect:Capture the output of a command as a expect variable

I am trying to capture the output of a program in unix, and use it as a variable in the expect script. When I excecute the program, I will get something like this:
[user#svr]# ./passwdgenerator
1234*&^^[user#svr]#
As you can see, the "1234*&^^" is the password I want to capture and put it into the expect script:
set user user1
set password [open ./passwdgenerator]
spawn ssh $user#server1.com
expect "password:"
send "$password\r"
Obviously it's not working currently, just wonder how I should write this. Have tried this on the txt file before, worked. but when it is a program output, and for security reason we don't want to store password in the text file, so it doesn't work. Any expert could give me a hand? Greatly greatly appreciated.!
This line
set password [open ./passwdgenerator]
change to
set password [exec ./passwdgenerator]

copy a txt file from Unix machine to Windows machine

I have the below shell script to copy a txt file from a Unix machine to a Windows machine in the same network:
#!/bin/sh
HOST='localhost'
USER='redacted'
PASSWD='redacted'
FILE='/los_prod/scripts/log.txt'
ftp $HOST <<END_SCRIPT
user $USER
$PASSWD
put $FILE
quit
END_SCRIPT
exit 0
It's giving the error as:
:connection failed.
I am giving all the correct info in the script. What could be the reason?
You might want to have a look here:
http://www.stratigery.com/scripting.ftp.html
It gives a simple way to do what you want.
ftp doesn't accept input from stdin, so you can't just feed the login information in like that. You'd need to use a program like expect to automate the normal ftp command-line client. A better alternative may be to use another language instead of a Bourne shell script. Something like Perl (as mentioned by #sarathi), Ruby, or Python would allow you to write a fairly small script and use the FTP libraries available to them to more easily script it.
Here's a version of your script for expect:
set host "localhost"
set user "redacted"
set pass "redacted"
set file "/los_prod/scripts/log.txt"
spawn ftp $host
expect "Name"
send "$user\n"
expect "Password:"
send "$pass\n"
expect "ftp>"
send "put $file\n"
expect "ftp>"
send "quit\n"
You'll probably have to install expect on your system; it's not as widely available as other interpreters. Any modern Linux distro will have a package for it.

BASH scripting for username/password constructs

I want to write a simple bash script using ncat to open a connection to a ISP and its port.
The first command would be:
nc address port
Upon doing this, I am prompted first to provide a username. I must hit ENTER, and then I will be prompted to provide a password and then I must hit ENTER again.
After this, I want to open a Terminal process window. Can anyone point me to sufficient resources for this type of scripting?
I know the username and password already, but I'm not too sure how to work around the fact that I must provide it and then hit enter. I'm also unsure how to open a new Terminal proceses.
Thanks in advance!
Check out expect script
Expect
Example:
# Assume $remote_server, $my_user_id, $my_password, and $my_command were read in earlier
# in the script.
# Open a telnet session to a remote server, and wait for a username prompt.
spawn telnet $remote_server
expect "username:"
# Send the username, and then wait for a password prompt.
send "$my_user_id\r"
expect "password:"
# Send the password, and then wait for a shell prompt.
send "$my_password\r"
expect "%"
# Send the prebuilt command, and then wait for another shell prompt.
send "$my_command\r"
expect "%"
# Capture the results of the command into a variable. This can be displayed, or written to disk.
set results $expect_out(buffer)
# Exit the telnet session, and wait for a special end-of-file character.
send "exit\r"
expect eof
The secret lies in the HEREDOC
You can solve this problem with something akin to:
$ command-that-needs-input <<EOF
authenticate here
issue a command
issue another command
EOF
Look at the link I provided for here documents - it includes support for variable substitution and lots of other useful things. Enjoy!

Writing strings to standard input in bash script

I'm trying to automate a startup of a specific service with bash
When the service is started with init.d (/etc/init.d/openvpn.custom) it is promting for username and then password - and then it connects
The auth-user-pass from-file is not possible with the installed version, and it cannot be upgraded because of dependencies
So i'm trying to write a simple bash scripts that executes the init.d script, sleeps for a bit, inputs the username, returns, sleeping a bit, inputting the password - you'll get the flow.
like http://pastebin.com/qWHX7Di5
I've experimented with echo, but it doesent seem to work
This is for a rather legacy firewall i'm asked to keep connected.
Is this even possible?
I would use expect instead of bash. You can still call it from within bash if you need to do other tasks as well.
In expect, the script would be something like the following (untested):
#!/usr/bin/expect -f
set username "username"
set password "password"
spawn /etc/init.d/openvpn.custom start
expect "Username:"
send "$username\r"
expect "Password:"
send "$password\r"
expect eof
You'd want to change the expect "Username:" & expect "Password:" lines to match the actual login prompts that are output by your init.d script.
See the expect man page for further details.
You can try using a here-doc:
/path/to/init.d << END
$username
$password
END

Resources