Get Client Machine Name in Ruby on Rails - ruby-on-rails

How do I get the client (not the server) machine name in Rails from a controller? I've tried Socket.gethostnamebut that just get's the name of the server the Rails app is running on. Resolv.getname(request.remote_ip) just gives back an error saying there is no name for the client's IP address. I'm on Rails v4 if that matters.

I had the same problem using Resolv, I ended up doing this, as it is not always possible to retrieve the name of a machine from an ip address:
dns_lookup = ""
begin
dns_lookup = Resolv.getname(request.remote_ip)
rescue Resolv::ResolvError => e
end

Related

How to use Rails Low Level Cache in production?

I have an application which calls an API and authenticates using a token. I need to store this token and refresh it every so often, so I am using Rails.cache.fetch to do so in a custom class for handling the API calls. This works great on my local dev machine, but in production it is erroring. I am running a Mac for my dev machine and production is on Ubuntu 18. Here is the code that raises the error:
def authenticate
Rails.cache.fetch(#token, expires_in: 2.hours.to_i) do
login_uri = #base_uri + "auth/login"
auth_response = HTTParty.post(login_uri, body: { username: ENV["API_USERNAME"], password: ENV["API_PASSWORD"] } )
#token = auth_response.parsed_response["token"]
end
end
Here is the error I am getting:
Errno::ENOTDIR (Not a directory # rb_file_s_rename -
(/var/www/myapp/releases/20190424134348/tmp/cache/.00020190424-1954-lgpvq5,
/var/www/myapp/releases/20190424134348/tmp/cache/001/000/)):
It looks like Rails is attempting to rename or move the cache file for some reason. Looking at the server the /001 directory is there, but the subdirectory /000 does not exist.

Using agent name instead of IP as Address in SNMP4J

I'm trying to make a program that sends SNMP queries to some switches in the network.
Using the Net-snmp tools, I can send get requests to the switch using its name, and it works fine. But SNMP4J requires an IP address in CommunityTarget, so I get an IllegalArgumentException.
This is the relevant part of the code:
TransportMapping transport = new DefaultUdpTransportMapping();
transport.listen();
CommunityTarget comtarget = new CommunityTarget();
comtarget.setCommunity(new OctetString("public"));
comtarget.setVersion(SnmpConstants.version1);
comtarget.setAddress(new UdpAddress("switchName")); // exception happens here
comtarget.setRetries(2);
comtarget.setTimeout(1000);
How can I work around this?
You can get the IP address by using DNS resolution, like this answer says:
InetAddress address = InetAddress.getByName(switchName);
System.out.println(address.getHostAddress());

How to connect to a local idIRCServer

Well I have create a project which uses idIRCClient to connect to an IRC server and get responses from the server using that code
with ircClient do
if (edt1.text<>'') and (edt2.text<>'') and (edt3.text<>'') then
try
Host:=edt1.Text;
port:=StrToIntDef(edt2.Text,6667);
Nickname:=edt3.Text;
Username:=edt4.Text;
Connect;
except
if not Connected then
MessageDlg('Error',mtError,[],0);
end
else
MessageDlg('fill all the fields',mtInformation,[],0);
well when I use a host like "ix1.undernet.org" with port 6667 I get responses and a welcome message and I can send c2c private messages , whereas when I create an other module which contains idIRCServer and make it active then connect to it using host='localhost' and port=6667 , I get connected but I don't get any response from the server nor a welcome message even if it is set withing the Greeting Property
Thanks in advance

MVC Application cannot send mail through local HMailServer

I am building a form in C#, .NET, and MVC. On the back end the form will send its contents over email. For testing, I am using a local install of hMailServer.
Initially I set HMS to run as localhost.localdomain; the SMTP setting for "local host name" is localhost. I attempted to connect to it on port 587, like so:
SmtpClient smtp = new SmtpClient
{
Host = WebConfigurationManager.AppSettings["emailServer"],
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = true,
Credentials = networkCredential
};
I have double- and triple-checked that the credentials are the mail server user and password that I set. Here they are, in case this helps:
<add key="emailUser" value="user#localhost.localdomain"/>
<add key="emailPassword" value="~~~"/>
<add key="emailServer" value="localhost.localdomain"/>
When using localhost.localdomain, sending mail throws an exception, with the outer message: "Failure sending mail", and the inner message: "The remote name could not be resolved: 'localhost.localdomain'."
So I tried using companyname.com. Sending mail throws an exception, with the outer message: "Failure sending mail", and the inner message: "Unable to connect to the remote server."
I expect either my HMS domain config is wrong or my protocol config is wrong. The HMS documentation didn't help me, but I may not have known what to look for.
ETA
hMail server status shows zero processed messages in a week, despite all my testing.
Here is how I configured it for development:
Created host file entry like following:
local.myname.com 127.0.0.1
Once done, I opened command prompt and make sure it is updated. I tested it by following:
tracert local.myname.com
It should return 127.0.0.1 if host file entry is updated.
Next, in hmail, we need to create a new domain: local.myname.com and add an email address with password. so your email address would be something like admin#local.myname.com.
Next is, in advance you need to double check the protocols configuration and IP range vs authentication configuration as well.
In my case I configured to block external incoming and outgoing emails and skipped authentication for internal emails. So basically that;s what you can do in advance - IP range configuration. Then with the development, you just need to make sure all your emails are *#local.myname.com and it should work.
Also enable logging in hmail to get detailed error that can help solve the problem because hmail's help documentation works directly with their error codes nicely.
hMail is actually good choice for real emails. For development, I would recommend using smtp4dev though.

error 16550 : "not authorized for query on " mongolab

I have a Rails application running on heroku and i am connecting at two dbs hosted in mongolab (X and Y).
I have configured two heroku env variables containing the connection strings.
When i query on Y all works fine but when i query on X db it gives me the error 16550 : "not authorized for query on X.table".
I have setted up correctly both env variables for these connections and also have a valid user to access X db.
If i connect with the shell all works fine.
How can i solve this?
Here is the error message in rails:
{"status":"500",
"error":"The operation: #<Moped::Protocol::Query\n #length=88\n #request_id=4\n #response_to=0\n
#op_code=2004\n #flags=[:slave_ok]\n
#full_collection_name=\"X.table\"\n
#skip=0\n #limit=0\n
#selector={\"_id\"=>\"5252c92521e4af681a000002\"}\n
#fields=nil>\n
failed with error 16550: \"not authorized for query on X.table\"\n\n
See https://github.com/mongodb/mongo/blob/master/docs/errors.md\nfor details about this error."}
I solved this, if someone comes here with the same problem : look at your table model, if as in my case it is "stored_in" another database you must specify there the session of the the uri = evn variables on datbase.yml

Resources