Ruby and telnet: waitfor doesnt work - ruby

I need to login in a server with Ruby and Telnet and execute a few commands. My actual script is:
tn = Net::Telnet::new("Host" => "#{ip}", "Port" => 23, "Timeout" => 60,
"Output_log"=>"output_log.log",
"Dump_log"=> "dump_log.log",
"Prompt" => /[#]/ )
tn.cmd("#{USER}\n#{PASS}") { |c| print c }
puts tn.cmd("Conf")
tn.waitfor(/config/) { |str| puts str }
puts tn.cmd("Int fa23")
puts tn.cmd("Shut")
puts tn.cmd("No shut")
puts tn.cmd("Exit")
tn.close
I must only execute the second command (Int fa23) after the string "config" is found at the output. The problem is that waitfor is not working. Here is the output_log:
Trying XX.XX.XX.XX...
Connected to XX.XX.XX.XX.
User Name:username
Password:*************
BOT-SWT-VSAT-AL-...#Conf
BOT-SWT-VSAT-AL-...(config)#
The script stops with waitfor time out error. What am I doing wrong?

Please add the waitfor right after Net::Telnet::new
You should wait for the connection to get established (which happens when creating a Telnet instance) and then wait for the server to respond back every time before sending the next command.
localhost = Net::Telnet::new("Host" => "*****",
"Port" => ***,
"Timeout" => 10,
"Prompt" => /[$%#>] \z/n)
localhost.waitfor(/USER/) {
localhost.cmd("****") {
localhost.waitfor(/PASS/) {
|c| print c
# your next commands
...
# localhost.close
}
}
}
The key here is to make sure that you have received all the packets from the server (until EOF is received) before responding back. Then sometimes, even consuming/waiting for a space character might count (depending to how your the telnet server is designed).
Then, make sure to set the right regular expression for your match.

Related

Ruby spawn an object method

I'm trying to figure out a way to track the status of a process that I've created. In my script I start out by creating my object:
ov = OpenVASOMP::OpenVASOMP.new({"host" => "localhost", "port" => "9390", "user" => "admin", "password" => "#{ENV["OV"]}"})
Which creates an ov object and exposes a bunch of other methods. In particular: ov.task_start.
I need to be able to track the process and perform other actions while it's running, such as sending a status update to a remote server.
My initial thought was to wrap this in a Process.spawn and track the PID, but that's throwing an error:
TypeError: no implicit conversion of REXML::Element into String
and the stack trace points to this line: pid = Process.spawn(ov.task_start(taskid))
So, I guess you can't pass objects and their methods into spawn?
Here's my whole block of code in case there is something else that I'm missing:
ov = OpenVASOMP::OpenVASOMP.new({"host" => "localhost", "port" => "9390", "user" => "admin", "password" => "#{ENV["OV"]}"})
taskid = ov.task_create({"name" => timestamp, "target" => target, "config" => config})
running = true
pid = Process.spawn(ov.task_start(taskid))
Signal.trap("HUP") { log("#{results_dir}/events.log", "[!] Stop triggered by user!"); exit }
until running == false
begin
running = Process.getpgid(pid)
log("#{results_dir}/events.log", "Scan PID: #{pid}")
stat = ov.task_get_byid(taskid)
update_ov_status(stat['progress'])
log("#{results_dir}/events.log", "[+] Sending progress to server: #{stat['progress']}%")
scan_status = get_scan_status
if scan_status == "Stopped"
ov.task_stop(taskid)
ov.task_delete(taskid)
ov.target_delete(target)
Process.kill("HUP", pid)
Process.wait
update_task_id("")
update_ov_status(0)
update_scan_status("Idle")
end
sleep 60
rescue Errno::ESRCH
running = false
puts "PID: #{pid} done!"
log("#{results_dir}/events.log", "[!] Scan complete")
end
end
And task_start looks like:
def task_start (task_id)
xmlreq=xml_attr("start_task",{"task_id" => task_id}).to_s()
begin
xr=omp_request_xml(xmlreq)
rescue
raise OMPResponseError
end
return xr
end
Am I going about this all wrong?
Just repeating what I said in the comment in an answer, for closure.
since task_start is not a shell script string, but rather a block of code that should be executed asynchronously, use Process.fork { ov.task_start taskid } instead of Process.spawn.
The Process.fork call returns a PID which can be used to stop the process, for example:
# in one terminal
ruby -e "puts Process.fork { loop { puts('tick'); sleep 1 } }"
# it then prints a PID like 20626
# then in another terminal:
kill -9 20626
# the "tick" will stop getting printed every second.

Ruby Net::Telnet waitfor() doesnt work

i have check out many different ways to fix this and it might be specific to the enviroment.
i'm creating a script in Ruby to telnet into a remote server, login, enter the shell type (its a prompt after logging in and has no "prompt" in front) and then entering a userid at the next prompt. currently it gets logged in via user and pass, but freezes at the term part.
i have tried to use regex, strings, sleep and then just puts("xterm" but nothing allows it to go past this prompt.
here is a snippet of code that is getting hung up and the output to go along:
$telnet = Net::Telnet::new("Host" => 'hostname',
"Output_log" => 'output.txt',
"Dump_log" => 'Dump.txt',
"Binmode" => false,
"Timeout" => false,
"Prompt" => /.* $ $/) { |c| print c }
$telnet.login("user", "pass") { |c| print c }
$telnet.waitfor("TERM = (xterm) ")
$telnet.puts("xterm") { |c| print c }
$telnet.waitfor(Match => /Enter\s*$/) { |c| print c }
$telnet.puts("userid")
the output is as follows:
HP-UX ds107492 B.11.11 U 9000/800 (tm)
login: user
Password:
Please wait...checking for disk quotas
. i
.[ci
.*s1^i
See /etc/copyright for copyright notices
You have mail.
'You have mail.'
TERM = (xterm)
$s.waitfor("Match" => /TERM\s*/){ |c| print c }
$s.print("xterm\n"){ |c| print c }
seems to be the answer, it allowed me to go past the prompt. still not sure what the difference between this regex and all the others i tried are :/

How to properly implement Net::SSH port forwards

I have been trying to get port forwarding to work correctly with Net::SSH. From what I understand I need to fork out the Net::SSH session if I want to be able to use it from the same Ruby program so that the event handling loop can actually process packets being sent through the connection. However, this results in the ugliness you can see in the following:
#!/usr/bin/env ruby -w
require 'net/ssh'
require 'httparty'
require 'socket'
include Process
log = Logger.new(STDOUT)
log.level = Logger::DEBUG
local_port = 2006
child_socket, parent_socket = Socket.pair(:UNIX, :DGRAM, 0)
maxlen = 1000
hostname = "www.example.com"
pid = fork do
parent_socket.close
Net::SSH.start("hostname", "username") do |session|
session.logger = log
session.logger.sev_threshold=Logger::Severity::DEBUG
session.forward.local(local_port, hostname, 80)
child_socket.send("ready", 0)
pidi = fork do
msg = child_socket.recv(maxlen)
puts "Message from parent was: #{msg}"
exit
end
session.loop do
status = waitpid(pidi, Process::WNOHANG)
puts "Status: #{status.inspect}"
status.nil?
end
end
end
child_socket.close
puts "Message from child: #{parent_socket.recv(maxlen)}"
resp = HTTParty.post("http://localhost:#{local_port}/", :headers => { "Host" => hostname } )
# the write cannot be the last statement, otherwise the child pid could end up
# not receiving it
parent_socket.write("done")
puts resp.inspect
Can anybody show me a more elegant/better working solution to this?
I spend a lot of time trying to figure out how to correctly implement port forwarding, then I took inspiration from net/ssh/gateway library. I needed a robust solution that works after various possible connection errors. This is what I'm using now, hope it helps:
require 'net/ssh'
ssh_options = ['host', 'login', :password => 'password']
tunnel_port = 2222
begin
run_tunnel_thread = true
tunnel_mutex = Mutex.new
ssh = Net::SSH.start *ssh_options
tunnel_thread = Thread.new do
begin
while run_tunnel_thread do
tunnel_mutex.synchronize { ssh.process 0.01 }
Thread.pass
end
rescue => exc
puts "tunnel thread error: #{exc.message}"
end
end
tunnel_mutex.synchronize do
ssh.forward.local tunnel_port, 'tunnel_host', 22
end
begin
ssh_tunnel = Net::SSH.start 'localhost', 'tunnel_login', :password => 'tunnel_password', :port => tunnel_port
puts ssh_tunnel.exec! 'date'
rescue => exc
puts "tunnel connection error: #{exc.message}"
ensure
ssh_tunnel.close if ssh_tunnel
end
tunnel_mutex.synchronize do
ssh.forward.cancel_local tunnel_port
end
rescue => exc
puts "tunnel error: #{exc.message}"
ensure
run_tunnel_thread = false
tunnel_thread.join if tunnel_thread
ssh.close if ssh
end
That's just how SSH in general is. If you're offended by how ugly it looks, you should probably wrap up that functionality into a port forwarding class of some sort so that the exposed part is a lot more succinct. An interface like this, perhaps:
forwarder = PortForwarder.new(8080, 'remote.host', 80)
So I have found a slightly better implementation. It only requires a single fork but still uses a socket for the communication. It uses IO#read_nonblock for checking if a message is ready. If there isn't one, the method throws an exception, in which case the block continues to return true and the SSH session keeps serving requests. Once the parent is done with the connection it sends a message, which causes child_socket.read_nonblock(maxlen).nil? to return false, making the loop exit and therefore shutting down the SSH connection.
I feel a little better about this, so between that and #tadman's suggestion to wrap it in a port forwarding class I think it's about as good as it'll get. However, any further suggestions for improving this are most welcome.
#!/usr/bin/env ruby -w
require 'net/ssh'
require 'httparty'
require 'socket'
log = Logger.new(STDOUT)
log.level = Logger::DEBUG
local_port = 2006
child_socket, parent_socket = Socket.pair(:UNIX, :DGRAM, 0)
maxlen = 1000
hostname = "www.example.com"
pid = fork do
parent_socket.close
Net::SSH.start("ssh-tunnel-hostname", "username") do |session|
session.logger = log
session.logger.sev_threshold=Logger::Severity::DEBUG
session.forward.local(local_port, hostname, 80)
child_socket.send("ready", 0)
session.loop { child_socket.read_nonblock(maxlen).nil? rescue true }
end
end
child_socket.close
puts "Message from child: #{parent_socket.recv(maxlen)}"
resp = HTTParty.post("http://localhost:#{local_port}/", :headers => { "Host" => hostname } )
# the write cannot be the last statement, otherwise the child pid could end up
# not receiving it
parent_socket.write("done")
puts resp.inspect

How can I only read one line of data from a TCPSocket in Ruby?

I'm using the following code to connect to a network service i'm writing (thats backed by EventMachine) and I'm having a bit of trouble getting into a situation allowing me to use one socket connection to execute multiple commands.
#!/usr/bin/env ruby
require 'socket'
opts = {
:address => "0.0.0.0",
:port => 2478
}
connection = TCPSocket.open opts[:address], opts[:port]
# Get ID
connection.print "ID something"
puts connection.read
# Status
connection.print "STATUS"
puts connection.read
# Close the connection
connection.close
Here's what my EventMachine server hander looks like...
module ConnectionHandler
def receive_data data
send_data "Some output #{data}"
end
end
However, my first ruby script hangs when it executes connection.read as I presume its waiting for the connection to close so it knows its got all of the data? This is not what I want to happen.
My socket server will just take one command (on one line) and return one line of output.
Any ideas how I can do this? Thanks.
It turns out the connection.gets method will return a line of data received if the server sends a response ending in a \n character. So I just added \n to the end of my send_data call and switch to using puts connection.gets and it worked great!

Problem in using NET/TELNET class in ruby

Hii all,
I am telneting a machine using "net/telnet" class that comes in ruby but am having some starnge problem...This is below code i have
require 'net/telnet'
ip="192.168.247.111"
localhost = Net::Telnet::new("Host" =>ip,
"Timeout" => 50,
"Prompt" => /[$%#>] \z/n)
localhost.login("root", "root") { |c| print c }
Now when i run the above code i got en error like
c:/ruby/lib/ruby/1.8/net/telnet.rb:352:in `initialize': getaddrinfo: no address
associated with hostname. (SocketError)
But if i harcoded th Ip address like "Host"=> "192.168.247.111"
am able to make it work means able to telnet to machine...but my requirement it to assign from some variable ....How could i achieve it??
Try this:
require 'net/telnet'
ip="192.168.1.5"
localhost = Net::Telnet::new("Host" => "#{ip}", "Timeout" => 50, "Prompt" => /[$%#>] \z/n)
localhost.login("ziad", "ziad") { |c| print c }

Resources