grep Ipaddress in expect script - expect

I want to grep ipaddress from file and set it to variable in expect scritp
send -- "cat $filename |grep ([0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\})\r"
expect -re "([0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}) *$prompt$"
set IP $expect_out(1,string)
but I'm not suceeding. Any idea? I'm using /usr/local/bin/expect. Thanks

From a quick scan, it seems to me you're trying to match a double prompt in your statement. You seem to have a variable followed by a literal prompt at the end of your expect regexp. Are you sure that is what you want?
Aside from that, I strongly recommend that you put expect_internal 1 somewhere at the top of your script, and that way expect will log output showing you what it is (and is not) matching. You really shouldn't be trying to debug regexp matching without it, it's like searching in the dark...

Related

Capturing output of an expect session with a bash script

I have a bash script that is using xmlstarlet to manipulate some key/value pairs in an application configuration file to prepare the file to be moved to a new production host. The values that need changed are host/encryption specific.
In order to discover one of the new values I need to interact with a vendor provided script in an expect session and capture the output into a variable in the bash script so I can continue to use it.
The expect part of the bash script looks something like this:
expect <<DONE
spawn command_provided_by_vendor
expect :
send -- "newvalue\r"
DONE
This is where I get stuck
In a shell the output of this command looks like:
Encrypted value (case sensitive, please cut and paste): 2qIrRvcSoHMb55dpcef6vw==
What I need to do is capture the non-whitespace output after the ":" and nothing I've tried works due to regexp errors, the parenthesis in the prompt string, etc.
There are other questions on stackoverflow that are similar, but I failed to understand how those answers helped my problem.
Any help, pointers appreciated.
I would use the expect command to look for an appropriate regular expression and capture the value there:
value=$(
expect <<DONE
spawn command_provided_by_vendor
expect :
send -- "newvalue\r"
expect -re {Encrypted value.*: (\S+)}
puts $expect_out(1,string)
expect eof
DONE
)

Way to set date automatically from command line

I make a script in a command line, but I have a problem. I need change date automatically from a command line. Is it possible?
I know, I can use static date for example:
date 0101122315
but I need an automatic date from the internet in macOs.
You've probably got some research to do, here is one possible outline:
Use curl to have the date from the internet and store in a file or send to standard output - in the latter case you may pipe into more commands or store in a shell variable.
If needed: Use sed, awk, grep, some JSON editor, etc. to extract the date from the result of (1). Not needed if your URL just returns the date and nothing else.
Use date and shell variable or command output substitution to pass your date as an argument.
You can find documentation on all the commands, and the shell, using the man command, e.g. man curl.
You might end up with a short one line script (something like:
date `curl URL | sed command`
) or something longer. Nobody can really tell you as it is unstated in the question what the URL will return.
If you get stuck once you're further along ask a new question showing what you've developed and explains the error and someone will undoubtedly help you progress.
Have fun!
Try this command
sudo sntp -sS time.apple.com

Return results for Ubuntu Updates in a Variable

I am trying to get the result from /usr/lib/update-notifier/apt-check on a Ubuntu 16 Server into a Array to make a XML response for a monitoring tool, but somehow the value of this apt-check just refuses to get in my Variable. For simplicity sake, I have omitted the XML creation part.
#!/bin/bash
APTCHECK="/usr/lib/update-notifier/apt-check"
APTResult="$(${APTCHECK})"
echo "Result is $APTResult"
exit 0
if you now run this code with bash -x you will see that the result is returned to the Terminal, but not assigned to the Variable. If I substitute the "command" to something simple like "ls -lah" everything works fine.
I just don't know why this is not working ? Anybody ?
apt-check prints to the stderr, so you need to capture that instead with aptresult=$(/usr/lib/update-notifier/apt-check 2>&1).
The other option is with the --human-readable switch, which'll print to the stdout. The only problem then is that you have to parse the text output (unless the text output is what you actually want).

AIX text formatting

AIX Version 6.1
I'm trying to write a script to pull times out of a program to send to Zabbix, but I'm wanting to modify the formatting of the times returned.
At present, when I pull the time it returns like so: [15:48:30]
My goal is to remove the brackets ([]) to then be able to pull the time apart with awk to do calculations on the time to render it down into seconds and draw relevant information from that.
AIX is continually giving me errors with every form of text formatting I can think of/find.
Ex: echo $unformattedtime | awk '{print substr($0,1,8)}'
gives me permissions errors when I've already chmod 777 the script. I've seen fixes that require going in and making root changes, and while that's possible for me to do, the script needs to run as a non-root user for what it's being designed to do.
Failing manipulating the unformattedtime variable, I tried putting it into a text file and manipulating it with tr.
Ex: tr -s [] '' < timet.txt > timetformat.txt
Where timet.txt simply had the '[15:48:30]' put in using vi. This simply returned a "tr:Error 0"
Is there some sort of AIX specific method of doing modifications that I'm missing? Or just anything that would accomplish the goal here?
Thanks!
Answer from my comments:
tr -s is going to leave single square brackets unmolested, this isn't what you want. (Though I don't understand that error unless it is a quoting issue on [] in the shell. Try '[]' instead.)
tr -d '[]' is probably more what you want there.
It might also be possible, depending on the shell, to avoid the temporary files by using a HEREDOC:
tr -d '[]' <<EOF
echo "$unformattedtime"
EOF
There can be no leading spaces before the final EOF there for the record.

Script awk v. sed for modifying ipsec.conf

I am creating a script that I can run and it will simply ask me the common location name...i.e SEC-DF1 and it will fetch the ip of that site from within script. My problem is taking that IP and replacing
right=IP_ADDRESS
with
right=NEW_IP_ADDRESS
I need this so I can call the script as I will be changing the value of right so often for testing.
I have been messing with sed until someone mentioned awk...this stuff has such horrid documentation I keep getting all types errors or weird results on the test file I am messing with.
Since this is a straight forward substitution, I would just use sed:
sed -e 's/^right=[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/right=192.168.1.92/' filename
This will match right= at the beginning of a line followed by an IP address and replace it with the IP of your choosing.
This command will modify your script:
NEW_IP_ADDRESS=101.102.103.104 sed -i "s/^(right=).*$/\\1$NEW_IP_ADDRESS/" script

Resources