Cut Help using Solaris 10
How can use the cut command to cut the last two octets or hostname from a string like for example:
PHP Code:
PHP Code:
|
i dunno' if there is a simpler way but this works:
Code:
echo S00CBBF.aa.bb.ccc.edu | rev | cut -d . -f 3- | rev |
Why not SED. This is crude, but works:
sed 's/\....\....$//' filename Translation: find a literal ".", any 3 characters, another literal ".", 3 more characters, and then the end of the line. Replace by an empty string. |
I am using Sun Solaris 10. Is that command 'rev' still valid?
|
Quote:
Code:
Sun Microsystems Inc. SunOS 5.8 Generic Patch February 2004 Code:
schneidz@lq:/temp> cat ssh-rev.c |
awk for the win.
Code:
awk -F'.' '{for (i=1;i<=NF-2;i++) {if (i<NF-2) printf $i"."; else print $i}}' |
echo S00CBBF.aa.bb.ccc.edu |cut -d'.' -f1-3
gives S00CBBF.aa.bb too easy ;) and shorter / clearer than awk |
Quote:
|
Quote:
|
No joy, I'm afraid:
Code:
[0 dave@cronus ~]$ i="S00CBBF.aa.bb.ccc.edu"; i=(${i//./ }); echo "${i[0]}.${i[1]}" awk's still in front. Go awk! etc. Anyone got any perl? Dave |
Code:
# echo S00CBBF.aa.bb.ccc.edu | nawk 'gsub(/\.[a-zA-Z0-9]+\.[a-zA-Z0-9]+$/,"")' |
heh. way too much work: i="S00CBBF.aa.bb.ccc.edu"; i=(${i//./ }); n=${#i[@]}; unset i[$[$n-1]] i[$[$n-2]]; i=${i[*]}; echo ${i// /.}
|
Well, given the OP's example input, 'removing the last 2 fields' and 'keeping the first 3' both give his example answer. The qn is, what does he REALLY need??
|
Everyone please read my original post so that all of you can understand my situation.
http://www.linuxquestions.org/questi...-shell-604725/ The output will always be in this format: PHP Code:
PHP Code:
PHP Code:
PHP Code:
|
So, basically, you need a function which will remove the last two 'between-dots' fields from your hostname string.
In that case, Code:
awk -F'.' '{for (i=1;i<=NF-2;i++) {if (i<NF-2) printf $i"."; else print $i}}' Code:
[0 dave@cronus ~]$ echo S00F3456.aa.bb.ccc.edu | awk -F'.' '{for (i=1;i<=NF-2;i++) {if (i<NF-2) printf $i"."; else print $i}}' |
I will give it a shot. thanks
|
That work fine for one string but this is a standard nslookup for our domain:
PHP Code:
PHP Code:
thanks |
Quick and dirty:
Code:
nslookup S00CBBF.aa.bb.ccc.edu | grep '^Name:' | awk '{print $2}' | awk -F'.' '{for (i=1;i<=NF-2;i++) {if (i<NF-2) printf $i"."; else print $i}}' |
ilikejam, you are the biggest help but please forgive me for my extremly limited programming knowledge(I promise will study up on this stuff) but I tried to incorporate this into my little program and I cant seem to get the appropiate output, check it out!
PHP Code:
PHP Code:
I know that problem is here: PHP Code:
PHP Code:
help |
Code:
nslookup S00CBBF.aa.bb.ccc.edu | nawk 'BEGIN{FS=":"} |
that is another way of doing it but I still have problems with the variables in my script and I think that it is the :
PHP Code:
|
Easy now.
I think you've just mixed up your variables. Try the following: Code:
#!/bin/ksh Edit: I'm assuming the snifflist is a list of fully qualified hostnames, not IP addresses. |
I freakin love you man! next pay check it is done. Here is my finish product with the help of so many great individuals just to name a few(ilikejam,jiliagre,chrism01,acidkeypie,pixellany) keep up the great work.
PHP Code:
|
By the way, that script still won't work - the ksh /dev/tcp/<IP>/<port> trick only works with numerical IP addresses, not hostnames, so you'll have to have
Code:
IP_Addr="$(nslookup $SNIFFERS | tail -3 | grep '^Address:' | awk '{print $2}')" Code:
read foo < /dev/tcp/$SNIFFERS/23 2>/dev/null && Telnet=open |
Do not ask me why but the scipts works. I am testing it now and it validity. I will get back to you shortly!
|
I'm betting one of two things will happen - either the script will always show the telnet connection as closed, or it won't return.
Dave |
It seems to be working, try it out.
|
Your telnet server(s) must print a line before the login prompt for 'read' to return. None of our do, so read just sits there waiting for a newline.
But hey, whatever works... Dave |
You are right. when it finds an open telnet session/port 23 it hangs. what to do!
|
To be honest, I'd use nmap (packages are available at sunfreeware) - it was designed to do this sort of thing.
This is the script (well, the important bit anyway) I run from cron at 6 every morning - the output is sent to my personal email to give me an early warning in case a machine has fallen over during the night. Code:
for go in `cat $LIST` If nmap's not an option, you could do something funky with a sleep->kill after the read in your script to see if the read process is waiting, and take that as a 'yes, telnet's running', but that's a disgusting hack and I feel dirty for even having thought of it. Dave |
I work in a very stick environment and they have refused me before. Is there a way that we can throw in a ^c to cancel the login the telnet session and add that to the script?
|
It's pretty horrible, but here goes. There's probably a better way to do this, so feel free to jump in, anyone.
Code:
read foo < /dev/tcp/$SNIFFERS/23 2>/dev/null & There's an obvious problem with this, though. If network congestion gets bad, you may get false positives, as the read may be waiting for the network, rather than waiting with an open telnet connection. It's also slow - it'll take an extra 2 seconds per host, if the host has telnet running. Use nmap if at all possible. Dave |
I had to add a sleep 10 second for it work on my script. That is ok but is there a way to make this more efficient?
|
I retried the script and it was giving false positives. help!
|
I don't think you're going to get round this without some software. Could you see if you can install 'Netcat' or 'nmap'? They might be willing to let Netcat on if they don't like the sound of nmap.
Or maybe you could sneak a Netcat binary into your ~/bin directory? It's only 26kB - they'll never notice ;) (Usual disclaimer: If the admin finds out, and beats you to death with a copy of 'The Practice of System and Network Administration (Limoncelli, Hogan 2001)', then we never had this conversation.) Dave |
@OP, if your project is legit, there's no reason why admin won't install these tools for your usage. After all, it should be approved by your management. if its legit, it may be worth a try doing some Perl/python socket programming.
|
I asked another admin and he said that will not let me use nmap or netcat. I will ask my management and see if I can use those utility if it will make my life easier. With that being said I will ask them on Monday. I will get back to you and once again all of you deserve all the credit. Many thanks.
|
hopefully management will see the diff between adding authorised tools when you need them and unnecessary paranoia...
Otherwise you might be better of going with eg Perl, which has socket modules as part of the core install. Use a forking soln? |
I well remember that my SysAdmins were very against the development dept using top on SunOS/Solaris in the early 1990's as it was downloaded and compiled and not in the distibution as originally loaded. Six months later after much arguing with management it was accepted as being the standard tool that it has become and all the fuss forgotten. God forbid, the SysAdmins discovered SAR for performance monitoring which was a bit like using a drogue chute to monitor air speed.
Put your case together and present it after doing your homework to explain away any objections from the SysAdmins, who may be working in a timewarped environment from yesterday. It doesn't stop them being good lads, but they often just have a different set of imperitives to those of the developers. |
some environment set policies that will not allow these tools, like nmap/netcat as they are powerful tools, often used for malicious purposes. So that's a valid "reason" for sysadmins/mgmt not wanting to install such tools.
@OP. see here for other alternative. Or you can start learning some socket programming. |
it looks like sleep 15 seconds (though very slow) is producing reliable results or is this script truly not reliable? Any feedback
|
If I use perl, will I still have the same issue using that as oppossed to ksh?
PHP Code:
|
Works for me :)
|
Can you give me an example of how I would plug
PHP Code:
PHP Code:
|
Just replace
Code:
read foo < /dev/tcp/$SNIFFERS/23 2>/dev/null && Telnet=open Code:
Telnet=$(perl -e 'use IO::Socket; print new IO::Socket::INET (PeerAddr => "'$SNIFFERS'", PeerPort => "23", ) ? "open\n" : "closed\n";') |
Add
Timeout => 5 option to the new() method to make it give up trying after 5 seconds |
Its that easy?
|
Yes. Yes it is.
|
right on! I will test it out and give you an update!
|
Of course, it's Perl ;)
|
All times are GMT -5. The time now is 08:15 AM. |