cat hosts.txt | while read h; do telnet $h; done
When I run this, it telnets to first host which denies the connection but then exits instead of looping over the other hosts in the file.
Why is it exiting the loop after the first host and how can I fix it?
Thanks.
That works just fine in my bash:
$ cat hosts.txt
machine1
machine3
$ cat hosts.txt | while read h; do telnet $h; done
Trying 9.190.123.47...
telnet: Unable to connect to remote host: Connection refused
Trying 9.190.123.61...
telnet: Unable to connect to remote host: Connection refused
However, when I connect to a machine that doesn't refuse the connection, I get:
$ cat hosts.txt
machine2
machine1
machine3
$ cat hosts.txt | while read h; do telnet $h; done
Trying 9.190.123.48...
Connected to machine2.
Escape character is '^]'.
Connection closed by foreign host.
That's because I'm actually connecting successfully and all the other host names are being sent to the telnet session. These are no doubt being used to attempt a login, the remaining host names are invalid as user/password and the session is being closed because of that.
If you just want to log in interactively to each system, you can use:
for h in $(cat hosts.txt); do telnet $h 1023; done
which will not capture the rest of the host names into the first successful session.
If you want to truly automate a telnet session, you should look into a tool such as expect or using one of the remoting UNIX tools such as rsh.
telnet is interactive. what are you actually wanting to do? If you want to automate "telnet" session, you can use SSH
while read -r host
do
ssh <options> <commands> "$host"
done <"hosts.txt"
As mentioned previously telnet is an interactive program that expects input.
At a guess all hosts after the first are consumed as input by the first telnet.
It is not clear what your script is trying to do. Perhaps you need to be clearer on what you are trying to do.
Related
I have a script which telnet to remote system & user can interact with remote system. But i want to hide telnet connection logs from getting printed for security reasons. I tried all the redirection techniques like (> , 1>, 2>), but my purpose is not served. "1>" is not allowing to interact with remote system.
How to redirect/hide only telnet connection logs (or first 3 connection lines) below & make telnet session interactive ?
script :
#!/bin/bash
telnet 1.2.3.4 7777
sample issue execution :
~/redirect.sh
Trying 1.2.3.4... // redirect
Connected to 1.2.3.4.
Escape character is '^]'.
login:
sample expected execution :
~/redirect.sh
login:
There is no easy fix for this, as those three lines are simply printf() in the code. It would be a great deal of effort to remove those lines and allow interactive connections.
However, it is a simple client side change to modify the telnet client source and recompiling:
Download inetutils-2.3 from here.
Extract with tar -xJvf inetutils-2.3.tar.xz.
cd inetutils-2.3.
./configure
Use the patch in this answer: patch telnet/commands.c < /path/to/telnet.patch
patching file telnet/commands.c
make
Then test:
2>/dev/null ./telnet/telnet 192.168.100.1 22
SSH-2.0-OpenSSH_8.6
Copy this version of telnet into your PATH somewhere. Possibly rename it stelnet.
I'm fairly new to openssh, i am writing a framework to automate a couple of tasks.
I understand that its easy to send commands over ssh to a remote vm to do some things. But i want to send some commands to a CLI-like service that's running inside my remote vm.
To explain it better:
[c:\~]$ ssh root#x.x.x.x
Connecting to x.x.x.x:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.
[root#vm1 ~]# ssh admin#0
admin#0's password:
admin connected from 127.0.0.1 using ssh on vm1
Your last successful login was at 2021-2-1 19:25:27
Your last successful login was from 127.0.0.1
admin#vm1>
This is what i'm doing,
a)I log into my remote machine via ssh.
b)Then i log into the CLI-like service that's running inside my vm.
So what i want to achieve is, i want to run some commands on the CLI on my vm. My perl script presently looks like this. (Note: My perl script is running in a different vm altogether)
#! /usr/bin/perl
use strict;
use warnings;
use Net::OpenSSH;
use IO::Pty;
print "New connection\n";
my $ssh = Net::OpenSSH->new("x.x.x.x",user=>'admin', password=>'password', port=>22, timeout=>30);
$ssh->error and die "Could not connect\n". $ssh->error;
my $cmd = $ssh->pipe_out("show alarm");
while(<$cmd>){
print "$_";
}
close $cmd;
undef $ssh;
This is the o/p that i see:
root#debian:~# perl automate.pl
New connection
Your last successful login was at 2021-2-1 19:40:20
Your last successful login was from x.x.x.x
^C
root#debian:~#
The o/p i'm expecting is for the command "show alarm" but looks like the command is not reaching the CLI of my target machine.
It would be of great help if can get some guidance from y'all.
Thanks.
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.
I'm trying to get the list of processes running on my Windows machine from Linux, but I don't get any output when I do it in a script. If I use telnet manually and use the command pslist I get the complete list of processes, but not in my script.
Here is the bash script (minus the variables):
( echo open ${host}
sleep 1
echo ${user}
sleep 3
echo ${pass}
sleep 1
echo pslist
sleep 2
) | telnet
and I simply call it with bash pslist.sh and the output is something like that:
telnet> Trying ip_address...
Connected to ip_address.
Escape character is '^]'.
Welcome to Microsoft Telnet Service
login: my_loginmy_passwordpslistConnection closed by foreign host.
What am I doing wrong ?
telnet is notoriously tricky to script. You may be able to succeed more often if you add a longer still sleep between the commands.
A better approach is to switch to a properly scriptable client, viz. netcat (aka nc). Better still would be to install an SSH server on your Windows box (perhaps for security only make it accessible from inside your network) and set it up with passwordless authentication. Then you can simply ssh user#ipaddress pslist
Terminate each echo with \r character, like this: echo -e "${user}\r"
I have to access my server in such way: localhost -> remote1 -> remote2 (my server)
[xxxx#localhost] $ ssh yyyy#remote1
[yyyy#remote1] $ ssh zzzz#remote2
[zzzz#remote2] $ echo "now I logined into my server..."
I know how to transfer files with scp. however I have no read or write permissions on remote1. How can I transfer a file to remote2?
Another alternative could be to use a Proxy command:
scp -o ProxyCommand='ssh yyy#remote1 netcat %h %p 2> /dev/null' zzz#remote2:fromfile tofile
if remote1 has netcat installed. Other viable options could be nc or socat (the latter has a different syntax).
Try this,
ssh -L localhost:8022:remote2:22 remote1
Now, you can use localhost port 8022 to contact 22 of remote2 via remote1. This session session should be active whenever you need to transfer. Use
scp -P 8022 /path/locale/file 127.0.0.1:/path/on/remote2
This is commonly called as SSH Tunneling. You can search and get to know lot about it.