I have the following program below which telnets into another device and prints serial number and Mac address.
My problem is that for some reason if I send the command once it skips the first command and sends the second, but if I copy the same command twice it will send the command.
What is the correct way to send a command multiple commands successively?
Should the buffer be flushed after every command sent ?
My Env
Eclipse Ide
Ubuntu 12.10
perl 5, version 14, subversion 2 (v5.14.2)
Snippet of my code:
$telnet = Net::Telnet->new($remoteSystem);
$| = 1;
$telnet->buffer_empty();
$telnet->buffer_empty();
$result = $telnet->input_log($errorlog);
#$_ = "#lines";
#TSN =$telnet->cmd('export | grep -e SerialNumber..[A-Z] | cut -d"\"" -f2');
#TSN =$telnet->cmd('export | grep -e SerialNumber..[A-Z] | cut -d"\"" -f2');
#mac = $telnet->cmd('ifconfig | grep eth0 | cut -d" " -f 11');
print "#TSN AND #TSN #mac";
print FH "$remoteSystem\n";
print "Telnetting into $remoteSystem .\n"; # Prints names of the tcd
close(telnet);
}
foreach (#host) {
checkStatus($_);
}
OUTPUT That skips the first command:
bash-2.02 AND bash-2.02 ifconfig | grep eth0 | cut -d" " -f 11
00:11:D9:3C:6E:02
bash-2.02 #
bash-2.02 Telnetting into debug79-109 .
OUTPUT That works but I have to send the same command twice:
export | grep -e SerialNumber..[A-Z] | cut -d"\"" -f2
AE20001901E2FD1
bash-2.02 #
bash-2.02 AND export | grep -e SerialNumber..[A-Z] | cut -d"\"" -f2
AE20001901E2FD1
bash-2.02 #
bash-2.02 ifconfig | grep eth0 | cut -d" " -f 11
00:11:D9:3C:6E:02
bash-2.02 #
bash-2.02 Telnetting into debug79-109
Specify the command prompt in your call to cmd(), e.g.#TSN =$telnet->cmd('export | grep -e SerialNumber..[A-Z] | cut -d"\"" -f2', Prompt => 'bash-2.02 #');
Try opening a connection after creating a object for the module telnet
$telnet->open($host);
After which execute waitFor method:(waits until the pattern bash-2.02 # comes)
$telnet->waitFor(/^(bash-\d+.\d+ #)$/);
and then execute your commands , it would give you proper output.
Related
I asked the dark side and here's what it printed.....
#!/bin/bash
for interface in $(ip addr show | awk '/^[0-9]+:/ {print $2}' | tr -d :)
do
if ! ip addr show $interface | awk '/inet / {print $2}' | grep -q . ; then
echo $interface
fi
done
I want to add n+ variable directly so the output will be the interfaces that is not used by the system,
Done
I have these two scripts, one for Linux and one for Mac, I hope they serve you because I have tested them on Linux Ubuntu and Mac.
on linux
#!/bin/bash
# This script will output unused interfaces on Linux
# Get list of all interfaces
interfaces=$(ifconfig -a | grep -o '^[^ ]*:' | tr -d :)
# Loop through each interface
for interface in $interfaces; do
# Check if interface is up
if [[ $(ifconfig $interface | grep -c 'UP') -eq 0 ]]; then
# Output interface name
echo "$interface is unused"
fi
done
on mac
#!/bin/bash
# Get list of all network interfaces
interfaces=$(networksetup -listallnetworkservices | tail -n +2)
# Loop through each interface
for interface in $interfaces; do
# Get the IP address of the interface
ip=$(ipconfig getifaddr "$interface")
# If the IP address is empty, the interface is unused
if [ -z "$ip" ]; then
echo "$interface is unused"
fi
done
#!/bin/bash
count=0
for interface in $(ip addr show | awk '/^[0-9]+:/ {print $2}' | tr -d :)
do
if ! ip addr show $interface | awk '/inet / {print $2}' | grep -q . ; then
free_interfaces[$count]=$interface
count=$((count + 1))
fi
done
case $count in
0)
echo "No usable interface found."
exit 1
;;
1)
DEFIF=${free_interfaces[0]}
echo "The interface $DEFIF will be used."
;;
*)
echo "Available interfaces to select: "
PS3="Press a number to select an interface to use (1-$count): "
select interface in "${free_interfaces[#]}"; do
DEFIF=$interface
break
done
echo "The interface $DEFIF will be used."
;;
esac
I want to convert this tcpdump output:
IP 10.10.10.1 > 20.20.20.1: ICMP echo request, id 8312, seq 0, length 64
IP 10.10.10.1.17441 > 20.20.20.1.22: Flags [S], seq 3936449810, win 65535, options [mss 1460,nop,wscale 6,sackOK,TS[|tcp]>
to:
IP 10.10.10.1 > 20.20.20.1: ICMP
IP 10.10.10.1.17441 > 20.20.20.1.22: tcp
I tried a lot to covert them with shell script by using the cutcommand but I can't.
Thanks all for your help.
Using awk (or the GNU gawk), setting field separator FS to ":" and assuming dump is inside test.txt:
gawk 'BEGIN{ FS=":" } { if($0 ~ / ICMP /){ print $1 ": ICMP" }else if($0 ~ /tcp[]]>/){ print $1 ": tcp" } }' test.txt
The expected result:
IP 10.10.10.1 > 20.20.20.1: ICMP
IP 10.10.10.1.17441 > 20.20.20.1.22: tcp
tcpdump output could be piped to gawk as
tcpdump <options> | gawk ' ... '
This is strictly a cut method. It is assuming your output will always be of this format. As mentioned, a sed (or awk) version would probably be more dynamic.
The main piece of this is the -d (delimiter) argument and the -f (field) argument. -f can specify a single field or range of fields that are separated by a specified delimiter (I believe tabs are default).
If your output is in a file called output.txt, you can use this little script.
line1="$(head -1 output.txt | cut -d ' ' -f1-5)"
line2="$(tail -1 output.txt | cut -d ' ' -f1-4) $(tail -1 output.txt | cut -d '|' -f2 | cut -d "]" -f1)"
echo "$line1"
echo "$line2"
If your output is stored in a variable called output, you could use this script with your
variable sent as a parameter like ./script.sh "$output"
arg="$1"
line1="$(echo "$arg" | head -1 | cut -d ' ' -f1-5)"
line2="$(echo "$arg" | tail -1 | cut -d ' ' -f1-4) $(echo "$arg" | tail -1 | cut -d '|' -f2 | cut -d "]" -f1)"
echo "$line1"
echo "$line2"
Output:
IP 10.10.10.1 > 20.20.20.1: ICMP
IP 10.10.10.1.17441 > 20.20.20.1.22: tcp
I'm having some trouble with backtics and pipe in perl. I have following code:
my #arr_lsdev = `lsdev -C | grep inet | awk '{print \$1}'` ;
print Dumper #arr_lsdev ;
But I get following error:
sh[2]: 0403-057 Syntax error : `|' is not expected
I'm guessing it has something to with my escape commands. I have tried escaping the | but it still results in the same error.
OS: AIX
Shell: KSH
Notice that the error is on line 2. You are actually executing
my #arr_lsdev = `lsdev -C | grep inet
| awk '{print \$1}'` ;
You can reduce the number of pipes:
my #arr_lsdev = map {(split ' ')[0]} grep {/inet/} `lsdev -C`;
The title says it all. I've seen this idiom used alot instead of adding an additional grep -v grep in some ps pipeline. For example it could be used like this:
$ ps aux | grep '[f]irefox' | awk '{ print $8 }'
instead of
$ ps aux | grep 'firefox' | grep -v grep | awk '{ print $8 }'
It's super-convenient, but how does it work and why?
The pattern [f]irefox will not match the literal string [f]irefox. Instead it will match strings with exactly one char from the 1-character class [f], followed by irefox.
hi
I wrote a perl script where I stored columns from a text file filled with ip and port scans into variables. The variables contain many ip addreses, ports, protocols, states, and services now I need a while loop that takes all the ip's stored in the ip variable and matches them with its corresponding ports protocols and state etc.side by side look so:
192.168.3 45 tcp open smtp
heres my code
$ip_address = `cat /cygdrive/c/Windows/System32/test11.txt |
grep 'Nmap scan report for'`;
$state = `cat /cygdrive/c/Windows/System32/test11.txt | grep -v 'PORT'|
grep -v 'filtered'| grep -v 'latency'| grep -v 'Nmap' | grep -v 'Discovered' |
grep -v 'Raw' | grep -v 'SYN' | grep -v 'DNS'| grep -v 'Ping' |
grep -v 'Scanning' `;
$port = `cat /cygdrive/c/Windows/System32/test11.txt | grep -v 'Discovered'|
grep -v 'Nmap' | grep -v 'PORT' | grep -v 'ports'| grep -v 'Read' |
grep -v 'Raw'| grep -v 'Completed'| grep -v 'DNS' | grep -v 'hosts' |
grep -v 'Ping' | grep -v 'SYN' | grep -v 'latency' `;
$protocol = `cat /cygdrive/c/Windows/System32/test11.txt | grep -v 'Discovered'|
grep -v 'Nmap' | grep -v 'PORT' | grep -v 'ports'| grep -v 'Read' |
grep -v 'Raw' | grep -v 'Completed'| grep -v 'DNS' | grep -v 'hosts' |
grep -v 'Ping' | grep -v 'SYN' | grep -v 'latency' `;
{
$service = `cat /cygdrive/c/Windows/System32/test11.txt | grep -v 'Nmap' |
grep-v 'Host' | grep -v 'filtered' | grep -v 'PORT' | grep -v 'Raw'|
grep -v 'Scanning'| grep -v 'Completed'| grep -v 'Ping' |grep -v 'DNS' |
grep -v 'Discovered'| grep -v 'SYN'`;
while($ip_address, $port, $protocol, $state, #service)
{
chomp ($ip_address, $port, $protocol, $state, #service);
print "$ip_address, $port, $protocol, $state, #service";
exit 0;
}
Usually I say to use the tools that you understand and that it's OK to do things like call awk from Perl. But for this code I'll make an exception. You should be using the builtin Perl commands for this task. Namely, arrays and Perl's grep operator. Here's how I would start to rewrite this.
# do this once instead of `cat ...` several times.
open my $fh, '<', '/cygdrive/c/Windows/System32/test11.txt';
my #the_input = <$fh>;
close $fh;
# do this instead of `| grep -v ... | grep -v ...`
my #ip_addresses = grep { /Nmap scan report for/ } #the_input;
my #states = grep {
!/PORT|filtered|latency|Nmap|Discovered|Raw|SYN|DNS|Ping|Scanning/
} #the_input;
my #ports = grep {
!/Discovered|Nmap|PORT|ports|Read|Raw|Completed|DNS|hosts|Ping|SYN|latency/
} #the_input;
my #protocols = grep {
!/Discovered|Nmap|PORT|ports|Read|Raw|Completed|DNS|hosts|Ping|SYN|latency/
} #the_input;
my #services = grep {
!/Nmap|Host|filtered|PORT|Raw|Scanning|Completed|Ping|DNS|Discovered|SYN/
} #the_input;
I usually try to do these sorts of things in one pass, like...
#!/usr/bin/perl
open(F, "/cygdrive/c/Windows/System32/test11.txt");
while(<F>) {
# If the current line has something that matches an IP
# address, store the matched pattern in $ip. We'll
# use this as we process the remaining lines.
#
$ip = $1 if ( /Nmap scan report for (\d+\.\d+\.\d+\.\d+)/ );
# Try to match lines like "ddd/www www www wwww"
#
( $port, $protocol, $state, $service) = ( m|(\d+)/(\w+)\s+(\w+)\s+(\w+)| );
print "$ip, $port, $protocol, $state, $service\n" if $port;
}